I'm tired with Java GridBagLayout. I want to create a panel that looks like the one shown in the picture below, but I couldn't get it. I'm unable to position the left panel, and I failed to make panels long. The width does not increase when I set gridWidth. I can't use any GUI builders for that. I want to get a layout like the picture below.
This is the unsuccessful code:
public class gui3 extends Frame{
Panel p1,p2,p3,p4,p5,p6,p7,pmain;
gui3(){
setVisible(true);
setSize(500,500);
setTitle(" Calculator ");
GridBagLayout gb1=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
setLayout(gb1);
p1=new Panel();
p1.setBackground(Color.BLACK);
gbc.gridx=5;
gbc.gridy=0;
gbc.gridwidth=3;
//gbc.weightx =0.5;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p1,gbc);
p2=new Panel();
p2.setBackground(Color.BLUE);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p2,gbc);
p3=new Panel();
p3.setBackground(Color.GREEN);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p3,gbc);
p4=new Panel();
p4.setBackground(Color.cyan);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p4,gbc);
p5=new Panel();
p5.setBackground(Color.RED);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p5,gbc);
p6=new Panel();
p6.setBackground(Color.pink);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridwidth=2;
// gbc.weightx = 1;
// gbc.fill=GridBagConstraints.HORIZONTAL;
add(p6,gbc);
p7=new Panel();
p7.setBackground(Color.yellow);
gbc.gridx=6;
gbc.gridy=0;
gbc.gridheight=6;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p7,gbc);
}
}
You mean something like...
So basically, you can use GridBagConstraints#gridheight (or gridwidth) to set the number of grid cells that a component will span across
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout {
public static void main(String[] args) {
new TestLayout();
}
public TestLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.weightx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(createPane(Color.RED), gbc);
gbc.gridy++;
frame.add(createPane(Color.GREEN), gbc);
gbc.gridy++;
frame.add(createPane(Color.BLUE), gbc);
gbc.gridy++;
frame.add(createPane(Color.CYAN), gbc);
gbc.gridy++;
frame.add(createPane(Color.MAGENTA), gbc);
gbc.gridy++;
frame.add(createPane(Color.ORANGE), gbc);
gbc.gridy++;
frame.add(createPane(Color.PINK), gbc);
gbc.gridx++;
gbc.weightx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.VERTICAL;
frame.add(createPane(Color.YELLOW), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public JPanel createPane(Color color) {
JPanel pane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
pane.setBackground(color);
return pane;
}
}
Have a look at How to Use GridBagLayout for more details
The abrupt changes in size from the MadProgrammer's solution can be
easily fixed in two ways:
public JPanel createPane(Color color) {
JPanel pane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(50, 50);
}
};
pane.setBackground(color);
return pane;
}
We also provide a mimimum size which is equal to the preferred size.
This way the panels are not shrinking but they are cut from the window
when the container is too small to display them.
The optimal solution is probably to set the weighty to 1. The panels will
gradually being shrinking.
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
Finally, I also created a solution with MigLayout. If possible, try
to create your layouts with MigLayout rather than with GridBagLayout.
package com.zetcode;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutPanels extends JFrame {
public MigLayoutPanels() {
initUI();
setTitle("Panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("wrap"));
add(createPanel(), "w 200, push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "cell 1 0 1 6, growy");
pack();
}
public JPanel createPanel() {
JPanel pnl = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
pnl.setBorder(BorderFactory.createEtchedBorder());
return pnl;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutPanels ex = new MigLayoutPanels();
ex.setVisible(true);
}
});
}
}
GridBagLayout is a powerful manager which can be used to create most layouts.
Many programmers find it difficult to use. MigLayout is easier to understand,
more powerful, and much less verbose.
Related
I am trying to move a table to the left side of a JPanel which is also inside a Window
What I get is this:
What I want is this:
I tried using .setBounds method but it does not work. Does anyone know what to do?
This is my code:
package Modulos;
import paneles.*;
import javax.swing.*;
public class ModuloAdmin extends JFrame {
public ModuloAdmin(){
//Crear tabla
String[][] datosprueba = {
{"001","Carlitos", "Casa","51202011"},
{"001","Carlitos", "Casa","51202011"}
};
String[] TituloColumna = {"Código", "Nombre", "Dirección", "Teléfono"};
JTable TablaSucursales = new JTable(datosprueba,TituloColumna);
//Contenido de las pestañas
JPanel PanelSucursales = new JPanel();
PanelSucursales.add(TablaSucursales);
PanelSucursales.add(new JScrollPane(TablaSucursales));
JPanel PanelProductos = new JPanel();
PanelProductos.add(new JLabel("Panel productos"));
JPanel PanelClientes = new JPanel();
PanelClientes.add(new JLabel("Panel Clientes"));
JPanel PanelVendedores = new JPanel();
PanelVendedores.add(new JLabel("Panel Vendedores"));
//CREACION DE PESTAÑAS
JTabbedPane Pestanias = new JTabbedPane();
Pestanias.setBounds(20,80,700,700);
Pestanias.add("Sucursales",PanelSucursales);
Pestanias.add("Productos",PanelProductos);
Pestanias.add("Clientes", PanelClientes);
Pestanias.add("Vendedores",PanelVendedores);
//vENTANA MODULO DE ADMIN
JFrame VentanaModuloAdmin = new JFrame();
VentanaModuloAdmin.add(Pestanias);
VentanaModuloAdmin.setLayout(null);
VentanaModuloAdmin.setSize(800,850);
VentanaModuloAdmin.setLocationRelativeTo(null);
VentanaModuloAdmin.setTitle("Módulo de administrador");
VentanaModuloAdmin.setVisible(true);
VentanaModuloAdmin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Use appropriate layout mangers and container management
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
public TestPane() {
setBorder(new EmptyBorder(16, 16, 16, 16));
setLayout(new GridLayout(0, 2));
add(new JScrollPane(table));
add(createButtonPane());
}
protected JPanel createButtonPane() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 16;
gbc.ipady = 16;
gbc.fill = gbc.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Clear"), gbc);
gbc.gridx = 1;
panel.add(new JButton("Carga Masiva"), gbc);
gbc.gridy++;
gbc.gridx = 0;
panel.add(new JButton("Actualizar"), gbc);
gbc.gridx = 1;
panel.add(new JButton("Eliminar"), gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
panel.add(new JButton("Exportar Listado a PDF"), gbc);
return panel;
}
}
}
Seriously, layout managers solve one of the most difficult issues facing UI developers - the variable nature of the output (screen resolutions, font metrics, accessibility modifiers, a whole bunch of different stuff which changes how layouts need to be calculated).
They might seem difficult to start with, but once you get use to using them and learn how to make use of "compound layouts", which is demonstrated above, it will help you make better UIs more quickly
i want to make a bigger resolution/fullscreen but i dont know what layout should i use, can u guys help me to choose a better layout
this is the first code for first pic
tp = new JTabbedPane();
tp.setBounds(0,0,250,400);
panel = new JPanel();
panel.setPreferredSize(new Dimension(250, 480));
tp.addTab("Mahasiswa",panel);
panel.setLayout(new FlowLayout());
label_insert = new JLabel("NIM : ");
label_insert2 = new JLabel("ID Jurusan : ");
tf_insert1 = new JTextField(15);
tf_insert2 = new JTextField(15);
Insert = new JButton(" Insert ");
//panel add
this one i want to make it on middle of frame and all of them already on panel, how can i move it to the middle ?
this is code for the second picture
public void mainform(){
main = new JPanel();
main.setLayout(new FlowLayout(FlowLayout.CENTER));
label_main = new JLabel("ID");
label_main2 = new JLabel("Password");
tf_main = new JTextField(15);
tf_main2 = new JPasswordField(15);
login = new JButton("LOGIN");
login.addActionListener(this);
main.add(label_main);
main.add(tf_main);
main.add(label_main2);
Following this guide here I would suggest the following:
yourFrame = new JFrame();
yourFrame.setLayout(new BorderLayout());
tp = new JTabbedPane();
yourFrame.add(tp, BorderLayout.CENTER);
Then fiddle with GridLayout, GridBagLayout, or GroupLayout for the tabs in your tabbedpane.
I would consider using a GridBagLayout which will give you greater flexibility in how the components are laid out
import java.awt.EventQueue;
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.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JLabel("ID"), gbc);
gbc.gridx++;
add(new JTextField(20), gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JLabel("Password"), gbc);
gbc.gridx++;
add(new JTextField(20), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton("Login"), gbc);
}
}
}
See Laying Out Components Within a Container and How to Use GridBagLayout for more details
I'm trying to get the following layout
Label..................NumericField
Label2................NumericField
Label3333..........NumericField
Basically the (.) dots would be empty space. I had tried GridBagLayout with making the label's gridwidth as 5 and the NumericField's gridwidth as 1. I'm posting the code below. But I don't see the desired result and I see all components aligned at the center instead of Labels being at left border and NFs being at right border.
For Labels:
GridBagConstraints localC = new GridBagConstraints();
localC.anchor = GridBagConstraints.FIRST_LINE_START;
//localC.fill = GridBagConstraints.HORIZONTAL;
localC.weightx = 1.0;
localC.weighty = 1.0;
localC.gridx = 0;
localC.gridy = 0;
localC.gridheight = 1;
localC.gridwidth = 5;
localC.insets = new Insets(0, 0, 0, 0);
For NumericFields
localC.anchor = GridBagConstraints.RELATIVE;
localC.weightx = 0.5;
localC.weighty = 0.5;
localC.gridx = 1;
localC.gridy = 0;
localC.gridheight = 1;
localC.gridwidth = 1;
I'm new to JAVA and struggling with layouts generally.
Add a value to the Insets right property, which will add that number of pixels to the right side of the column. You could also use GridBagConstraints#anchor set to GridBagConstraints.WEST, which will force the components in the columns to be positioned on the left hand side of the "column", this ensures that when a component in the column is wider, they won't be laid out in the middle of the resulting space.
gridwidth determines how a given cell will span across multiple columns, but if there are no other components in the resulting columns, they are discard (defaulted to 0), so in your layout, it's meaningless.
See How to Use GridBagLayout more details
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0, 0, 0, 12);
add(new JLabel("Label"), gbc);
gbc.gridy++;
add(new JLabel("Label2"), gbc);
gbc.gridy++;
add(new JLabel("Label3333"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(0, 0, 0, 0);
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
}
}
}
I want to align all the JLabels to the left side of the panel. Following is my code, but it doesnt work properly, I dont know why.
JFrame frame1 = new JFrame("Register a passenger");
frame1.setVisible(true);
frame1.setSize(550, 200);
JPanel panel = new JPanel();
frame1.add(panel);
JLabel label1 = new JLabel("Name",SwingConstants.LEFT);
JLabel label2 = new JLabel("Activities",SwingConstants.LEFT);
JButton jbtReg = new JButton("Register");
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(jbtReg);
Based on your example, you could use
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
But this will align all your components to the left.
You could also consider using a different layout manager or combination of layout managers?
Take a look at A Visual Guide to Layout Managers for more ideas
Updated
FlowLayout (which is the default layout manager for JPanel) doesn't give you a lot of options, instead consider trying to use a different layout manager or combination of layout managers, for example...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LayoutExample {
public static void main(String[] args) {
new LayoutExample();
}
public LayoutExample() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("Name:"), gbc);
gbc.gridy++;
add(new JLabel("Activity:"), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(20), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 2;
add(new JButton("Register"), gbc);
}
}
}
I need to build in swing a screen that is something like this image. I have a main panel and in that panel I need to add multiple columns 2 vertical columns and 3 horizontal. This columns are Jpanels. I tried to use GridLayout but I did not succeeded.
Very rarly will a single layout manager do everything you want. You want to start using compound layout managers where you can/need.
This example uses both a GridLayout and a GridBagLayout
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class BadLayout21 {
public static void main(String[] args) {
new BadLayout21();
}
public BadLayout21() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new GridBagLayout());
JPanel leftPane = new JPanel(new GridLayout(2, 0, 0, 4));
leftPane.add(createPane(Color.RED));
leftPane.add(createPane(Color.RED));
JPanel leftMiddlePanel = createPane(Color.BLUE);
JPanel rightMiddlePanel = createPane(Color.BLUE);
JPanel rightPane = new JPanel(new GridLayout(2, 0, 0, 4));
rightPane.add(createPane(Color.GREEN));
rightPane.add(createPane(Color.GREEN));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(20, 20, 0, 0);
add(leftPane, gbc);
gbc.gridx = 3;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(20, 0, 0, 20);
add(rightPane, gbc);
gbc.weightx = 0;
gbc.gridx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(0, 0, 0, 10);
add(leftMiddlePanel, gbc);
gbc.gridx = 2;
gbc.insets = new Insets(0, 10, 0, 0);
add(rightMiddlePanel, gbc);
}
protected JPanel createPane(Color color) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(25, 25));
panel.setBackground(color);
return panel;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
I think it will be difficult to place all the panels directly in one big panel. Multiple solutions are possible. Like dividing the the main panel in four seperate (vertical) panels and add the final panels to those four sub-panels. Or as freak suggested, start with a border layout, and first create a left-subpanel, center-subpanel and right-subpanel, each with their own layout managers. And in those sub-panels you can place your final panels.
Although the precise purpose or goal is a little fuzzy for me right now, I hope you can use my suggesten.