A a rule of thumb, Java layout managers will ignore the size property of any component. Instead, layout managers will size a component anywhere between their minimum and maximum sizes, regardless of what is set for size based on what is "best" for the current layout. If you really need to force a size, use both .setMaximumSize()
and .setMinimumSize()
on a component to force it to a specific size.
private void setCategoryPanel(){
//set the color label category
JPanel panelCategory=new JPanel();
panelCategory.setLayout(new FlowLayout(FlowLayout.LEADING));
JButton btnCategory1=new JButton(" ");
Dimension btnCategory_1_Dimension = new Dimension(categoryButtonWidth, categoryHight);
btnCategory_1.setSize(btnCategory_1_Dimension);
btnCategory_1.setMaximumSize(btnCategory_1_Dimension);
btnCategory_1.setMinimumSize(btnCategory_1_Dimension);
btnCategory_1.setBackground(Color.red);
btnCategory_1.addActionListener(this);
panelCategory.add(btnCategory_1);
JTextField txtCategory1 = new JTextField(20);
Dimension txtCategory_1_Dimension = new Dimension (categoryTextFieldWidth, categoryHight);
txtCategory_1.setSize(txtCategory_1_Dimension);
txtCategory_1.setMinimumSize(txtCategory_1_Dimension);
txtCategory_1.setMaximumSize(txtCategory_1_Dimension);
panelCategory.add(txtCategory_1);
JButton btnCategory_2=new JButton(" ");
Dimension btnCategory_2_Dimension = new Dimension(categoryButtonWidth, categoryHight);
btnCategory_2.setSize(btnCategory_2_Dimension);
btnCategory_2.setMaximumSize(btnCategory_2_Dimension);
btnCategory_2.setMinimumSize(btnCategory_2_Dimension);
btnCategory_2.setSize(new Dimension ( categoryButtonWidth, categoryHight));
btnCategory_2.setBackground(Color.YELLOW);
btnCategory_2.addActionListener(this);
panelCategory.add(btnCategory_2);
JTextField txtCategory2 = new JTextField(20);
Dimension txtCategory_2_Dimension = new Dimension (categoryTextFieldWidth, categoryHight);
txtCategory_2.setSize(txtCategory_1_Dimension);
txtCategory_2.setMinimumSize(txtCategory_1_Dimension);
txtCategory_2.setMaximumSize(txtCategory_1_Dimension);
panelCategory.add(txtCategory2);
this.add(panelCategory, BorderLayout.NORTH);
}
But this is generally frowned upon, since the layout manager should know best. Usually strict sizing inside a layout manager means poor design and using the layout managers incorrectly. The Java Tutorial gives some excellent advice on layout managers. It can be frustrating to get a swing layout manager to give you the results you want, but you will usually be thankful in the end that you have a program that properly resizes with the wishes of the user without pesky pixel requirements on sizes, especially when you consider that your program could render wildly differently on a different system and all of your hard work on pixel adjustments goes down the drain in an instant.