BoxLayout inner JPannels alignment - how can I set them to left? - java

I have been struggling with BoxLayout. I have one JPanel containing 3 inner panels. The external panel is set to BoxLayout with vertical axis, the first and second inner panels were set to set to GridBagLayout and the last one to the flowLayout (left).
The result is that first and second inner containers are aligned to the center(I guess that is is caused by the default behavior of GBL without setting the weightx). How can I align them to the left?
Do I understand correctly that anchor would work inside a cell but not align the whole grid? And that weightx would distribute the space among the cells, but not move the whole container to the left? If that is the true, should I use another component as a wrapper, or is there another way?
panAdv1 = new JPanel();
add(panAdv1);
panAdv1.setLayout(new BoxLayout(panAdv1, BoxLayout.PAGE_AXIS));
// PANEL 1 //
panel1 = new JPanel();
panAdv1.add(panel1);
panel1.setLayout(new GridBagLayout());
....
// PANEL 2 //
panel2 = new JPanel();
panAdv1.add(panel2);
panel2.setLayout(new GridBagLayout());
...
//PANEL 5 - buttons //
panel5 = new JPanel();
panAdv1.add(panel5);
panel5.setLayout(new FlowLayout(FlowLayout.LEFT));

Read the section from the Swing tutorial on Fixing Alignment Problems. The BoxLayout respects the "alignment" property of the component.
How can I align them to the left?
The default for a JPanel would be to have the panel center aligned, so you will need to change the alignment to be left aligned.

Related

Adding Multiple Panels with Different Sizes in One Frame

I am trying to add more panels to my frame, but the program seems to ignore all the other panels than the first one I added. How should I add the panels?
I have checked other people's questions and their answers, but none of them seemed to be the solution to mine.
frame = new JFrame("Hey");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mid = new JPanel(new GridLayout(7,7));
JPanel top = new JPanel();
frame.add(top);
frame.add(mid);
frame.pack();
frame.setVisible(true);
The program ignores the "top" panel, along with the buttons I added to it.
From the JFrame documentation:
The default content pane will have a BorderLayout manager set on it.
So you should use the BorderLayout regions in order to add your JPanels to the JFrame's content pane like this:
frame.add(top, BorderLayout.NORTH);
frame.add(mid, BorderLayout.CENTER);
Otherwise, the BorderLayout will default to adding everything to the CENTER region:
As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER:
Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);
and you will only see the JPanel added last because:
Each region may contain no more than one component

BoxLayout ignores subpanel alignment

I have a JPanel with a BoxLayout manager that contains subpanels. I want the components inside these subpanels to have a left alignment, but they always appear centered.
It looks like BoxLayout correctly aligns components that are inserted directly, but fails to do that when they are inside a subpanel.
I have modified the example found in http://www.java2s.com/Tutorial/Java/0240__Swing/YAxisAlignment.htm so each button is placed inside a subpanel, and then the subpanel is placed inside the main panel with the BoxLayout manager:
public class YAxisAlignX {
private static Container makeIt(String title, float alignment) {
String labels[] = { "--", "----", "--------", "------------" };
JPanel container = new JPanel();
container.setBorder(BorderFactory.createTitledBorder(title));
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
// modified loop. the original version does not create JPanel pan.
// adds the buttons directly the the JPanel container with the
// BoxLayout
for (int i = 0, n = labels.length; i < n; i++) {
JPanel pan = new JPanel();
JButton button = new JButton(labels[i]);
pan.add(button);
button.setAlignmentX(alignment);
pan.setAlignmentX(alignment);
container.add(pan);
}
return container;
}
public static void main(String args[]) {
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panel1 = makeIt("Left", Component.LEFT_ALIGNMENT);
Container panel2 = makeIt("Center", Component.CENTER_ALIGNMENT);
Container panel3 = makeIt("Right", Component.RIGHT_ALIGNMENT);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
frame.pack();
frame.setVisible(true);
}
}
If you execute this version, you can see that the buttons are all centered, despite the alignment set. Why is this happening? Is there any solution? In my case, every subpanel contains several components, and I do not want to add then directly to the main panel.
Thank you very much.
But they are aligned!
First of all, setAlignmentX changes a property at a JComponent level, the layout in which these components are placed may or may not use this information. For example, BoxLayout uses it but FlowLayout and BorderLayout don't.
In your case you are placing some panels with a vertical BoxLayout and you are aligning them in various ways, and it works! It just so happens that the the panels stretch themselves to fit the whole column so actually aligning doesn't change their appearance. You can see this by setting a Border around the panels:
pan.setBorder(BorderFactory.createLineBorder(Color.red));
See:
The fact that the panels contain a button or anything else is mostly irrelevant (it only influences the size that the panel wants to take, and not definitely), the BoxLayout is aligning panels not what is there inside the panels, that's the work of each panel's layout. That's why the buttons wouldn't have its alignment affected by the BoxLayout.
Now how are those buttons deciding its alignment? Well that's up to the layout they are in. The buttons are inside the pan panel that is using the default LayoutManager FlowLayout. Now, as I said, FlowLayout doesn't care for the alignmentX/Y property so the line:
button.setAlignmentX(alignment);
Doesn't accomplish anything. To align in a FlowLayout you need to change its alignment field through FlowLayout.setAligment(int) (docs), you can also do this in the constructor, so If we change the pan declaration to:
JPanel pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
You'll also have the buttons aligned to the left inside their panels:
Of course, all the columns are aligned to the left since the parameter float alignment of makeIt does not influence the FlowLayout's alignment just the BoxLayout's one (and it doesn't matter). You might want to change that argument to an int and call the function with the different FlowLayout constants.
All in all, in the original version the line button.setAlignmentX(alignment); made sense because the buttons were added directly to the container panel and the BoxLayout aligned them properly:
However once you put the buttons inside other panels, the BoxLayout starts aligning the panels (which because of how panels works inside a BoxLayout they were being stretched to fill the whole width) not the buttons, and the buttons alignment is up to the panels layout. That's how it has to work so we can make consistent nested layouts.
I hope that's clear.

Swing display JPanels in a row to the right corner

I am creating a custom decoration for my first customized Swing program window, I just started with layout managers, and it looks like I am doing something wrong, first I used BorderLayout and BorderLayout.EAST or WEST to display on the corner, but it only allows one panel to be displayed on a corner, like it won't display in a row.
Looks like this:
(source: gyazo.com)
With that code:
this.panel.setLayout(new BorderLayout());
this.panel.add(this.createToolButton("X"), BorderLayout.EAST);
But if I add another panel, the newest panel will be on the previous one (Note I used panels because JButton hates me, with it's default styles doesn't let me make it flat)
Now I used GridBagLayout
this.panel.setLayout(new GridBagLayout());
Box panels = new Box(BoxLayout.X_AXIS);
panels.add(this.createToolButton("X"));
this.panel.add(panels, BorderLayout.EAST);
But on run I get
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)
What am I doing wrong? how can I have the panels floated to right one by one?
EDIT:
this.panel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.WEST;
this.panel.add(this.createToolButton("X"), gc);
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)
Use GridBagConstraint with GridBagLayout.
this.panel.setLayout(new GridBagLayout());
GridBagConstraint gc = new GridBagConstraint();
// set different properties of GridBagConstraint as per your need
this.panel.add(panels, gc);
Read more How to Use GridBagLayout read more about properties of GridBagConstraint.
Here is The Example to learn more about it.
EDIT
You can try with FlowLayout with right alignment:
JPanel titlePanel=new JPanel(new GridLayout(1,2));
titlePanel.setBorder(new LineBorder(Color.BLACK));
titlePanel.setBackground(Color.LIGHT_GRAY);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
panel.setBackground(Color.LIGHT_GRAY);
titlePanel.add(new JLabel("Title",JLabel.LEFT));
panel.add(new JButton("X"));
titlePanel.add(panel);
frame.add(titlePanel, BorderLayout.NORTH);
// add panel in the north section of the undecorated JFrame
// that by default uses BorderLayout
snapshot:
If you are using
this.panel.add(panels, BorderLayout.EAST);
then you should use BorderLayout not GridBagLayout.
this.panel.setLayout(new BorderLayout());
You can read more in the documentation How to Use BorderLayout.

How to set the size of JTextPane according to the size of JPanel?

I want to set the size of the JTextPane according to the size of the panel so that when i add other panels, it changes accordingly. But it just gives a small text pane in the center and when i add some text, it's size changes accordingly.
JPanel panel = new JPanel();
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane();
pane.add(txt);
panel.add(pane,BorderLayout.CENTER);
add(pane);
now the jtextpane just appears at the center of the screen like a small box. I want it to appear according to the size of the panel
JPanel uses FlowLayout by default which sizes components according to their preferred sizes. You can use BorderLayout which will use the maximum area possible.
Also using constraints such as BorderLayout.CENTER has no effect unless the container is actually using BorderLayout. Dont add components to the JScrollPane. This will replaces all components within the view of the component. Instead set the JTextPane as the ViewPortView, for example
JPanel panel = new JPanel(new BorderLayout());
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane(txt);
// pane.add(txt); remove
panel.add(pane, BorderLayout.CENTER);
Read:
How to Use BorderLayout
How to Use Scroll Panes
You added pane twice. Add panel to your base (a JFrame?) instead and remember to actually set your JPanel to use BorderLayout.

Using BorderLayout for adding 4 in-line Components to a JPanel

In my application, there are 4 panels. And i need to insert them into the main panel, which uses BorderLayout. The 4 panels are...
A thin Image strip.
4 buttons just below above
A TextField covering the complete page.
An about at end.
This is my code...
add(imageLabel, BorderLayout.NORTH);
add(buttonPanel,BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
add(about, BorderLayout.PAGE_END);
When I do this, the buttonPanel disappears. How can I achieve what I need?
I usually try to keep a maximum of 3 components in any BorderLayout, so I would do it like this...
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel innerPanel= new JPanel(new BorderLayout());
innerPanel.add(buttonPanel,BorderLayout.NORTH);
innerPanel.add(logScrollPane, BorderLayout.CENTER);
innerPanel.add(about, BorderLayout.SOUTH);
outerPanel.add(imageLabel, BorderLayout.NORTH);
outerPanel.add(innerPanel,BorderLayout.CENTER);
As long as you keep the 'maximum-stretched' component in the CENTER (in this case, your logScrollPane) then it'll always work. If you want to use the panel, such as setting it on a JFrame, just use add(outerPanel).
Don't be afraid of BorderLayout - the ability of this layout to auto-expand the CENTER component to fill the available space make it a very powerful and very important LayoutManager!

Categories

Resources