Search code examples
javaswingalignmentjlabel

Make JLabel text grow right


Hi is it possible to have a JLabel text growing to the right instead of left? I think the only way to force it in my program is to use setMinimumSize (which, for some reason sets the maximum size, when I put too much text, it will add three dots at the end) Any ideas?

min = new Dimension(100, 10);
mylabel.setHorizontalAlignment(JLabel.RIGHT); //does not do anything
mylabel.setPreferredSize(pref); //does not do anything
mylabel.setMaximumSize(max); //does not change anything
mylabel.setMinimumSize(min); //constrains max size instead of min
mylabel.setBorder(border);

gbc.gridx = 2;
labels.add(mylabel, gbc); //labels is a JPanel

Solution

  • Well I found the solution!

    It was enough to add this line after gridx change:

        gbc.anchor = GridBagConstraints.EAST;