GridBagLayout is displaying JTextField and JTextArea as short, vertical lines

680 Views Asked by At

Some background: I am making a program that uses a JTree full of nodes of various types. The TreeSelectionListener determines what type of node it is, clears an inner JPanel of components, then adds the appropriate JPanel to the JPanel it just cleared, to edit properties specific to that type of node. The JPanel uses GridBagLayout, but the text fields aren't displaying correctly (Note: the title is a JTextField, the description is a JTextArea: The JPanel

Code for the JPanel is as follows:

class nodePanel extends JPanel{
        public nodePanel(DefaultMutableTreeNode info){
            super(new GridBagLayout());

            GridBagConstraints c = new GridBagConstraints();
            c.insets=new Insets(10,10,10,10);
            c.gridx=0;
            c.gridy=0;
            c.gridheight=1;
            c.gridwidth=1;
            c.weightx=0;
            c.weighty=0;

            c.fill=GridBagConstraints.NONE;
            c.anchor=GridBagConstraints.EAST;
            this.add(new JLabel("Title: "),c);

            c.gridy=1;
            c.weighty=2;
            c.gridheight=2;
            this.add(new JLabel("Description: "),c);

            c.gridx=1;
            c.gridy=0;
            c.gridheight=1;
            c.gridwidth=2;
            c.weightx=100;
            c.weighty=0;
            c.anchor=GridBagConstraints.WEST;
            c.fill=GridBagConstraints.HORIZONTAL;
            JTextField title = new JTextField();
            title.setMinimumSize(new Dimension(300,25));
            this.add(title,c);

            c.gridy=1;
            c.gridheight=2;
            c.weighty=200;
            JTextArea description = new JTextArea();
            JScrollPane descriptionScroll= new JScrollPane(description);
            descriptionScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            descriptionScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
            this.add(descriptionScroll, c);
        }
}

Thank you

4

There are 4 best solutions below

1
On BEST ANSWER
// ..
JTextField title = new JTextField(20);  // suggest a size in columns!
//title.setMinimumSize(new Dimension(300,25)); // don't call these methods! (1)
this.add(title,c);

c.gridy=1;
c.gridheight=2;
c.weighty=200;
JTextArea description = new JTextArea(3, 15); // suggest a size in rows/cols!
// ..    
  1. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
2
On

OK, the problem wasn't with the JPanel itself- the enclosing JPanel had Gridbag, but wasn't set to have weight or any dimensions. Guess it goes to show that the problem is where you'd least expect it.

0
On
class nodePanel extends JPanel{
        public nodePanel(DefaultMutableTreeNode info){
            super(new GridBagLayout());

            GridBagConstraints c = new GridBagConstraints();
            c.insets=new Insets(10,10,10,10);
            c.gridx=0;
            c.gridy=0;
            c.gridheight=1;
            c.gridwidth=1;
            c.weightx=0;
            c.weighty=0;
            c.fill=GridBagConstraints.NONE;
            c.anchor=GridBagConstraints.EAST;
            this.add(new JLabel("Title: "),c);

            c.gridy=1;
            c.weighty=2;
            c.gridheight=2;
            this.add(new JLabel("Description: "),c);

            c.gridx=1;
            c.gridy=0;
            c.ipadx=20;
            c.gridheight=1;
            c.gridwidth=2;
            c.weightx=100;
            c.weighty=0;
            c.anchor=GridBagConstraints.WEST;
            c.fill=GridBagConstraints.HORIZONTAL;
            JTextField title = new JTextField(10);
            title.setMinimumSize(new Dimension(300,25));
            this.add(title,c);

            c.gridy=1;
            c.gridheight=2;
            c.ipadx=20;
            c.ipady=5;
            c.weighty=200;
            JTextArea description = new JTextArea(5,2);
            JScrollPane descriptionScroll= new JScrollPane(description);
            descriptionScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            descriptionScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
            this.add(descriptionScroll, c);
        }
}

use this code i changed in your code. you are not use c.ipadx i am add this field and it's should be work.

2
On

It,s because you did not give length to it.If you add length in it it will work fine.Replace your code with this

JTextField title = new JTextField(10);

JTextArea description = new JTextArea(2,10);