Align JButtons in center of nested JPanel (BoxLayout) - java

I have a JFrame's child class and have the followiwng layout inside it. I have one big panel and one small buttonsPanel with two JButtons. I add buttons to the smaller panel and add that panel to the first one. Buttons are supposed to be centered, but it doesn't happen.
panel=new JPanel();
add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button1=new JButton("button1");
JButton button2=new JButton("button2");
buttonsPanel=new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(button1, CENTER_ALIGNMENT);
buttonsPanel.add(button2, CENTER_ALIGNMENT);
panel.add(buttonsPanel, BorderLayout.CENTER);
What should I do?

You really need to read the Swing tutorial on Layout Managers. You need to understand what a "constraint" is and when to use it.
buttonsPanel.add(button1, CENTER_ALIGNMENT);
The buttons panel uses a BoxLayout. It does not support any constraint, so the CENTER_ALIGNMENT makes no sense.
panel.add(buttonsPanel, BorderLayout.CENTER);
Again, panel uses a BoxLayout. You can't just use a BorderLayout constraint.
The easiest way to center a component (vertically and horizontally on a frame is to use a GridBagLayout.
So the basic code might be something like:
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(button1);
buttonsPanel.add(button2);
frame.setLayout( new GridBagLayout() );
frame.add(buttonsPanel, new GridBagConstraints());
If you want to try to use a BoxLayout then you need to use "glue" before and after the panel:
Box vertical = Box.createVerticalBox();
vertical.add(Box.createVerticalGlue());
vertical.add(buttonsPanel);
vertical.add(Box.createVerticalGlue());
Again, read the tutorial for more basic information about the BoxLayout.

Related

How to layout 2 buttons on one panel using BorderLayout

I recently found out that you can put a JPanel inside another JPanel, I tried it and it didn't quite do what I expected.I'm trying to have one button in the center and one button on the bottom right of the panel, I'm using a BorderLayout for all my panels.When I tried using two panels inside the center panel, the two panels were on the same line, but I've coded that one needed to be on the top and one on the bottom.Does anyone know how I could resolve this?
Sorry for not adding the code in the place, wasn't sure and I had already deleted it so I quickly made this, it is the same code I used in my project.
My code:
JFrame frame = new JFrame();
JButton btn = new JButton();
JPanel center = new JPanel();
JPanel top = new JPanel();
JPanel bot = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bot.add(btn);
center.add(top, BorderLayout.PAGE_START);
center.add(bot, BorderLayout.PAGE_END);
frame.add(center, BorderLayout.CENTER);
When I use this code, "bot" is going to be displayed on the right of "top".
I think it has to do with the layout of "center" but I'm not sure.
Set the LayoutManager of center to BorderLayout, with this method:
center.setLayout(new BorderLayout());

How to put buttons sticked to borders?

I mean with the standard flow layout I get this:
But I need something like this instead
Is there a way to put buttons like this in JPanel? Which layout should I choose?
Yes, Use BoxLayout and add Box.createHorizontalGlue() in between 2 buttons.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(new JButton("Left"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("Mid"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("Right"));

JSeparator between BorderLayout.CENTER and BorderLayout.EAST?

I want to make a program with a BorderLayout where the center and the east part is graphically separated. I tried the following, but unfortunately the separator is on the left side of the JFrame and not between the centerPanel and the rightPanel.
setLayout(new BorderLayout());
JPanel centerPanel = new JPanel();
JPanel rightPanel = new JPanel();
centerPanel.add(new JLabel("center"));
rightPanel.add(new JLabel("right"));
getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(rightPanel, BorderLayout.EAST);
getContentPane().add(new JSeparator(JSeparator.VERTICAL), BorderLayout.LINE_START);
Can someone please help me out?
You can use a Nested Layout approach to give your center panel a BorderLayout and add the vertical separator to this one instead of the content pane:
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(new JLabel("center"), BorderLayout.CENTER);
centerPanel.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.LINE_END);
JPanel rightPanel = new JPanel();
rightPanel.add(new JLabel("right"));
getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(rightPanel, BorderLayout.LINE_END);
Other notes
1) Since Java 1.4 when using BorderLayout the use of new constants is highly encouraged:
PAGE_START
PAGE_END
LINE_START
LINE_END
CENTER
From How to Use BorderLayout tutorial (bold text mine):
Before JDK release 1.4, the preferred names for the various areas were
different, ranging from points of the compass (for example,
BorderLayout.NORTH for the top area) to wordier versions of the
constants we use in our examples. The constants our examples use are
preferred because they are standard and enable programs to adjust to
languages that have different orientations.
2) Please avoid extending Swing components if you won't add any Swing related feature. See:
Extends JFrame vs. creating it inside the the program
Java Swing: Does the phrase “favor composition over inheritance”
apply?

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.

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