java - How do you stop a JLabel changing its size when its text changes? -


i'm generating jcomponents in code , using gridbag layout arrange them. layout consists of 12 rows , 3 columns, each row consisting of jslider, jcheckbox , jlabel. here's code i'm using generate ui:

final int num_motors = 12;  // panel i'm adding components to. pnlmotorsliders.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints();  (int = 0; < num_motors; ++i) {     c.gridy = i;      // create slider     jslider slider = new jslider(swingconstants.horizontal, 10, 4085, 10);     c.fill = gridbagconstraints.horizontal;     c.gridx = 0;     c.weightx = 0.9;     pnlmotorsliders.add(slider, c);      // create checkbox     jcheckbox checkbox = new jcheckbox();     checkbox.setopaque(true);     checkbox.setbackground(color.blue);     c.fill = gridbagconstraints.none;     c.gridx = 1;     c.weightx = 0.1;     pnlmotorsliders.add(checkbox, c);      // create current label     jlabel label = new jlabel("0");     label.setborder(borderfactory.createlineborder(color.red));     c.fill = gridbagconstraints.horizontal;     c.gridx = 2;     c.weightx = 0.2;     pnlmotorsliders.add(label, c); } 

the problem i'm having when set text in of jlabels, change width , affect rest of layout, if width of text i'm setting appears smaller width of jlabel. following 2 screenshots demonstrate mean (the red , blue borders debugging purposes):

the layout before changing jlabel text.

the layout after changing bottom jlabel text. notice how label has gotten bigger.

i've set text on bottom jlabel "-12". though jlabel appears wider text, has changed size, affecting rest of layout.

why happening , can prevent it?

you can fix size of labels setting minimum, prefered , maximum size:

label.setminimumsize(width, height); label.setpreferedsize(width, height); label.setmaximumsize(width, height); 

also make sure set gridbagconstraints#fill none, although not sure if still neccessary (i think is).

edit: btw, rid of nasty dashed lines around focused component, can set not focusable:

slider.setfocusable(false); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -