- I am trying to add combo box to the JPanel dynamically but the combo box occupying entire panel.According to the combo box count the size of the combo boxes are changing but I want fixed size of the combo box and I need to create the combo boxes one by one means below of another combo /in a new line.
How to set the location of the components in a panel.
JComboBox startDate = new JComboBox(); startDate.setPreferredSize(new Dimension(80,25)); jPanelStartDate.add(startDate); jPanelStartDate.setLayout(new GridLayout(0, 3, 10, 10)); jPanelStartDate.revalidate();
Asked
Active
Viewed 615 times
1

Andrew Thompson
- 168,117
- 40
- 217
- 433

suma2020
- 17
- 5
-
Can you show the codes for your JPanel or even the JFrame? If not, we can only diagnose by guessing. – user3437460 Mar 08 '16 at 17:55
-
1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) .. – Andrew Thompson Mar 09 '16 at 00:10
-
.. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) Ask one question per thread. SO is a Q&A site, not a help desk. – Andrew Thompson Mar 09 '16 at 00:10
1 Answers
0
Ok, you have more than one option. You can use a BoxLayout with a Y_Axis and a rigid area or you can use the more advanced, complex and dynamic GridBagLayout. The following is an example with a BoxLayout and rigid areas.
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
for(int index = 0; index < 5; ++index){
JComboBox<String> box = new JComboBox<>(new String[]{"a", "b", "c"});
box.setMaximumSize(new Dimension(50, 50));
box.setMinimumSize(new Dimension(50, 50));
panel.add(box);
panel.add(Box.createRigidArea(new Dimension(10, 10)));
}
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.setVisible(true);
You can change the dimensions, specially of the rigid area to adjust the spaces and the sizes of the components according to your needs.

m1o2
- 1,549
- 2
- 17
- 27
-
Now its not occupying complete panel but same issue.based on the no.of combo boxes the size is changing.once it gets to the least size ,the size remaining as it though combo boxes are added and I want to add combo boxes vertically .Grid Layout adding horizontally.I have used box layout but the combo box occupying complete panel and there is no space between two combo's.jPanelStartDate.setLayout(new BoxLayout(jPanelStartDate, BoxLayout.Y_AXIS)); – suma2020 Mar 08 '16 at 16:12
-
@suma2020 I have adjusted my answer according to your comment. Please see whether the answer is more helpful now. – m1o2 Mar 08 '16 at 18:21