0

I have a JPanel with several JLabels in it and a JTextPane. I want them to be below each other (so no two two labels on the same line), and aligned to the left. I have tried several things:

  • Using a BoxLayout with BoxLayout.Y_AXIS works for properly getting all elements below each other. However, while the JTextPane correctly aligns to the left, the JLabels stay centered, even when calling several methods to try and get the alignment to the left (see code below).
  • Using a GridLayout will correctly put the elements below each other and align them to the left, but then the elements will be vertically spread all over the JPanel with huge spaces between the text lines. I want all elements to be in the top of the panel as far as possible.
private final void init() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(new JLabel("a"));
        final JLabel label = new JLabel();
        final JTextPane pane = new JTextPane();
        add(label);
        add(pane);
        // these three lines seem to be ignored
        label.setHorizontalAlignment(SwingConstants.LEFT);
        label.setAlignmentX(LEFT_ALIGNMENT);
        label.setHorizontalTextPosition(SwingConstants.LEFT);
        
        pane.setForeground(Color.RED);
        pane.setEditable(false);
}

How can I get this right?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Priv
  • 812
  • 2
  • 9
  • 23
  • You have to execute the JLabel methods before you add the JLabel to the JPanel. – Gilbert Le Blanc May 28 '21 at 09:11
  • Other methods like setForeground, setEditable and setText are working fine, though.. – Priv May 28 '21 at 09:12
  • Ok, it's a really really great idea to organize your code such that you execute the Swing component methods before you add the component to the JPanel. – Gilbert Le Blanc May 28 '21 at 09:15
  • I just tried it and it makes no difference. Also, I need to execute some methods after adding the component, because the text of the components can change. – Priv May 28 '21 at 09:17
  • Can you make this into a compilable example? Aligning the BoxLayouts can be tricky. – matt May 28 '21 at 09:35
  • [here](https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#features) are some examples of alignments. – matt May 28 '21 at 09:36
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson May 28 '21 at 10:22

1 Answers1

0

I want them to be below each other (so no two two labels on the same line), and aligned to the left.

You need to set the alignment on all components, even the JTextPane.

add(new JLabel("a"));

How do you expect to change the alignment of that label when you don't have a reference to it?

final JLabel label = new JLabel();
final JTextPane pane = new JTextPane();
add(label);
add(pane);

You don't set the alignment of the above components.

Read the section from the Swing tutorial on Fixing Alignment Problems

camickr
  • 321,443
  • 19
  • 166
  • 288