I am trying to make a java desktop application where I am using multiple JLabel
I want to set fix height and width of JLabel
. How can I achieve thi
You can set a fixed the size by setting the minimum, preferred and maximum size:
setMinimumSize(width, height);
setPreferredSize(width, height);
setMaximumSize(width, height);
As the Link from MadProgrammer, you need to override these methods, not using them from outside, based on the reasons mentioned in this link.
if this method not work
setMinimumSize(width, height);
setPreferredSize(width, height);
setMaximumSize(width, height);
then use it
setMinimumSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
Use JLabel.setPreferredSize(width, height);
The jpanel does not have a layout, try Jpanel pane =new JPanel(new FlowLayout());
An easy work around (without overriding existing classes) is to;
Create a JPanel specifically for the component
Set the size for the JPanel
Place the component within the JPanel
Place the JPanel where you would have originally placed the component
e.g.
JPanel mainPanel = new JPanel(); //Assume this is the panel you are placing things into.
JLabel nameLabel = new JLabel("Name:");
JPanel nameLabelPanel = new JPanel();
nameLabelPanel.setPreferredSize(new Dimension(100, 25));
JPanel.add(nameLabel);
mainPanel.add(nameLabelPanel);