-1

I need to totally align components to the JTextArea components, I am currently using a BoxLayout and I already used the setAlignmentX and setHorizontalAlignment to LEFT but it's not working. Here I upload an image to make clearer what I mean. For example look at "+ Pla PLAMARC" it's clearly not aligned with the text area component.

Components not aligned with JTextArea components

By the moment this is the code:

//Declarations
private JLabel nomPla;
private JTextArea infoPla;
private JScrollPane textAreaScroll;

//Inside the constructor
nomPla = new JLabel();
infoPla = new JTextArea(2, 50);
textAreaScroll = new JScrollPane(infoPla);

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

nomPla.setAlignmentX(Component.LEFT_ALIGNMENT);
nomPla.setHorizontalAlignment(nomPla.LEFT);
textAreaScroll.setAlignmentX(Component.CENTER_ALIGNMENT);

this.setBorder(new EmptyBorder(new Insets(25, 25, 25, 25)));

this.add(nomPla, BorderLayout.NORTH);
this.add(textAreaScroll, BorderLayout.NORTH); //Orientacions

I am clearly telling nomPla to be on the leftside, but this is not the same as the JTextArea.

How is this done then?

Marc43
  • 491
  • 2
  • 6
  • 15
  • *"How is this done then?"* How are you doing it now? For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 04 '18 at 16:30
  • use a borderLayout and put each JTextArea component in each section you want (ie. left). – Leemi Dec 04 '18 at 16:33
  • @AndrewThompson already edited... – Marc43 Dec 04 '18 at 16:39
  • 1
    @Marc43, `already edited` - that is not an "MCVE". We can't compile that code and test to see your described behaviour. – camickr Dec 04 '18 at 16:43

1 Answers1

3

I already used the setAlignmentX and

The setAlignmentX(...) needs to be applied to all the components you add to the BoxLayout if you want all to be left aligned with respect to the BoxLayout.

Edit:

I just want the labels to be on the left side, not the JTextArea components..

Then you need to use a wrapper panel for the BoxLayout Panel.

For example:

JPanel wrapper = new JPanel( new FlowLayout(FlowLayout.CENTER) );
wrapper.add(yourBoxLayoutPanel);
frame.add(wrapper);

Now all the components in the BoxLayout will be left aligned and the BoxLayout panel will be center aligned in the wrapper panel.

Layout management is about nesting panels with different layout manager to achieve you desired effect.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I finally used a GridBagLayout; But I could set your answer to work (not for this case), but I needed to set the alignments for every component to really see the alignment, setting only one of them was not working. Thank you and sorry for the inconvenience I could provoke. – Marc43 Dec 08 '18 at 16:04
  • Your question was about a BoxLayout and the component alignments. I provided an explanation why your original code did not work and provided a solution to fix the problem. This will allow you to make a better informed decision in the future when choosing a layout manager. `I finally used a GridBagLayout;` - there is always more than one layout solution. If you find it easier to set multiple constraints for each component rather than setting the alignment for each component, then that is your choice, but if a question is answered it should be accepted even if you use a different solution. – camickr Dec 08 '18 at 16:33
  • Yeah but the problem is that I can't directly validate your answer if I can't test it, huh? But I could. Thank you. – Marc43 Dec 09 '18 at 09:20
  • Yes you could directly validate my answer. It would take 20 lines of code maximum to validate. You create a panel with a box layout. you create an label and text area and set the alignment and add them to the panel. Then you create the wrapper panel and add the box layout panel to it. Finally you add the wrapper panel to the frame. It will take 5 minutes to do this. This is the point of the "MCVE". Learn how to simplify the problem and understand programming concepts, intead of thinking about your entire application. Then problem solving becomes much easier. – camickr Dec 09 '18 at 15:37