The alignment of Java Layout is messed up - java

Trying to build a simple registration form in Java Swing and AWT, but couldn't accomplish what I really want.
Here is the result that I want
Here's the code
import java.awt.*;
import javax.swing.*;
public class MainFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Main Frame
JFrame mainFrame = new JFrame("New Account Registration");
JPanel borderPanel = new JPanel(new BorderLayout());
JPanel gridPanel = new JPanel(new GridLayout(9,2));
JPanel gridGenderPanel = new JPanel(new GridLayout(1,2));
JPanel flowButton = new JPanel(new FlowLayout());
//JLabels
JLabel title = new JLabel("New Account Registration");
JLabel name = new JLabel("Name");
JLabel email = new JLabel("Email Address:");
JLabel createPassword = new JLabel("Create Password:");
JLabel confirmPassword = new JLabel("Confirm Password:");
JLabel gender = new JLabel("Gender:");
JLabel address = new JLabel("Address:");
JLabel state = new JLabel("State:");
JLabel country = new JLabel("Country:");
JLabel phoneNo = new JLabel("Phone No:");
String[] coutriesStrings = { "America", "Japan", "India", "Korea", "Sweden" };
// JTextFields, JRadioButton, JComboBox
JTextField nameField = new JTextField();
JTextField emailField = new JTextField();
JPasswordField passField = new JPasswordField();
JPasswordField confirmPassField = new JPasswordField();
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup group = new ButtonGroup();
group.add(male);
group.add(female);
JTextField addressField = new JTextField();
JComboBox stateBox = new JComboBox(coutriesStrings);
stateBox.setSelectedIndex(1);
JTextField countryField = new JTextField();
JTextField phoneField = new JTextField();
JButton submitButton = new JButton("Submit");
JButton clearButton = new JButton("Clear");
// borderPanel.add(title, BorderLayout.NORTH);
// gridPanel.add(title);
// Name
gridPanel.add(name);
gridPanel.add(nameField);
//Email
gridPanel.add(email);
gridPanel.add(emailField);
// CreatePassword
gridPanel.add(createPassword);
gridPanel.add(passField);
// Confirm Password
gridPanel.add(confirmPassword);
gridPanel.add(confirmPassField);
// Gender
gridGenderPanel.add(gender);
gridGenderPanel.add(male);
gridGenderPanel.add(female);
gridPanel.add(gridGenderPanel);
// Address
gridPanel.add(address);
gridPanel.add(addressField);
// State
gridPanel.add(state);
gridPanel.add(stateBox);
// Country
gridPanel.add(country);
gridPanel.add(countryField);
//Button
flowButton.add(submitButton);
flowButton.add(clearButton);
gridPanel.add(flowButton);
mainFrame.add(gridPanel);
mainFrame.setSize(600, 700);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here is the result of the codes
I don't know where did I do wrong, please guide me.

You have to add gender label to gridPanel not to gridGenderPanel
// Gender
//gridGenderPanel.add(gender);//The mistake
gridPanel.add(gender);//add to main panel
gridGenderPanel.add(male);
gridGenderPanel.add(female);
gridPanel.add(gridGenderPanel);

Grid layout has a bad habit of making every single part of the grid the same size as the biggest part of the grid. Making UI that require everything to be the same size - this works great. Try out using a GridBagLayout - it isn't pretty but it definitely works.
public class MainFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Main Frame
JFrame mainFrame = new JFrame("New Account Registration");
JPanel borderPanel = new JPanel(new BorderLayout());
JPanel gridPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel gridGenderPanel = new JPanel(new GridLayout(1, 2));
JPanel flowButton = new JPanel(new FlowLayout());
// JLabels
JLabel title = new JLabel("New Account Registration");
setSize(title);
JLabel name = new JLabel("Name");
setSize(name);
JLabel email = new JLabel("Email Address:");
setSize(email);
JLabel createPassword = new JLabel("Create Password:");
setSize(createPassword);
JLabel confirmPassword = new JLabel("Confirm Password:");
setSize(confirmPassword);
JLabel gender = new JLabel("Gender:");
setSize(gender);
JLabel address = new JLabel("Address:");
setSize(address);
JLabel state = new JLabel("State:");
setSize(state);
JLabel country = new JLabel("Country:");
setSize(country);
JLabel phoneNo = new JLabel("Phone No:");
String[] coutriesStrings = { "America", "Japan", "India", "Korea",
"Sweden" };
// JTextFields, JRadioButton, JComboBox
JTextField nameField = new JTextField(15);
JTextField emailField = new JTextField(15);
JPasswordField passField = new JPasswordField(15);
JPasswordField confirmPassField = new JPasswordField(15);
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup group = new ButtonGroup();
group.add(male);
group.add(female);
JTextField addressField = new JTextField(15);
JComboBox stateBox = new JComboBox(coutriesStrings);
stateBox.setPreferredSize(new Dimension(200, 25));
stateBox.setMinimumSize(new Dimension(200, 25));
stateBox.setSelectedIndex(1);
JTextField countryField = new JTextField(15);
JTextField phoneField = new JTextField(15);
JButton submitButton = new JButton("Submit");
JButton clearButton = new JButton("Clear");
// Name
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(name, c);
c.gridx = 2;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(nameField, c);
// Email
c.gridx = 0;
c.gridy = 1;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(email, c);
c.gridx = 2;
c.gridy = 1;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(emailField, c);
// CreatePassword
c.gridx = 0;
c.gridy = 2;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(createPassword, c);
c.gridx = 2;
c.gridy = 2;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(passField, c);
// Confirm Password
c.gridx = 0;
c.gridy = 3;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(confirmPassword, c);
c.gridx = 2;
c.gridy = 3;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(confirmPassField, c);
// Gender
c.gridx = 0;
c.gridy = 4;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(gender,c );
c.gridx = 2;
c.gridy = 4;
c.gridheight = 1;
c.gridwidth = 1;
gridPanel.add(male,c);
c.gridx = 3;
c.gridy = 4;
c.gridheight = 1;
c.gridwidth = 1;
gridPanel.add(female,c);
// Address
c.gridx = 0;
c.gridy = 5;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(address, c);
c.gridx = 2;
c.gridy = 5;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(addressField, c);
// State
c.gridx = 0;
c.gridy = 6;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(state, c);
c.gridx = 2;
c.gridy = 6;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(stateBox, c);
// Country
c.gridx = 0;
c.gridy = 7;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(country, c);
c.gridx = 2;
c.gridy = 7;
c.gridheight = 1;
c.gridwidth = 2;
gridPanel.add(countryField, c);
// Button
flowButton.add(submitButton);
flowButton.add(clearButton);
c.gridx = 1;
c.gridy = 8;
c.gridheight = 1;
c.gridwidth = 4;
gridPanel.add(flowButton, c);
mainFrame.add(gridPanel);
mainFrame.setSize(350, 300);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static void setSize(Component label) {
label.setMinimumSize(new Dimension(120, 15));
label.setPreferredSize(new Dimension(120, 15));
}
}

Related

Cannot add Listeners

I'm trying to put some Listeners in my code but I can't. For example I want to add a listener and when I write somenthing in the textfield area , then I choose the JRadioButton "From TextField" and after that I push the button "Do It" , I want to see the text that I wrote(in TextField) on JTextArea. How is that possible?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javafx.scene.Group;
public class Layout {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
GridBagConstraints c1 = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;}
JTextField text = new JTextField( "Some Text");
if (shouldWeightX) {
c.weightx = 0.5;}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.ipady = 50;
pane.add(text, c);
String names[] = { "Ferrari", "Koenigsegg", "Alfa Romeo" };
JComboBox cb = new JComboBox( names );
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.ipady = 50;
pane.add(cb, c);
Box box=Box.createVerticalBox();
JCheckBox checkbox = new JCheckBox("Water");
JCheckBox checkbox2 = new JCheckBox("Fire");
JCheckBox checkbox3 = new JCheckBox("Earth");
c1.fill = GridBagConstraints.HORIZONTAL;
c1.weightx = 0.5;
c1.ipady = 5;
box.add(checkbox);
box.add(checkbox2);
box.add(checkbox3);
pane.add(box,c1);
JPanel container = new JPanel();
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
container.setLayout(new GridLayout(2, 1));
JSlider slider = new JSlider(JSlider.HORIZONTAL,0,100,50);
slider.setPreferredSize(new Dimension(150,20));
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(slider, c);
JTextField text1 = new JTextField(" ");
c.ipady = 80;
c.weighty = 2.0;
c.anchor = GridBagConstraints.BASELINE;
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 2;
pane.add(text1, c);
JRadioButton b1= new JRadioButton("From JTextField");
JRadioButton b2= new JRadioButton("From JComboBox");
JRadioButton b3= new JRadioButton("From JCheckboxes");
JRadioButton b4= new JRadioButton("J Slider");
b1.setForeground(Color.BLUE);
b2.setForeground(Color.RED);
b3.setBackground(Color.GREEN);
Box box1 = Box.createHorizontalBox();
box1.add(b1);
box1.add(b2);
box1.add(b3);
box1.add(b4);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weighty = 1.0;
c.anchor = GridBagConstraints.BASELINE;
c.gridx = 0;
c.gridwidth = 3;
c.gridy =2;
pane.add(box1,c);
button = new JButton("Do it");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady =0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10,0,0,0);
c.gridx = 0;
c.gridwidth =3;
c.gridy = 2;
pane.add(button, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame(" ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
frame.setSize(600,400);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Read the section from the Swing tutorial on How to Use Text Areas. The first example shows how to append text from a JTextField to a JTextArea. This example uses an ActionListener for the text field.
then I choose the JRadioButton "From TextField" and after that I push the button "Do It" , I want to see the text that I wrote(in TextField) on JTextArea.
The concept will be similar except you add the ActionListener to you button. Then when it is invoked you check if the "From TextField" radio button is selected. If so, then you get the text from the text field and append it to the text area.

JScrollPane not showing on JTable

I am trying to get a JScrollPane to appear on my JTable. I passed the table to the scrollpane when i created an instance of the component. But to no avail it has yet to show on my table.
table = new JTable();
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200,100));
I dont know how i can fix this issue, i cant seem to find an issue that would cause it to fail. Here is the rest of the GUI code. It is very long. Adding the jtable to a jpanel starts at line 152.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javasql;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.table.DefaultTableModel;
/**
*
* #author KJ4CC
*/
public class UserInterface implements ActionListener {
DefaultTableModel dtm = new DefaultTableModel(0, 0);
public UserInterface() {
startGui();
}
JFrame frame = new JFrame();
Javasql sql = new Javasql();
JPanel buttom = new JPanel(new GridBagLayout());
JPanel commandPane = new JPanel(new GridBagLayout());
JPanel top = new JPanel(new GridBagLayout());
JPanel buttons = new JPanel(new GridBagLayout());
JPanel label = new JPanel(new GridBagLayout());
JButton connect = new JButton("Connect To Database");
JButton clr = new JButton("Clear Command");
JButton exeSql = new JButton("Execute SQL Command");
JButton clrRes = new JButton("Clear Result Window");
JLabel infoLabel = new JLabel("Enter Database Information ");
JLabel driverLabel = new JLabel("JDBC Driver: ");
JLabel dbLabel = new JLabel("Database URL: ");
JLabel userLabel = new JLabel("Username: ");
JLabel passLabel = new JLabel("Password: ");
JLabel sqlLabel = new JLabel("Enter SQL Command: ");
JLabel connectionLabel = new JLabel("No Connection Now ");
JLabel exeLabel = new JLabel("SQL Execution Result: ");
//creating an instance of the new table
public JTable table;
public JScrollPane scrollPane;
JComboBox driverSelect = new JComboBox();
JComboBox url = new JComboBox();
JTextField username = new JTextField();
JTextField pass = new JTextField();
JTextArea command = new JTextArea(1, 1);
public void startGui() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
System.out.println("sdf");
c.insets = new Insets(0, 0, 0, 10);
c.fill = 0;
c.weightx = 1;
//adding all of the compoenets to their panel and then to the frame.
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(infoLabel, c);
c.gridx = 0;
c.gridy = 1;
top.add(driverLabel, c);
c.gridx = 1;
c.gridy = 1;
c.ipadx = 150;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(driverSelect, c);
c.gridx = 0;
c.gridy = 2;
c.fill = 0;
top.add(dbLabel, c);
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(url, c);
c.gridx = 0;
c.gridy = 3;
c.fill = 0;
c.fill = 0;
top.add(userLabel, c);
c.gridx = 1;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(username, c);
c.gridx = 0;
c.gridy = 4;
c.fill = 0;
c.fill = 0;
top.add(passLabel, c);
c.gridx = 1;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(pass, c);
//add the driver and url to the comboboxes
c.gridx = 2;
c.gridy = 0;
commandPane.add(sqlLabel, c);
c.gridx = 2;
c.gridy = 1;
c.ipadx = 150;
c.ipady = 75; //sql text area for command
c.fill = GridBagConstraints.FIRST_LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
commandPane.add(command, c);
c.insets = new Insets(0, 0, 0, 20);
c.ipadx = 9;
c.ipady = 1;
c.gridx = 0;
c.gridy = 0;
//buttons
label.add(connectionLabel, c);
c.gridx = 1;
c.gridy = 0;
//c.insets = new Insets(0, 0, 0, 50);
buttons.add(connect, c);
connect.addActionListener(this);
c.gridx = 2;
buttons.add(clr, c);
clr.addActionListener(this);
c.gridx = 3;
buttons.add(exeSql, c);
exeSql.addActionListener(this);
//adding the label and buttons above and below the tabel.
c.gridx = 0;
c.gridy = 1;
buttom.add(exeLabel, c);
c.gridx = 0;
c.gridy = 2;
//-----------------------------------------------------------------Table here
table = new JTable();
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c);
buttom.add(scrollPane);
c.gridx = 0;
c.gridy = 3;
buttom.add(clrRes, c);
c.weightx = 2;
c.weighty = 2;
c.gridx = 0;
c.gridy = 0;
frame.setLayout(new GridLayout(3, 2));
frame.add(top);
c.gridx = 2;
c.gridy = 1;
frame.add(commandPane);
frame.add(label);
frame.add(buttons);
frame.add(buttom, BorderLayout.SOUTH);
//adding the content panel to the jframe.
frame.pack();
frame.setSize(1000, 550);
frame.setVisible(true);
//adding items to both of the combo boxes.
driverSelect.addItem("com.mysql.jdbc.Driver");
url.addItem("jdbc:mysql://localhost:3306/project3");
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == connect) {
sql.connect(this);
} else if (e.getSource() == clr) {
command.setText("");
} else if (e.getSource() == exeSql) {
sql.exeCommand(this);
}
}
}
You can not add the table twice :
table = new JTable();
scrollPane = new JScrollPane(table); //here
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c); //here
buttom.add(scrollPane);
If you add it to the scrollPane, just add the scrollpane. A Component can't have two parents.
I did not check your complete code but try
buttom.add(scrollPane,c);
instead of this
buttom.add(table, c); //here
buttom.add(scrollPane);
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c);
buttom.add(scrollPane);
here you add the table twice, directly in the first line and (implicitly) along with the ScrollPane at the last line.
In Swing this is not possible. Therefore the JTable is removed from the Scrollpane at the first line, when you add it directly to the bottom panel, and in turn at the last line an empty scrollpane is added, removing the JTable added earlier.
just remove the first line.

Issues with jlabel/button formatting

The category labels 1, 2, 3, 4 and 5 don't seem to align with row I'm assigning them to. I don't understand where the problem is; the x value is equal. If someone could lead me in the right direction, that would be really great.
Here's my code
public class GovernmentJepordyGame {
public static void main(String[] args)
{
JFrame frame = new JFrame("Jepordy");
frame.setVisible(true);
frame.setSize(1300,900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.getContentPane().add(panel, BorderLayout.PAGE_START);
/*JLabel label = new JLabel("Welcome to government jepordy.");*/
JLabel catagoryOne = new JLabel("Catagory 1");
c.gridx = 0;
c.gridy = 0;
panel.add(catagoryOne, c);
JButton button500A = new JButton("500");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500A, c);
JButton button400A = new JButton("400");
c.gridx = 0;
c.gridy = 2;
panel.add(button400A, c);
JButton button300A = new JButton("300");
c.gridx = 0;
c.gridy = 3;
panel.add(button300A, c);
JButton button200A = new JButton("200");
c.gridx = 0;
c.gridy = 4;
panel.add(button200A, c);
JButton button100A = new JButton("100");
c.gridx = 0;
c.gridy = 5;
panel.add(button100A, c);
JLabel catagoryTwo = new JLabel("Catagory 2");
c.gridx = -1;
c.gridy = 0;
panel.add(catagoryTwo, c);
JButton button500B = new JButton("500");
c.gridx = -1;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500B, c);
JButton button400B = new JButton("400");
c.gridx = -1;
c.gridy = 2;
panel.add(button400B, c);
JButton button300B = new JButton("300");
c.gridx = -1;
c.gridy = 3;
panel.add(button300B, c);
JButton button200B = new JButton("200");
c.gridx = -1;
c.gridy = 4;
panel.add(button200B, c);
JButton button100B = new JButton("100");
c.gridx = -1;
c.gridy = 5;
panel.add(button100B, c);
JLabel catagoryThree = new JLabel("Catagory 3");
c.gridx = -2;
c.gridy = 0;
panel.add(catagoryThree);
JButton button500C = new JButton("500");
c.gridx = -2;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500C, c);
JButton button400C = new JButton("400");
c.gridx = -2;
c.gridy = 2;
panel.add(button400C, c);
JButton button300C = new JButton("300");
c.gridx = -2;
c.gridy = 3;
panel.add(button300C, c);
JButton button200C = new JButton("200");
c.gridx = -2;
c.gridy = 4;
panel.add(button200C, c);
JButton button100C = new JButton("100");
c.gridx = -2;
c.gridy = 5;
panel.add(button100C, c);
JLabel catagoryFour = new JLabel("Catagory 4");
c.gridx = -3;
c.gridy = 0;
panel.add(catagoryFour);
JButton button500D = new JButton("500");
c.gridx = -3;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500D, c);
JButton button400D = new JButton("400");
c.gridx = -3;
c.gridy = 2;
panel.add(button400D, c);
JButton button300D = new JButton("300");
c.gridx = -3;
c.gridy = 3;
panel.add(button300D, c);
JButton button200D = new JButton("200");
c.gridx = -3;
c.gridy = 4;
panel.add(button200D, c);
JButton button100D = new JButton("100");
c.gridx = -3;
c.gridy = 5;
panel.add(button100D, c);
JLabel catagoryFive = new JLabel("Catagory 5");
c.gridx = -4;
c.gridy = 0;
panel.add(catagoryFive);
JButton button500E = new JButton("500");
c.gridx = -4;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500E, c);
JButton button400E = new JButton("400");
c.gridx = -4;
c.gridy = 2;
panel.add(button400E, c);
JButton button300E = new JButton("300");
c.gridx = -4;
c.gridy = 3;
panel.add(button300E, c);
JButton button200E = new JButton("200");
c.gridx = -4;
c.gridy = 4;
panel.add(button200E, c);
JButton button100E = new JButton("100");
c.gridx = -4;
c.gridy = 5;
panel.add(button100E, c);
}
}
The gridx should be a positive value, something like...
JFrame frame = new JFrame("Jepordy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.getContentPane().add(panel, BorderLayout.PAGE_START);
c.gridx = 0;
for (int index = 0; index < 5; index++) {
c.gridy = 0;
JLabel label = new JLabel("Catagory " + index);
c.insets = new Insets(0, 0, 0, 0);
panel.add(label, c);
c.insets = new Insets(65, 65, 65, 65);
for (int cat = 1; cat < 6; cat++) {
c.gridy++;
panel.add(new JButton(Integer.toString(cat * 100)), c);
}
c.gridx++;
}
frame.setVisible(true);
frame.setSize(1300, 900);
for instance...
You are getting x and y mixed up. The top left corner of your screen is 0,0. As you move to the right, x increases, as you move down, y increases. So assigning a (-) value to your x coordinate system is not necessary, it will move likely start moving your buttons off screen to the left. Every button, try increasing the x grid value, instead of the y, and you should get your desired results.
gridx = 10....gridx = 20.. and so on.
Even better, implement some sort of loop to iterate through each button and increase the x values upon each iterate.
Or, look into flowlayout managers, they handle this automatically.

GridBagLayout JLabels & JTextAreas organization within JPanel

I'm trying to make 4 columns within a panel.
JLabel(Title)
JLabel JLabel
JLabel JTextArea JLabel JTextArea
...
...
...
JButton
Its pretty much a panel where you enter data. The labels will be something like "speed" then you type in a number in the text area next to it. I'm having a problem with the gridbaglayout though. The title is pretty big so it looks something like this. Using GridBagLayout , The title is at (1,0) but seeing as its so big, when I put the JLabel1 (0,1) and JLabel2 (2,0), they are way too spaced out since the title seems to have taken a pretty big chunk.
JLabel(Title..............)
JLabel1 JLabel2
...
...
...
JButton
I want it to be more like
JLabel(Title..........................................)
JLabel JLabel
JLabel JTextArea JLabel JTextArea
...
...
...
JButton
The code if you'd like to run it:
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Example {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
new Example();
}
});
}
JFrame go = new JFrame("Example");
JPanel panel = new JPanel();
JButton Button = new JButton("Button");
GridBagLayout Grid = new GridBagLayout();
JLabel Title = new JLabel("LARGEE TITLEE");
JLabel Label1 = new JLabel("Label 1");
JLabel Label2 = new JLabel("Label 2");
public Example() {
panel.setLayout(Grid);
GridBagConstraints c = new GridBagConstraints();
Title.setFont(new Font("Serif", Font.BOLD, 60));
c.insets = new Insets(10,10,10,10);
c.gridy = 0; c.gridx = 1;
panel.add(Title, c);
c.gridy = 1; c.gridx = 0;
panel.add(Label1 , c);
c.gridx = 2;
panel.add(Label2, c);
c.gridy = 2; c.gridx = 1;
panel.add(Button, c);
go.add(panel);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(750, 750);
go.setVisible(true);
}
}
Divide the screen into parts and specify the width of every component (gridwidth), as well as the gridx and gridy, so that they will be placed accordingly.
The output of the sample I wrote looks like this:
Code:
public class Example extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
JFrame go = new JFrame("Example");
JPanel panel = new JPanel();
GridBagLayout Grid = new GridBagLayout();
JLabel Title = new JLabel("LARGE TITLE", SwingConstants.CENTER);
JLabel Label1 = new JLabel("Label 1", SwingConstants.CENTER);
JLabel Label2 = new JLabel("Label 2", SwingConstants.CENTER);
JLabel anotherLabel1 = new JLabel("Another Label 1", SwingConstants.CENTER);
JLabel anotherLabel2 = new JLabel("Another Label 2", SwingConstants.CENTER);
JTextArea textArea1 = new JTextArea("TextArea 1");
JTextArea textArea2 = new JTextArea("TextArea 2");
public Example() {
panel.setLayout(Grid);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
Title.setFont(new Font("Serif", Font.BOLD, 60));
JButton button;
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //increase height of the title
c.weightx = 0.5;
c.gridwidth = 4;
c.gridx = 0;
c.gridy = 0;
panel.add(Title, c);
c.ipady = 0;
c.gridwidth = 2;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
panel.add(Label1, c);
c.weightx = 0.5;
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 1;
panel.add(Label2, c);
c.ipady = 0;
c.gridwidth = 1;
c.weightx = 0.25;
c.gridx = 0;
c.gridy = 2;
panel.add(anotherLabel1, c);
c.weightx = 0.25;
c.gridx = 1;
c.gridy = 2;
panel.add(textArea1, c);
c.weightx = 0.25;
c.gridx = 2;
c.gridy = 2;
panel.add(anotherLabel2, c);
c.weightx = 0.25;
c.gridx = 3;
c.gridy = 2;
panel.add(textArea2, c);
button = new JButton("JButton");
c.ipady = 0;
c.weighty = 1.0;
c.insets = new Insets(10, 0, 0, 0);
c.gridx = 1;
c.gridwidth = 2;
c.gridy = 3;
panel.add(button, c);
go.add(panel);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.pack();
go.setSize(750, 300);
go.setVisible(true);
}
}
Related Documentation:
How to Use GridBagLayout
If I get it right, what you need is to set the title label full width, try this:
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 4; //where 4 is the numbers of columns
c.gridx=0; // set position at (0,0) because now is full width
panel.add(title, c);

constraint must be a string (or null)

I can not find the error to save my life. The error is "constraint must be a string (or null)" I dont know why it is giving me this error, I have to be missing something simple.
I tried adding
for example: dataPane = new JPanel(new GridBagLayout()); to all my panels and nothing.
I am trying to add a panel (2) to the extended panel.
here is my code:
public class SearchFlight extends JPanel {
//giving names to the components\\\
private JRadioButton oneWay;
private JRadioButton roundTrip;
private ButtonGroup buttonGroup;
private JLabel fromDestdLabel;
private JComboBox fromDestCb;
private JLabel toDestLbl;
private JComboBox toDestCb;
private JLabel departLbl;
private JComboBox departMonth;
private JComboBox departDay;
private JTextField departYear;
private JLabel arriveLbl;
private JComboBox arriveMonth;
private JComboBox arriveDay;
private JTextField arriveYear;
private JLabel adultLbl;
private JComboBox adultCb;
private JLabel childLbl;
private JComboBox childCb;
private JLabel infantLbl;
private JComboBox infantCb;
private JButton searchBtn;
private JButton canxBtn;
private JPanel buttonPane;
private JPanel dataPane;
public SearchFlight(){
initcomp();
}
public void initcomp(){
//initilizing all the componets\\
oneWay = new JRadioButton("One Way");
roundTrip = new JRadioButton("Round Trip");
buttonGroup = new ButtonGroup();
fromDestdLabel = new JLabel("From");
fromDestCb = new JComboBox();
toDestLbl = new JLabel("To");
toDestCb = new JComboBox();
departLbl = new JLabel("Depart");
departMonth = new JComboBox();
departDay = new JComboBox();
departYear = new JTextField();
arriveLbl = new JLabel("Arrive");
arriveMonth = new JComboBox();
arriveDay = new JComboBox();
arriveYear = new JTextField();
adultLbl = new JLabel("Adult");
adultCb = new JComboBox();
childLbl = new JLabel("Child");
childCb = new JComboBox();
infantLbl = new JLabel("infant");
infantCb = new JComboBox();
searchBtn = new JButton("Search");
canxBtn = new JButton("Cancel");
buttonPane = new JPanel();
dataPane = new JPanel(new GridBagLayout());
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
BorderLayout borderLayout = new BorderLayout();
c.fill = GridBagConstraints.HORIZONTAL;
dataPane.setLayout(borderLayout);
c.gridx = 1;
c.gridy = 0;
dataPane.add(oneWay, c);
c.gridx = 2;
c.gridy = 0;
dataPane.add(roundTrip, c);
c.gridx = 0;
c.gridy = 1;
dataPane.add(fromDestdLabel ,c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
dataPane.add(fromDestCb, c);
c.gridx = 0;
c.gridy = 2;
dataPane.add(toDestLbl,c);
c.gridx = 1;
c.gridy = 2;
dataPane.add(toDestCb, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 3;
dataPane.add(departLbl ,c);
c.gridx = 1;
c.gridy = 3;
dataPane.add(departMonth,c);
c.gridx = 2;
c.gridy = 3;
dataPane.add(departDay, c);
c.gridx = 3;
c.gridy = 3;
dataPane.add(departYear, c);
c.gridx = 0;
c.gridy = 4;
dataPane.add(arriveLbl, c);
c.gridx = 1;
c.gridy = 4;
dataPane.add(arriveMonth, c);
c.gridx = 2;
c.gridy = 4;
dataPane.add(arriveDay,c);
c.gridx = 3;
c.gridy = 4;
dataPane.add(arriveYear,c);
c.gridx = 0;
c.gridy = 5;
dataPane.add(adultLbl,c);
c.gridx = 1;
c.gridy = 5;
dataPane.add(adultCb,c);
c.gridx = 0;
c.gridy = 6;
dataPane.add(childLbl,c);
c.gridx = 1;
c.gridy = 6;
dataPane.add(childCb,c);
c.gridx = 0;
c.gridy = 7;
dataPane.add(infantLbl,c);
c.gridx = 1;
c.gridy = 7;
dataPane.add(infantCb,c);
buttonPane.add(searchBtn);
buttonPane.add(canxBtn);
add(buttonPane, BorderLayout.SOUTH);
add(dataPane);
}
}
You are giving dataPane a BorderLayout, but then trying to use GridBagConstraints when adding components to it --- not allowed, and even if allowed, just doesn't make sense.
Instead you have one of two options:
Keep the container's layout as BorderLayout but use BorderLayout constants such as BorderLayout.EAST when adding components to this container, or
Change the dataPane's layout manager to GridBagLayout, and then sure, go ahead and continue using GridBagConstraints when adding components.
Edit
You state in comment:
so I use dataPane = new JPanel(new GridBagLayout()); then add it by add(dataPane);
Yes, it is fine to use a GridBagLayout, but I'm not sure what you mean by your second point, the one re add(dataPane) as that appears unrelated to your original problem.

Categories