How to align the elements to the top in a GridBagLayout? - java

I want to put a text element in a GridBagLayout after clicking on a button.
But the elements are vertically centered. So how can I force the grid bag layout to position the elements to the top?
Here is an example code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class RefreshPanel {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JTextPane [] textPane;// = new JTextPane[1];
private JScrollPane scrollbar;
private ArrayList arrayList = new ArrayList();
private JButton newItem = new JButton("new");
private GridBagLayout gbl = new GridBagLayout();
private JMenu menuStart= new JMenu("Start");
private JMenu menuEdit= new JMenu("Edit");
private JMenu menuWindow= new JMenu("Window");
private JMenu menuHelp= new JMenu("help");
private JMenuBar menuBar = new JMenuBar();
RefreshPanel() {
menuBar.add(menuStart);
menuBar.add(menuEdit);
menuBar.add(menuWindow);
menuBar.add(menuHelp);
scrollbar = new JScrollPane(panel);
panel.setBackground(Color.WHITE);
panel.setLayout(gbl);
addButtonListener();
createFrame();
} //constructor
public void addButtonListener() {
newItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
arrayList.add("data");
textPane = generateTextPane(arrayList.size(), arrayList);
System.out.println("Länge: "+textPane.length);
for(int i=0;i<textPane.length;i++) {
System.out.println(textPane[i].getText());
addComponent(panel, gbl, textPane[i], 0, i, 1, 1, 1, 0);
panel.revalidate();
}
frame.revalidate();
}
});
}
private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
JTextPane [] textPane = new JTextPane[arraySize];
for(int i=0;i<textPane.length;i++) {
textPane[i]=new JTextPane();
textPane[i].setText((String) arrayList.get(i));
System.out.println("length >> "+textPane.length);
}
return textPane;
}
public void addComponent(Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbc.anchor = GridBagConstraints.SOUTH;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public void createFrame() {
frame.add(menuBar, BorderLayout.NORTH);
frame.add(scrollbar, BorderLayout.CENTER);
frame.add(newItem, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300,300));
frame.setVisible(true);
}
public static void main(String [] args) {
new RefreshPanel();
}
}
I know that the grids are dynamically changing their size when I put a new element.
Any ideas how can I solve my problem?

I have changed your GridBagLayout properties. Check this:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class RefreshPanel {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JTextPane [] textPane;// = new JTextPane[1];
private JScrollPane scrollbar;
private ArrayList arrayList = new ArrayList();
private JButton newItem = new JButton("new");
private GridBagLayout gbl = new GridBagLayout();
private JMenu menuStart= new JMenu("Start");
private JMenu menuEdit= new JMenu("Edit");
private JMenu menuWindow= new JMenu("Window");
private JMenu menuHelp= new JMenu("help");
private JMenuBar menuBar = new JMenuBar();
RefreshPanel() {
menuBar.add(menuStart);
menuBar.add(menuEdit);
menuBar.add(menuWindow);
menuBar.add(menuHelp);
scrollbar = new JScrollPane(panel);
panel.setBackground(Color.WHITE);
gbl.columnWidths = new int[]{0, 0, 0, 0};
gbl.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl);
addButtonListener();
createFrame();
} //constructor
public void addButtonListener() {
newItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
arrayList.add("data");
textPane = generateTextPane(arrayList.size(), arrayList);
System.out.println("Länge: "+textPane.length);
for(int i=0;i<textPane.length;i++) {
System.out.println(textPane[i].getText());
panel.add(textPane[i],new GridBagConstraints( 0, i, 1, 1,
1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
panel.revalidate();
}
frame.revalidate();
}
});
}
private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
JTextPane [] textPane = new JTextPane[arraySize];
for(int i=0;i<textPane.length;i++) {
textPane[i]=new JTextPane();
textPane[i].setText((String) arrayList.get(i));
System.out.println("length >> "+textPane.length);
}
return textPane;
}
public void addComponent(Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbc.anchor = GridBagConstraints.SOUTH;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public void createFrame() {
frame.add(menuBar, BorderLayout.NORTH);
frame.add(scrollbar, BorderLayout.CENTER);
frame.add(newItem, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300,300));
frame.setVisible(true);
}
public static void main(String [] args) {
new RefreshPanel();
}
}

Related

GridBagLayout 25% 50% 25%

Is this the right way to prevent GridBagLayout cells from being resized relative to their contents?
gbl_panel.columnWidths = new int[] {1000, 4000, 1000};
SSCCE
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class GridBagLayoutTemplate extends JFrame {
private JPanel contentPane;
public GridBagLayoutTemplate() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.rowHeights = new int[] {1};
gbl_panel.columnWidths = new int[] {1000, 4000, 1000};
gbl_panel.columnWeights = new double[]{1.0, 4.0, 1.0};
gbl_panel.rowWeights = new double[]{1.0};
panel.setLayout(gbl_panel);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
panel.add(scrollPane, gbc_scrollPane);
JList list = new JList();
scrollPane.setViewportView(list);
list.setModel(new AbstractListModel() {
String[] values = new String[] {"qwe"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 1;
gbc_scrollPane_1.gridy = 0;
panel.add(scrollPane_1, gbc_scrollPane_1);
JList list_1 = new JList();
scrollPane_1.setViewportView(list_1);
list_1.setModel(new AbstractListModel() {
String[] values = new String[] {"qwe"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
JScrollPane scrollPane_2 = new JScrollPane();
GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
gbc_scrollPane_2.gridx = 2;
gbc_scrollPane_2.gridy = 0;
panel.add(scrollPane_2, gbc_scrollPane_2);
JList list_3 = new JList();
scrollPane_2.setViewportView(list_3);
list_3.setModel(new AbstractListModel() {
String[] values = new String[] {"qweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqweqweqweqweqweqwqweqweqweqwe"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutTemplate frame = new GridBagLayoutTemplate();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Interesting. I've never seen an approach like this before.
Typically the GridBagLayout is used such that:
Each component is allocated space based on its preferred size
If extra space is available then space is allocated to to each component based on the weightx (weighty) values assigned to each component.
Therefore only the extra space is allocated in the 25% 50% 25% ratio.
Your approach seems to ignore the preferred size of components and allocate space in the 25% 50% 25% ratio.
However, you would first need to make the following change to get the proper ratio:
//gbl_panel.columnWidths = new int[] {1000, 4000, 1000};
//gbl_panel.columnWeights = new double[]{1.0, 4.0, 1.0};
gbl_panel.columnWidths = new int[] {1000, 2000, 1000};
gbl_panel.columnWeights = new double[]{1.0, 2.0, 1.0};
Below is the simplified code I used to test allocating space to components on a panel that is 400 pixels wide:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutRelative extends JPanel
{
public GridBagLayoutRelative()
{
setPreferredSize( new Dimension(400, 300) );
// Components will be allocated relative widths of 25%, 50%, 25%
// (preferred width will be ignored)
GridBagLayout gbl = new GridBagLayout();
//gbl.columnWidths = new int[] {25, 50, 25}; // doesn't work
gbl.columnWidths = new int[] {250, 500, 250}; // larger value in same ratio
gbl.columnWeights = new double[] {25.0, 50.0, 25.0};
setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1.0;
gbc.gridy = 0;
JPanel red = new JPanel();
red.setBackground( Color.RED );
red.setPreferredSize( new Dimension(30, 200) );
gbc.gridx = 0;
add(red, gbc);
JPanel green = new JPanel();
green.setBackground( Color.GREEN );
green.setPreferredSize( new Dimension(40, 200) );
gbc.gridx = 1;
add(green, gbc);
JPanel blue = new JPanel();
blue.setBackground( Color.BLUE );
blue.setPreferredSize( new Dimension(50, 200) );
gbc.gridx = 2;
add(blue, gbc);
addComponentListener( new ComponentAdapter()
{
#Override
public void componentResized(ComponentEvent e)
{
System.out.println( e.getComponent().getSize() );
for (Component c: getComponents())
System.out.println( "\t" + c.getSize() );
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("GridBagLayout Relative");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GridBagLayoutRelative());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
}
}
And the results are:
java.awt.Dimension[width=100,height=300]
java.awt.Dimension[width=200,height=300]
java.awt.Dimension[width=100,height=300]
So I would say the approach works.
Note: In case you are interested, in the past I have suggested you can use the Relative Layout which was designed specifically for this type of layout and is easier to use.
Using the RelativeLayout the above behaviour would be replicated by using:
RelativeLayout rl = new RelativeLayout(RelativeLayout.X_AXIS);
rl.setFill(true);
setLayout( rl );
JPanel red = new JPanel();
red.setBackground( Color.RED );
red.setPreferredSize( new Dimension(30, 200) );
add(red, new Float(25));
JPanel green = new JPanel();
green.setBackground( Color.GREEN );
green.setPreferredSize( new Dimension(40, 200) );
add(green, new Float(50));
JPanel blue = new JPanel();
blue.setBackground( Color.BLUE );
blue.setPreferredSize( new Dimension(50, 200) );
add(blue, new Float(25));

JPanel with CardLayout is blank when it changed

I've a problem with a JPanel.
It's a CardLayout and two sub-panel that change.
I need to do a button which switch from subpanel 1 to 2 and display selected Product.
ISSUE:
When I select a product card, JPanel changes but it's blank.
If I step over the window with the mouse , the buttons appear, but not the JLabels.
And If I resize the windows, it's empty again.
What is the problem?
EDIT:
Here the code that return same error.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
// private String title;
// private Dimension d;
public MainFrame(String title, Dimension d) {
// LoginPanel template = new LoginPanel(this);
// RegisterPanel template = new RegisterPanel(this);
CustomerPanel template = new CustomerPanel(this);
this.setTitle(title);
this.setSize(d);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.getContentPane().add(template);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new MainFrame("Fubars", new Dimension(800, 650));
});
}
}
#SuppressWarnings("serial")
class CustomerPanel extends JPanel {
MainFrame mf;
JPanel one, two;
JPanel panel;
public CustomerPanel(MainFrame mf) {
this.mf = mf;
mf.getContentPane().setLayout(new CardLayout(0, 0));
JPanel container = new JPanel();
mf.getContentPane().add(container, "name_36743208542992");
container.setLayout(new BorderLayout(0, 0));
JPanel back = new JPanel();
container.add(back, BorderLayout.NORTH);
back.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton btnControl = new JButton("<");
back.add(btnControl);
panel = new JPanel();
container.add(panel, BorderLayout.CENTER);
CardLayout cl = new CardLayout(0, 0);
panel.setLayout(cl);
one = new OnePanel(this.mf);
two = new JPanel();
panel.add(one, "1");
panel.add(two, "2");
btnControl.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
two = new LoginPanel(mf);
cl.next(panel);
}
});
}
}
#SuppressWarnings("serial")
class LoginPanel extends JPanel {
// private MainFrame mf;
private JTextField textField;
private JPasswordField passwordField;
public LoginPanel(MainFrame mf) {
// this.mf = mf;
mf.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
mf.getContentPane().add(panel, BorderLayout.CENTER);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 100, 55, 72, 171, 0 };
gbl_panel.rowHeights = new int[] { 69, 22, 22, 0 };
gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Email");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.fill = GridBagConstraints.HORIZONTAL;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 1;
panel.add(lblNewLabel, gbc_lblNewLabel);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.anchor = GridBagConstraints.NORTH;
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.gridx = 3;
gbc_textField.gridy = 1;
panel.add(textField, gbc_textField);
textField.setColumns(15);
JLabel lblNewLabel_1 = new JLabel("Password");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel_1.gridx = 1;
gbc_lblNewLabel_1.gridy = 2;
panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
passwordField = new JPasswordField();
passwordField.setColumns(15);
GridBagConstraints gbc_passwordField = new GridBagConstraints();
gbc_passwordField.anchor = GridBagConstraints.NORTH;
gbc_passwordField.gridx = 3;
gbc_passwordField.gridy = 2;
panel.add(passwordField, gbc_passwordField);
JPanel panel_1 = new JPanel();
mf.getContentPane().add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 30));
JButton btnNewButton = new JButton("LOGIN");
panel_1.add(btnNewButton);
btnNewButton.setActionCommand("login");
JButton btnRegistration = new JButton("REGISTER");
panel_1.add(btnRegistration);
btnRegistration.setActionCommand("registration");
}
}
#SuppressWarnings("serial")
class OnePanel extends JPanel {
// private MainFrame mf;
public OnePanel(MainFrame mf) {
// this.mf = mf;
mf.getContentPane().setLayout(new CardLayout(0, 0));
JPanel container = new JPanel();
mf.getContentPane().add(container, "name_36743208542992");
container.setLayout(new BorderLayout(0, 0));
JPanel image = new JPanel();
container.add(image, BorderLayout.CENTER);
JButton btnImageBack = new JButton("<");
image.add(btnImageBack);
JLabel imageContainer = new JLabel("Images");
image.add(imageContainer);
imageContainer.setBounds(new Rectangle(100, 100, 100, 100));
imageContainer.setHorizontalTextPosition(SwingConstants.CENTER);
imageContainer.setHorizontalAlignment(SwingConstants.CENTER);
imageContainer.setAlignmentX(Component.CENTER_ALIGNMENT);
imageContainer.setIconTextGap(3);
imageContainer.setIcon(null);
JButton btnImageForward = new JButton(">");
image.add(btnImageForward);
btnImageForward.setAlignmentY(Component.BOTTOM_ALIGNMENT);
btnImageForward.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel info = new JPanel();
container.add(info, BorderLayout.EAST);
GridBagLayout gbl_info = new GridBagLayout();
gbl_info.columnWidths = new int[] { 0, 0, 63, 0, 0, 0 };
gbl_info.rowHeights = new int[] { 0, 0, 25, 0, 0, 0, 0 };
gbl_info.columnWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE };
gbl_info.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
info.setLayout(gbl_info);
JLabel lblTitle = new JLabel("Title");
GridBagConstraints gbc_lblTitle = new GridBagConstraints();
gbc_lblTitle.anchor = GridBagConstraints.NORTHWEST;
gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
gbc_lblTitle.gridx = 2;
gbc_lblTitle.gridy = 1;
info.add(lblTitle, gbc_lblTitle);
JLabel lblDescription = new JLabel("Description");
GridBagConstraints gbc_lblDescription = new GridBagConstraints();
gbc_lblDescription.anchor = GridBagConstraints.WEST;
gbc_lblDescription.insets = new Insets(0, 0, 5, 5);
gbc_lblDescription.gridx = 2;
gbc_lblDescription.gridy = 2;
info.add(lblDescription, gbc_lblDescription);
JLabel lblPrice = new JLabel("Price");
GridBagConstraints gbc_lblPrice = new GridBagConstraints();
gbc_lblPrice.anchor = GridBagConstraints.WEST;
gbc_lblPrice.insets = new Insets(0, 0, 5, 5);
gbc_lblPrice.gridx = 2;
gbc_lblPrice.gridy = 3;
info.add(lblPrice, gbc_lblPrice);
lblPrice.setToolTipText("Price");
JButton btnAddCart = new JButton("Add to Cart");
GridBagConstraints gbc_btnAddCart = new GridBagConstraints();
gbc_btnAddCart.insets = new Insets(0, 0, 5, 5);
gbc_btnAddCart.anchor = GridBagConstraints.WEST;
gbc_btnAddCart.gridx = 2;
gbc_btnAddCart.gridy = 4;
info.add(btnAddCart, gbc_btnAddCart);
}
}

Increasing distance from top of JPanel

I'm trying to increase the distance of my JButtons from the top of my Panel to make it more visually appealing, i've tried using an invisible button but have had no luck.
public class SimpleBorder {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border etched = (Border) BorderFactory.createEtchedBorder();
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
JTextArea text = new JTextArea(10, 40);
JScrollPane scrol = new JScrollPane(text);
JScrollPane scrol2 = new JScrollPane(list);
JPanel panel= new JPanel();
panel.add(scrol2,BorderLayout.WEST);
panel.add(scrol, BorderLayout.EAST);
panel.setBorder(etched);
frame.add(panel);
frame.setVisible(true);
}
}
Any ideas ?
The key basically lies, in the fact, that the component doesn't knows it's actual size, till frame.pack() won't be called. Hence after this I am performing this calculation, to determine how much empty space to put for Border and again calling frame.pack() to repack() everything after putting the Border.
Please do have a look at this example, and see if this suite your needs :
import java.awt.*;
import javax.swing.*;
public class MainMenu {
private JButton playButton;
private JButton instructionButton;
private JButton scoreboardButton;
private JButton exitButton;
private JPanel menuPanel;
private GridBagConstraints gbc;
public MainMenu() {
gbc = new GridBagConstraints();
gbc.insets = new Insets(15, 15, 15, 15);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
}
private void displayGUI() {
JFrame frame = new JFrame("Main Menu");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout());
menuPanel = new JPanel(new GridBagLayout());
menuPanel.setOpaque(true);
menuPanel.setBackground(Color.BLACK);
playButton = new JButton("Play");
instructionButton = new JButton("Instructions");
scoreboardButton = new JButton("Scoreboard");
exitButton = new JButton("Exit");
addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.20,
GridBagConstraints.HORIZONTAL);
contentPane.add(menuPanel);
frame.setContentPane(contentPane);
frame.pack();
contentPane.setBorder(
BorderFactory.createEmptyBorder(
contentPane.getHeight() - (contentPane.getHeight() / 4),
20, 5, 20));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp,
int gridx, int gridy,
int gridwidth, int gridheight,
double weightx, double weighty,
int fill) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
panel.add(comp, gbc);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new MainMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
Here is the output :
EDIT 1 :
Moreover, if you will use GridLayout instead of using GridBagLayout for the MainMenu, then I guess the results will be more promising. Please have a look at this example for that change :
import java.awt.*;
import javax.swing.*;
public class MainMenu {
private JButton playButton;
private JButton instructionButton;
private JButton scoreboardButton;
private JButton exitButton;
private JPanel menuPanel;
private void displayGUI() {
JFrame frame = new JFrame("Main Menu");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new GridBagLayout());
menuPanel = new JPanel(new GridLayout(0, 1, 5, 5));
menuPanel.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
menuPanel.setOpaque(true);
menuPanel.setBackground(Color.BLACK);
playButton = new JButton("Play");
instructionButton = new JButton("Instructions");
scoreboardButton = new JButton("Scoreboard");
exitButton = new JButton("Exit");
menuPanel.add(playButton);
menuPanel.add(instructionButton);
menuPanel.add(scoreboardButton);
menuPanel.add(exitButton);
contentPane.add(menuPanel);
frame.setContentPane(contentPane);
frame.pack();
contentPane.setBorder(
BorderFactory.createEmptyBorder(
contentPane.getHeight() -
(contentPane.getHeight() -
(3 * menuPanel.getHeight())),
20, 0, 20));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new MainMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
EDIT 2 :
Another variant is looking much better, though, this time, the base JPanel is using GridLayout and the MenuPanel is using GridBagLayout. Please have a look at this example :
import java.awt.*;
import javax.swing.*;
public class MainMenu {
private JButton playButton;
private JButton instructionButton;
private JButton scoreboardButton;
private JButton exitButton;
private JPanel menuPanel;
private GridBagConstraints gbc;
public MainMenu() {
gbc = new GridBagConstraints();
gbc.insets = new Insets(15, 15, 15, 15);
gbc.anchor = GridBagConstraints.CENTER;
}
private void displayGUI() {
JFrame frame = new JFrame("Main Menu");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new GridLayout(1, 1, 5, 2));
menuPanel = new JPanel(new GridBagLayout());
menuPanel.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
menuPanel.setOpaque(true);
menuPanel.setBackground(Color.BLACK);
playButton = new JButton("Play");
instructionButton = new JButton("Instructions");
scoreboardButton = new JButton("Scoreboard");
exitButton = new JButton("Exit");
addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.10,
GridBagConstraints.CENTER);
contentPane.add(menuPanel);
frame.setContentPane(contentPane);
frame.pack();
contentPane.setBorder(
BorderFactory.createEmptyBorder(
contentPane.getHeight() -
(contentPane.getHeight() -
(2 * menuPanel.getHeight()) + 100),
20, 2, 20));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp,
int gridx, int gridy,
int gridwidth, int gridheight,
double weightx, double weighty,
int fill) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
panel.add(comp, gbc);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new MainMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}

GridBagLayout 2 JPanels - One less width than other

I would like to find out how exactly would I go about to having two JPanels on one JPanel, all using GridBagLayout.
Basically you have your Top Most JPanel and then with 2 JPanels on their as below requirement:
--------------------------------------
| | |
| | |
| | |
| JPanel 1 | JPanel |
| | 2 |
| | |
| | |
--------------------------------------
Basically I would need JPanel 1 to be bigger than JPanel 2. But also JPanel 2 must NOT resize, JPanel 1 should only resize if the main panel gets resized.
Any idea's?
Have a look at this code example and see if this is what you wanted :
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GridBagExample {
private JPanel leftPanel;
private JPanel rightPanel;
private GridBagConstraints gbc;
private Random random;
public GridBagExample() {
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(5, 5, 5, 5);
random = new Random();
}
private void displayGUI() {
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel();
contentPane.setLayout(new GridBagLayout());
leftPanel = getPanel();
rightPanel = getPanel();
addComp(contentPane, leftPanel, 0, 0, 1, 1,
GridBagConstraints.BOTH, 0.7, 1.0);
addComp(contentPane, rightPanel, 1, 0, 1, 1,
GridBagConstraints.BOTH, 0.3, 1.0);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp, int gridx,
int gridy, int gridwidth, int gridheight,
int fill, double weightx, double weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
return panel;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new GridBagExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
Though if you want your right JPanel not to change size, you can pass GridBagConstraints.NONE to the function instead of GridBagConstraints.BOTH as done by me. Since without the actual contents known for this JPanel which will become its part, its hard to present its true size.
OUTPUT :
EDIT :
Updating my code, to better explain, as to what I am saying, though I used user2699405's (OP) ideas too from the comments.
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GridBagExample {
private JPanel leftPanel;
private JPanel rightPanel;
private GridBagConstraints gbc;
private Random random;
public GridBagExample() {
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(5, 5, 5, 5);
random = new Random();
}
private void displayGUI() {
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel();
contentPane.setLayout(new GridBagLayout());
leftPanel = getPanel();
rightPanel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return (new Dimension(100, 100));
}
};
addComp(contentPane, leftPanel, 0, 0, 1, 1,
GridBagConstraints.BOTH, 1.0, 1.0);
addComp(contentPane, rightPanel, 1, 0, 1, 1,
GridBagConstraints.NONE, 0.0, 1.0);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp, int gridx,
int gridy, int gridwidth, int gridheight,
int fill, double weightx, double weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
return panel;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new GridBagExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}

How to prevent Boxlayout/Box from stretching children components?

As you can see in the runnable code below, i try to have a Box with expandable child-boxes. The children Boxes can change they size and this all works good. The main problem is that the size is always relative to the parent. But I want them to have a specific size and in case there is no place anymore use the JScrollPane. At the moment they shrink the other children-boxes only.
I tried Glue and Filler, but it didn't work. The glue just had no effect and the filler had the side effect to keep always some place at the (even when the ScrollPane is in action). That is pretty ugly to have there so much free space.
So, do you know a good way to prevent the Boxes from stretching the children?
Thank you in advance!
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
public class ExpandableMenueDemo {
Box allBoxes;
ExpandableMenueDemo(){
allBoxes = Box.createVerticalBox();
TitledBorder title;
title = BorderFactory.createTitledBorder("Filter");
allBoxes.setBorder(title);
for (int i = 0 ;i<3;i++){
//generate collapsable components
SubBox b = new SubBox("SubBox"+i);
allBoxes.add(b.getSwingBox());
}
allBoxes.add(Box.createVerticalGlue());
}
public Container getMenue(){
return allBoxes;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
ExpandableMenueDemo m = new ExpandableMenueDemo();
Box mainBox = Box.createHorizontalBox();
mainBox.add(new JScrollPane(m.getMenue()));
mainBox.add(new JTable(20,5));
frame.setContentPane(mainBox);
frame.pack();
frame.setVisible(true);
}
class SubBox{
Box box;
Box header;
String name;
JButton cBtn;
boolean isCollapsed = true;
JLabel headerLine;
SubBox(String name) {
this.name= name;
box = Box.createVerticalBox();
headerLine = new JLabel(name+" () :");
header= Box.createHorizontalBox();
cBtn = new JButton("v");
cBtn.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
if (isCollapsed)show();
else collapse();
}
});
collapse();
header.add(cBtn);
header.add(Box.createHorizontalStrut(10));
header.add(headerLine);
header.add(Box.createHorizontalGlue());
}
Box getSwingBox() {
Box b = Box.createVerticalBox();
b.add(header);
b.add(box);
return b;
}
public void collapse(){
System.out.println("collapse");
box.removeAll();
this.isCollapsed=true;
cBtn.setText("v");
}
public void show(){
System.out.println("show");
box.removeAll();
this.isCollapsed=false;
cBtn.setText("^");
for (int i = 0; i<3;i++) {
Box b = Box.createHorizontalBox();
b.add(Box.createHorizontalStrut(20));
b.add(new JCheckBox("checkBox "+i));
b.add(Box.createHorizontalGlue());
box.add(b);
}
}
}
}
BoxLayout can accepting setXxxSize,
maybe better example for expanding/collapsing JPanels nests another JComponents
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ExpandingPanels extends MouseAdapter {
private ActionPanel[] aps;
private JPanel[] panels;
public ExpandingPanels() {
assembleActionPanels();
assemblePanels();
}
#Override
public void mousePressed(MouseEvent e) {
ActionPanel ap = (ActionPanel) e.getSource();
if (ap.target.contains(e.getPoint())) {
ap.toggleSelection();
togglePanelVisibility(ap);
}
}
private void togglePanelVisibility(ActionPanel ap) {
int index = getPanelIndex(ap);
if (panels[index].isShowing()) {
panels[index].setVisible(false);
} else {
panels[index].setVisible(true);
}
ap.getParent().validate();
}
private int getPanelIndex(ActionPanel ap) {
for (int j = 0; j < aps.length; j++) {
if (ap == aps[j]) {
return j;
}
}
return -1;
}
private void assembleActionPanels() {
String[] ids = {"level 1", "level 2", "level 3", "level 4"};
aps = new ActionPanel[ids.length];
for (int j = 0; j < aps.length; j++) {
aps[j] = new ActionPanel(ids[j], this);
}
}
private void assemblePanels() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 1, 2, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
JPanel p1 = new JPanel(new GridBagLayout());
gbc.gridwidth = GridBagConstraints.RELATIVE;
p1.add(new JButton("button 1"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p1.add(new JButton("button 2"), gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
p1.add(new JButton("button 3"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p1.add(new JButton("button 4"), gbc);
JPanel p2 = new JPanel(new GridBagLayout());
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
p2.add(new JLabel("enter"), gbc);
gbc.anchor = GridBagConstraints.WEST;
p2.add(new JTextField(8), gbc);
gbc.anchor = GridBagConstraints.CENTER;
p2.add(new JButton("button 1"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p2.add(new JButton("button 2"), gbc);
JPanel p3 = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea(8, 12);
textArea.setLineWrap(true);
p3.add(new JScrollPane(textArea));
JPanel p4 = new JPanel(new GridBagLayout());
addComponents(new JLabel("label 1"), new JTextField(12), p4, gbc);
addComponents(new JLabel("label 2"), new JTextField(16), p4, gbc);
gbc.gridwidth = 2;
gbc.gridy = 2;
p4.add(new JSlider(), gbc);
gbc.gridy++;
JPanel p5 = new JPanel(new GridBagLayout());
p5.add(new JButton("button 1"), gbc);
p5.add(new JButton("button 2"), gbc);
p5.add(new JButton("button 3"), gbc);
p5.add(new JButton("button 4"), gbc);
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
p4.add(p5, gbc);
panels = new JPanel[]{p1, p2, p3, p4};
}
private void addComponents(Component c1, Component c2, Container c, GridBagConstraints gbc) {
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = GridBagConstraints.RELATIVE;
c.add(c1, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
c.add(c2, gbc);
gbc.anchor = GridBagConstraints.CENTER;
}
private JPanel getComponent() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 3, 0, 3);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
for (int j = 0; j < aps.length; j++) {
panel.add(aps[j], gbc);
panel.add(panels[j], gbc);
panels[j].setVisible(false);
}
JLabel padding = new JLabel();
gbc.weighty = 1.0;
panel.add(padding, gbc);
return panel;
}
public static void main(String[] args) {
Runnable doRun = new Runnable() {
#Override
public void run() {
ExpandingPanels test = new ExpandingPanels();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(test.getComponent()));
f.setSize(360, 500);
f.setLocation(200, 100);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(doRun);
}
}
class ActionPanel extends JPanel {
private static final long serialVersionUID = 1L;
private String text;
private Font font;
private boolean selected;
private BufferedImage open, closed;
public Rectangle target;
final int OFFSET = 30,
PAD = 5;
ActionPanel(String text, MouseListener ml) {
this.text = text;
addMouseListener(ml);
font = new Font("sans-serif", Font.PLAIN, 12);
selected = false;
setBackground(new Color(200, 200, 220));
setPreferredSize(new Dimension(200, 20));
setBorder(BorderFactory.createRaisedBevelBorder());
setPreferredSize(new Dimension(200, 20));
createImages();
setRequestFocusEnabled(true);
}
public void toggleSelection() {
selected = !selected;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
if (selected) {
g2.drawImage(open, PAD, 0, this);
} else {
g2.drawImage(closed, PAD, 0, this);
}
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics(text, frc);
float height = lm.getAscent() + lm.getDescent();
float x = OFFSET;
float y = (h + height) / 2 - lm.getDescent();
g2.drawString(text, x, y);
}
private void createImages() {
int w = 20;
int h = getPreferredSize().height;
target = new Rectangle(2, 0, 20, 18);
open = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = open.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getBackground());
g2.fillRect(0, 0, w, h);
int[] x = {2, w / 2, 18};
int[] y = {4, 15, 4};
Polygon p = new Polygon(x, y, 3);
g2.setPaint(Color.green.brighter());
g2.fill(p);
g2.setPaint(Color.blue.brighter());
g2.draw(p);
g2.dispose();
closed = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
g2 = closed.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getBackground());
g2.fillRect(0, 0, w, h);
x = new int[]{3, 13, 3};
y = new int[]{4, h / 2, 16};
p = new Polygon(x, y, 3);
g2.setPaint(Color.red);
g2.fill(p);
g2.setPaint(Color.blue.brighter());
g2.draw(p);
g2.dispose();
}
}
Instead of re-inventing the wheel, you might consider to use JXTaskPane/-Container contained in SwingX. Its demo shows it in action (as taskPanes) at the left - that's the part for choosing the demos.

Categories