Artemkller545
Artemkller545

Reputation: 999

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:

img
(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);

Upvotes: 1

Views: 595

Answers (2)

Braj
Braj

Reputation: 46841

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:

enter image description here

Upvotes: 4

Aniket Thakur
Aniket Thakur

Reputation: 68905

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.

Upvotes: 2

Related Questions