In the following code, when I add the JTextArea to the main panel, it doesn't show up. When I add the controlPanel, it shows up in the center, not the edge. I'm new with GridBagLayout, so I'm assuming I'm missing something simple.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
JPanel controlPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
controlPanel.add(new JButton("Play"));
controlPanel.add(new JButton("Pause"));
controlPanel.add(new JSpinner());
JTextArea textArea = new JTextArea();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
c.gridwidth = 3;
mainPanel.add(textArea, c);
// mainPanel.add(controlPanel, c);
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 700);
frame.setLocation(250, 100);
frame.setVisible(true);
}
}
EDIT: This is how the constraints look after your suggestions. The textArea still does not show up.
c.gridx = 0;
c.gridy = 0;
// c.gridheight = 3;
// c.gridwidth = 3;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.NORTHWEST;
mainPanel.add(textArea, c);
// mainPanel.add(controlPanel, c);
frame.add(mainPanel);
Don't forget weights and anchors:
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.NORTHWEST;
Edit:
Example of adding row and column values to the JTextArea:
controlPanel.add(new JButton("Play"));
controlPanel.add(new JButton("Pause"));
controlPanel.add(new JSpinner());
JTextArea textArea = new JTextArea(20, 40);
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
c.gridwidth = 3;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.NORTHWEST;
mainPanel.add(new JScrollPane(textArea), c);
My totally serious answer would be to not use GridBagLayout. Leave GridBagLayout for the GUI form builders.
If you want to build GUI's by hand (I recommend building them by hand and avoiding form builders, BTW) then generally BorderLayout with nested panels using BoxLayouts are really your best bet.
Related
I'm trying to create some UI but no idea how to use LayoutManger for it.
Like showing in the image Purple color component have fixed width and height but stick to the corners. 8 and 4 have fixed with and variable height, 2 and 6 have fixed height and variable width. the middle component (9) need variable width and height.
These 8 component work like a border and middle one is need to resize according to parent component size. I could do this coding absolute positions using null layout. but I have suggested to not to use null layout.
How could I do this with a Layout manager witch layout I can use for this? Do I need to use more layouts than one?
UPDATE
i have tried something with GridBagLayout as Andrew's suggestion but I still need a little help to understand how it works. Here is my code
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridLayoutTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button;
GridBagConstraints c = new GridBagConstraints();
button = new JButton("1");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.gridx = 0;
c.gridy = 0;
jPanel1.add(button, c);
button = new JButton("2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridx = 1;
c.gridy = 0;
jPanel1.add(button, c);
button = new JButton("3");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.FIRST_LINE_END;
c.gridx = 2;
c.gridy = 0;
jPanel1.add(button, c);
button = new JButton("4");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_START;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 1;
jPanel1.add(button, c);
//Panel
JPanel panel = new JPanel();
panel.setBackground(Color.green.darker());
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.0;
//c.weightx = 1.0;
c.gridx = 1;
c.gridy = 1;
jPanel1.add(panel, c);
button = new JButton("5");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.weighty = 1.0;
c.gridx = 2;
c.gridy = 1;
jPanel1.add(button, c);
button = new JButton("6");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LAST_LINE_START;
c.gridx = 0;
c.gridy = 2;
jPanel1.add(button, c);
button = new JButton("7");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridx = 1;
c.gridy = 2;
jPanel1.add(button, c);
button = new JButton("8");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.gridx = 2;
c.gridy = 2;
jPanel1.add(button, c);
frame.add(jPanel1);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
I created the following GUI.
I set all six of the GridBagConstraints (anchor, fill, gridx, gridy, weightx, weighty) for each element of the grid. That way, I could more easily keep track of what the values were for each element.
Here's the complete runnable code I used. In other words a minimal, reproducible example.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GridBagLayoutGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("GridBagLayout GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setPreferredSize(new Dimension(300, 300));
JButton button;
GridBagConstraints c = new GridBagConstraints();
button = new JButton("1");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 0;
c.gridy = 0;
panel.add(button, c);
button = new JButton("2");
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 1.0d;
c.weighty = 0.0d;
c.gridx = 1;
c.gridy = 0;
panel.add(button, c);
button = new JButton("3");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.FIRST_LINE_END;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 2;
c.gridy = 0;
panel.add(button, c);
button = new JButton("4");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0.0d;
c.weighty = 1.0d;
c.gridx = 0;
c.gridy = 1;
panel.add(button, c);
// Panel
JPanel innerPanel = new JPanel();
innerPanel.setBackground(Color.green.darker());
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 1.0d;
c.weighty = 1.0d;
c.gridx = 1;
c.gridy = 1;
panel.add(innerPanel, c);
button = new JButton("5");
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.weightx = 0.0d;
c.weighty = 1.0d;
c.gridx = 2;
c.gridy = 1;
panel.add(button, c);
button = new JButton("6");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_START;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 0;
c.gridy = 2;
panel.add(button, c);
button = new JButton("7");
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 1.0d;
c.weighty = 0.0d;
c.gridx = 1;
c.gridy = 2;
panel.add(button, c);
button = new JButton("8");
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.weightx = 0.0d;
c.weighty = 0.0d;
c.gridx = 2;
c.gridy = 2;
panel.add(button, c);
return panel;
}
}
Another option would be JGoodies FormLayout. I have been using this in combination with JFormDesigner for all my layouting needs since basically forever. It covers 95% percent of my use cases. The remaining 5% is BorderLayout and absolute positioning (null layout).
A common strategy to solve complex computing tasks, is to break them into small, well defined manageable tasks. Divide and conquer.
This also applies to gui: you can break the design into small, easy to layout containers.
In this case the needed layout can be achieved by using BoxLayout and BorderLayout:
import java.awt.*;
import javax.swing.Box.Filler;
import javax.swing.*;
public class FramedContent extends JPanel{
private static final int FRAME_WIDTH = 35, CONTENT_SIZE = 300, BORDER_SIZE = 1;
private static final Color CORNER_COLOR = Color.MAGENTA, RULLER_COLOR = Color.BLUE,
CONTENT_COLOR = Color.GREEN, BORDER_COLOR = Color.WHITE;
FramedContent(){
setLayout(new BorderLayout());
add(header(), BorderLayout.NORTH);
add(center(), BorderLayout.CENTER);
add(header(), BorderLayout.SOUTH);
}
private JPanel header(){
JPanel hExpandPane = new JPanel();
hExpandPane.setLayout(new BoxLayout(hExpandPane, BoxLayout.X_AXIS));
hExpandPane.add(corner());
hExpandPane.add(hRuller());
hExpandPane.add(corner());
return hExpandPane;
}
private JComponent corner(){
Dimension d = new Dimension(FRAME_WIDTH, FRAME_WIDTH);
Filler filler = new Filler(d, d, d);
filler.setBackground(CORNER_COLOR);
filler.setBorder(BorderFactory.createLineBorder(BORDER_COLOR, BORDER_SIZE));
filler.setOpaque(true);
return filler;
}
private JComponent hRuller(){
Dimension d = new Dimension(0,FRAME_WIDTH);
Filler filler = new Filler(d, d, new Dimension(Short.MAX_VALUE, FRAME_WIDTH));
filler.setBackground(RULLER_COLOR);;
filler.setOpaque(true);
return filler;
}
private JPanel center(){
JPanel vExpandPane = new JPanel();
vExpandPane.setLayout(new BorderLayout());
vExpandPane.add(vRuller(), BorderLayout.WEST);
vExpandPane.add(content(), BorderLayout.CENTER);
vExpandPane.add(vRuller(), BorderLayout.EAST);
return vExpandPane;
}
private JComponent vRuller(){
Dimension d = new Dimension(FRAME_WIDTH, 0);
Filler filler = new Filler(d, d, new Dimension(FRAME_WIDTH, Short.MAX_VALUE));
filler.setBackground(RULLER_COLOR);
filler.setOpaque(true);
return filler;
}
private JPanel content(){
JPanel content = new JPanel(new GridBagLayout());// GridBagLayout used just to center the content
content.setBackground(CONTENT_COLOR);
content.add(new JLabel("Your Content Goes Here"));
content.setPreferredSize(new Dimension(CONTENT_SIZE,CONTENT_SIZE));
return content;
}
public static void createWindow() {
JFrame frame = new JFrame("Framed content");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(new FramedContent());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createWindow();
}
}
I have JFrame form which must contain TextField somewhere around center of the form, I am using GridBagLayout to set TextField position on the frame.
The code like this below:
public static JFrame enterFrameDraw(JFrame frame){
JButton btnEnter = new JButton("Sign in!");
JLabel loginLabel = new JLabel("Login!");
GridBagLayout gbl = new GridBagLayout();
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
btnEnter.addActionListener(e -> {
try{
Chat.btnEnterHandler(frame);
} catch (Exception e2){
e2.printStackTrace();
}
});
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(FRM_TITLE);
frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
frame.setSize(FRM_WIDTH, FRM_HEIGHT);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.NONE;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(40, 0, 0, 0);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.0;
c.weighty = 0.0;
gbl.setConstraints(textFieldLogin, c);
frame.add(textFieldLogin, c);
frame.setResizable(false);
frame.setVisible(true);
return frame;
}
And result seems like:
Image
Could the problem lies in the "frame.add()" instead of simple "add()"?
UPDATE:
Method enterFrameDraw() after modification. Still doesn't work.
public static JFrame enterFrameDraw(JFrame frame){
JButton btnEnter = new JButton("Sign in!");
JLabel loginLabel = new JLabel("Login!");
GridBagLayout gbl = new GridBagLayout();
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
btnEnter.addActionListener(e -> {
try{
Chat.btnEnterHandler(frame);
} catch (Exception e2){
e2.printStackTrace();
}
});
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(FRM_TITLE);
frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
frame.setSize(FRM_WIDTH, FRM_HEIGHT);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(0, 40, 0, 40);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.5;
c.weighty = 0.0;
gbl.setConstraints(textFieldLogin, c);
frame.add(textFieldLogin);
frame.setResizable(false);
frame.setVisible(true);
return frame;
}
UPDATE 2:
What i want to have on the form.
Image: This one
As far as I can tell there are two problems.
You need to set your constraints' fill to horizontal:
c.fill = GridBagConstraints.HORIZONTAL;
You need to set the constraints' weightx to a non-zero value:
c.weightx = 0.5;
Edit 1:
There's 2 ways you can centralize the content:
Define the constraints' insets to push it in from the left and right, like so:
c.insets = new Insets(0, 40, 0, 40);
The value 40 here is just an example. You could change the parameters to whatever suits.
You can use javax.swing.Box to create empty grid cells. To keep your content in the center, you can create an empty 'strut' on either side of the middle components:
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
frame.add(Box.createHorizontalStrut(100), c);
c.gridx = 1;
frame.add(textFieldLogin, c);
c.gridx = 2;
frame.add(Box.createHorizontalStrut(100), c);
Again, the value of 100 here is just an example, you should tweak the parameters to your needs.
It would be worthwhile reading up on things like Struts and Glues in the context of gridbaglayouts, they're really useful in organizing your content the way you want it.
Edit 2:
To add more components below the text field that's in the center, you should use the second approach of adding horizontal struts to the cells in the first row.
The idea is that putting a strut in a cell fills up space that would be used by a component, if there was a component in that cell. So by specifying the width of a horizontal strut in the first row, any following components with the same x value as the strut will share the same width as the strut.
So in this case, your method might look something like this:
public static JFrame enterFrameDraw(JFrame frame){
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
frame.add(Box.createHorizontalStrut(80), c);
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
c.gridx = 1;
c.gridy = 0;
frame.add(textFieldLogin, c);
c.gridx = 2;
c.gridy = 0;
frame.add(Box.createHorizontalStrut(80), c);
JLabel label = new JLabel("Label");
c.gridx = 0;
c.gridy = 1;
frame.add(label, c);
frame.setVisible(true);
return frame;
}
Here a label is added to the second row, and it does not appear in the center.
I want to create a layout like this
This was my code (does not work):
outer.setLayout(new BorderLayout());
panel1 = new JPanel();
...
outer.add(panel1, BorderLayout.PAGE_START);
outer.add(panel2, BorderLayout.LINE_START);
outer.add(panel3, BorderLayout.CENTER);
outer.add(panel4, BorderLayout.LINE_END);
outer.add(panel5, BorderLayout.PAGE_END);
note: panel5 above should contain 2 more panels inside it
In the above code, I can get them on the correct places but the center one (panel3) is very big so that all others are squashed to the side.
How can i get some ratio of size in these eg 2:10:2 etc?
Should i change my layout?
If you want to do something like that, using BorderLayout is a good start. So yes I would use BorderLayout as well here.
However you should change they way you are adding the panels:
outer.add(panel1, BorderLayout.NORTH);
outer.add(panel2, BorderLayout.WEST);
outer.add(panel3, BorderLayout.CENTER);
outer.add(panel4, BorderLayout.EAST);
//Create a additional Panel for the two at the bottom
JPanel southPanelContainer = new JPanel(new BorderLayout());
southPanelContainer.add(panel5, BorderLayout.EAST);
southPanelContainer.add(panel6, BorderLayout.WEST);
outer.add(southPanelContainer, BorderLayout.SOUTH);
This should already look somewhat decent, however if you still want to change the way it looks then you should add some components to those panels. The layout manager will automatically resize the panels so everything fits.
Use a GridBagLayout for a table/matrix like layout, where some "cells" occupy more than one slot.
BorderLayout is for one central panel having some bordering panels around.
public MainFrame() {
JPanel outer = new JPanel(new GridBagLayout());
outer.setPreferredSize(new Dimension(800, 600));
JPanel panel1 = createPanel("1");
JPanel panel2 = createPanel("2");
JPanel panel3 = createPanel("3");
JPanel panel4 = createPanel("4");
JPanel panel5 = createPanel("5");
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3; // col span
c.gridheight = 1;
outer.add(panel1, c);
c.weightx = 0.33;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
outer.add(panel2, c);
c.weightx = 0.33;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
outer.add(panel2, c);
c.weightx = 0.33;
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
outer.add(panel3, c);
c.weightx = 0.33;
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 3;
c.gridheight = 1;
outer.add(panel4, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.gridheight = 1;
outer.add(panel5, c);
setContentPane(outer);
pack();
}
private JPanel createPanel(String title) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(title));
setPreferredSize(new Dimension(300, 300));
return panel;
}
There is a GridBagConstraints constructor setting all fields. Not so readable here however.
It is also a quite error prone layout.
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.
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);