1 year ago
#345239
OrlandoVSilva
Cannot use GridbagLayout in JPanel inside another Jpanel
So my issue is, I have my frame(BorderLayout) and three Jpanels (north, center, south). The JPanel on the center(BorderLayout) has two Jpanels (north, south).
The Panel on the south(GridBagLayout), gives me this error "Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)", on line 44.
This is what happens without line 44 and 47 (image attached):
What I want on that JPanel for testing purposes (for now) is to add 2 buttons, but I get the error mentioned above.
The code beneath relates to the center panel of the frame, mentioned above:
package com.system.reservation.airline.panels;
import javax.swing.*;
import java.awt.*;
public class BodyPanel extends JPanel {
JLabel login_label;
public BodyPanel(){
this.setBackground(Color.BLUE);
this.setLayout(new BorderLayout());
//------------ Login Panel ------------------------
JPanel login_panel = new JPanel(new BorderLayout());
login_panel.setPreferredSize(new Dimension(400, 100));
login_panel.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 5));
login_label = new JLabel();
login_label.setText("Login");
login_label.setHorizontalAlignment(JLabel.CENTER);
login_label.setFont(new Font("Arial", Font.PLAIN, 20));
login_panel.add(login_label, BorderLayout.CENTER);
//------------ Login Panel ------------------------
//------------ Input Panel ------------------------
JPanel input_fields_panel = new JPanel(new GridBagLayout());
input_fields_panel.setPreferredSize(new Dimension(400, 150));
input_fields_panel.setBorder(BorderFactory.createLineBorder(Color.black, 5));
Button btn1 = new Button("Btn 1");
Button btn2 = new Button("Btn 2");
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 1;
this.add(btn1, gbc);
gbc.gridx = 2;
this.add(btn2, gbc);
//------------ Input Panel ------------------------
this.add(login_panel, BorderLayout.NORTH);
this.add(input_fields_panel, BorderLayout.CENTER);
}
}
Edit: I thought the post would show the number of lines.
Line 44: this.add(btn1, gbc);
Line 47: this.add(btn2, gbc);
Sorry for that!
Any help would appreciated.
java
swing
awt
layout-manager
gridbaglayout
0 Answers
Your Answer