I have created design GUI using java by programmatically. I want to add some of JPanel to my JFrame (mainwindow). I set JFrame layout using BoxLayout. I am use BoxLayout because want the children resize only width. But, when I resize the mainwindow (JFrame), children width and height is resized. BoxLayout expanding all children width and height.
This is code to add JPanel to my JFrame using BoxLayout :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
JPanelProgress panel1 = new JPanelProgress(); panel1.setBackground(Color.red); JPanelProgress panel2 = new JPanelProgress(); panel2.setBackground(Color.green); JPanelProgress panel3 = new JPanelProgress(); panel3.setBackground(Color.yellow); BoxLayout boxlayout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS); jPanel1.setLayout(boxlayout); jPanel1.add(panel1); jPanel1.add(panel2); jPanel1.add(panel3); |
Output using this code is like this :
Alternatively, we can use Box.Filler prevent BoxLayout resized the childen height. I have modified my code like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
JPanelProgress panel1 = new JPanelProgress(); panel1.setBackground(Color.red); JPanelProgress panel2 = new JPanelProgress(); panel2.setBackground(Color.green); JPanelProgress panel3 = new JPanelProgress(); panel3.setBackground(Color.yellow); BoxLayout boxlayout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS); jPanel1.setLayout(boxlayout); jPanel1.add(panel1); jPanel1.add(panel2); jPanel1.add(panel3); //prevent all child height resized jPanel1.add(new Box.Filler(new Dimension(0, 0), new Dimension(0, Short.MAX_VALUE), new Dimension(0, Short.MAX_VALUE))); |
The result using this modified code (prevent BoxLayout expanding all children) is like this :
Add Box.Filler to our BoxLayout prevent all children expanding they height.
Source : http://stackoverflow.com/questions/14010864/keep-boxlayout-from-expanding-children
My name is Toto Sugito. This is my notes when I try something. Maybe, this is NOT THE BEST SOLUTION for your problems. If you have other method or idea that better with my post, please share in this blog. Thank for visiting my site.