I have a JPanel parent with three JPanel children inside. All of them currently use GridLayout and together represent a UML class. The problem is that when I add a new attribute or method, all 3 JPanels grow the same size.
Desire is that:
- The size of the header field always remains the same when adding a method or attribute.
- When adding a method, only the method panel increases, and the size of the title bar and attribute remains unchanged.
- When adding an attribute, only the attribute panel grows, while the size of the remaining panels remains unchanged.
Parent JPanel can already automatically grow / contract whenever a method / attribute is added. I play with the GridBagLayout atm, but I do not get anywhere near the results of desire.
Is there a simple (or simpler) way to solve this problem ?!
Here are some photos to show my situation.
Newly created UML class =>
this is how he is currently behaving => 
But I want this =>
or this =>
or this => 
Edit2: added new photos for clarity. I'm sorry if the original version is misleading.
Edit3: YESS! I took it apart! Feeling forever !! Here is the SSEEC:
Kids panel
import java.awt.Component; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.MouseEvent; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.event.MouseInputAdapter; import javax.swing.event.MouseInputListener; public class APanel extends JPanel{ private JTextField tf; public APanel() { this.setLayout(new GridLayout(0,1)); this.addMouseListener(mouseInputListener); } MouseInputListener mouseInputListener = new MouseInputAdapter() { @Override public void mouseClicked(MouseEvent e) { System.out.println("Adding a new text field!"); tf = MyTF.create("Double click"); addNewTF(tf); Component source = (Component) e.getSource(); Container c = source.getParent(); while(true) { if(c instanceof PPanel) break; else c=c.getParent(); } PPanel p = (PPanel) c; p.expand(); } }; public void addNewTF(JTextField tf) { this.add(tf); this.setSize(this.getWidth(), this.getHeight()+tf.getHeight()); this.revalidate(); this.repaint(); } }
Parent Panel:
import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class PPanel extends JPanel{
Help Class:
import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.event.MouseEvent; import javax.swing.JTextField; import javax.swing.event.MouseInputAdapter; import javax.swing.event.MouseInputListener; public class MyTF { public static JTextField create(String name) { final JTextField tf = new JTextField(name); System.out.println(tf.getPreferredSize()); tf.setPreferredSize(new Dimension(100,33)); tf.addMouseListener(mouseInputListener); return tf; } static MouseInputListener mouseInputListener = new MouseInputAdapter() { @Override public void mouseClicked(MouseEvent e) { Component source = (Component) e.getSource(); Container c = source.getParent(); while(true) { if(c instanceof PPanel) break; else if(c instanceof APanel) { c.dispatchEvent(e); c = c.getParent(); break; } else c=c.getParent(); } c.dispatchEvent(e); } }; }
I refused to play with GridBagLayout, it was too much for me. Then I tried borderLayout, as suggested, but could not get it to work, just like me. Then, finally, BoxLayout, it should have worked, but there was an error in my code! So when I tried the 0verbose code, offering and playing with it, it failed! Until I finished SSEEC, did the final compilation and started it before I decided to publish (I practically gave up at this point), then I realized that it worked ... A panel that can grow in its own space, they do not interfere with each other.
I was like WTF!
I returned to my code and compared it with SSEEC, and an error occurred, the code to increase the height of the panel was in the wrong place, so they kind of ate each other.
Even better! I can specify the distance between the middle box with the field above and below it by one pixel. This means that I can still use the mKorbel trick to draw a return line separating these fields!
Edit 4: is there a way to set the size of the component? If you run SSEEC, you will notice that after adding a JTextField it will be huge! It is more than a container ...