0

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

Daniel M.
  • 1,433
  • 13
  • 24
  • Also, for the node itself, the `.toString()` method is the title for this kind of node, so it should display the "Example Child Node" in the title area. – Daniel M. Jun 07 '15 at 00:33

4 Answers4

3
// ..
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.)
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I was able to remove the setMinimumSize, now that GridBag manages the size of the components (I want the text input areas to expand when the window is resized, but the size doesn't have to be exact). – Daniel M. Jun 07 '15 at 13:55
0

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.

Daniel M.
  • 1,433
  • 13
  • 24
  • 2
    It also shows you the important of providing sizing hints to text components (like `setColumns` and `setRows` or via the constructor) – MadProgrammer Jun 07 '15 at 03:35
  • 1
    *"it goes to show that the problem is where you'd least expect it"* One of the many reasons we prefer people to post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jun 07 '15 at 04:28
0

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);
Programmer
  • 445
  • 4
  • 12
  • The problem was that the GridBagLayout (that contained the panel whose code is above) was crunching the JPanel into a tiny square (it wasn't set to have any resize weight). – Daniel M. Jun 07 '15 at 13:50
  • 1-, this answer was given 3 hours earlier. No need to add duplicate answers. – camickr Jun 07 '15 at 17:34
0
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.

A.K.
  • 2,284
  • 3
  • 26
  • 35