I'm new to using Java Swing and am looking to produce a GUI that resembles something like this:
I am pretty close but having an issue where a JLabel pushes up on the grid design like so:
How can I avoid this happening? I place the grid into a GridBagLayout panel and the JLabel into another GridBag panel(plan on putting a JToolbar here too), then add them to the main frame.
GridBagLayout tileLayout = new GridBagLayout();
GridBagConstraints tileLayoutConstraints = new GridBagConstraints();
JPanel tilePanel = new JPanel(tileLayout);
JPanel selectionPanel = new JPanel(tileLayout);
TileButton[][] tileAccessMatrix = new TileButton[4][4];
public BoardUILayer() {
setSize(1920, 1080);
buildBoardTiles();
buildResourcePrompt();
add(selectionPanel, BorderLayout.SOUTH);
add(tilePanel, BorderLayout.CENTER);
}
private void buildBoardTiles() {
for (int r = 0; r < tileAccessMatrix.length; r++) {
for (int c = 0; c < tileAccessMatrix[r].length; c++) {
TileButton temp = new TileButton(r, c);
tileAccessMatrix[r][c] = temp;
tileLayoutConstraints.ipadx = 115;
tileLayoutConstraints.ipady = 115;
tileLayoutConstraints.gridx = r;
tileLayoutConstraints.gridy = c;
temp.addActionListener(temp);
tilePanel.add(temp, tileLayoutConstraints);
}
}
}
public void buildResourcePrompt() {
final JLabel resourceTextLabel = new JLabel("What resource would you like?");
tileLayoutConstraints.ipadx = 0;
tileLayoutConstraints.ipady = 400;
tileLayoutConstraints.gridx = 50;
tileLayoutConstraints.gridy = 50;
selectionPanel.add(resourceTextLabel, tileLayoutConstraints);
}
I created a GUI based on your drawing. This was one of the most complex GUI's I have ever created in Swing. The challenge was keeping the JButton sizes constant.
Maybe a MigLayout would work better for you.
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Netbeans section.
I created 11 inner JPanels to construct this GUI.
JFrame
main JPanel
inner JPanel
turnButton JPanel
turn JPanel
buttonGrid JPanel
resource JPanel
resourceLabel JPanel
resourceButton JPanel
manual JPanel
outerEastButton JPanel
eastButton JPanel
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class RandomGameView implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new RandomGameView());
}
#Override
public void run() {
JFrame frame = new JFrame("Random Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.add(createManualPanel(), BorderLayout.EAST);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
JPanel upperPanel = createTurnButtonPanel();
JPanel lowerPanel = createResourcePanel();
JPanel innerPanel = createUpperPanel(upperPanel, lowerPanel);
panel.add(innerPanel, BorderLayout.NORTH);
panel.add(lowerPanel, BorderLayout.SOUTH);
return panel;
}
private JPanel createUpperPanel(JPanel upperPanel, JPanel lowerPanel) {
Dimension upperSize = upperPanel.getPreferredSize();
Dimension lowerSize = lowerPanel.getPreferredSize();
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
int difference = lowerSize.width - upperSize.width;
int left = difference / 2;
int right = difference - left;
panel.setBorder(BorderFactory.createEmptyBorder(0, left, 0, right));
panel.add(upperPanel);
return panel;
}
private JPanel createTurnButtonPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(createTurnPanel(), BorderLayout.NORTH);
panel.add(createButtonGrid(), BorderLayout.SOUTH);
return panel;
}
private JPanel createTurnPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(Font.BOLD, 36f);
JLabel playerLabel = new JLabel("Player's Turn");
playerLabel.setFont(font);
panel.add(playerLabel);
return panel;
}
private JPanel createButtonGrid() {
JPanel panel = new JPanel(new GridLayout(0, 4, 2, 2));
panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
JButton[] buttonArray = new JButton[16];
Dimension buttonSize = new Dimension(100, 100);
for (int index = 0; index < buttonArray.length; index++) {
buttonArray[index] = new JButton("Empty");
buttonArray[index].setPreferredSize(buttonSize);
panel.add(buttonArray[index]);
}
return panel;
}
private JPanel createResourcePanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(createResourceLabelPanel(), BorderLayout.NORTH);
panel.add(createResourceButtonPanel(), BorderLayout.CENTER);
return panel;
}
private JPanel createResourceLabelPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(Font.BOLD, 36f);
JLabel label = new JLabel("Select a Resource");
label.setFont(font);
panel.add(label);
return panel;
}
private JPanel createResourceButtonPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton[] resourceButton = new JButton[5];
Dimension buttonSize = new Dimension(100, 100);
for (int index = 0; index < resourceButton.length; index++) {
resourceButton[index] = new JButton("Empty");
resourceButton[index].setPreferredSize(buttonSize);
panel.add(resourceButton[index]);
}
return panel;
}
private JPanel createManualPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 20, 300, 20));
Font font = panel.getFont().deriveFont(Font.BOLD, 36f);
JLabel label = new JLabel("Manual");
label.setHorizontalAlignment(JLabel.CENTER);
label.setFont(font);
panel.add(label, BorderLayout.NORTH);
JTextArea textArea = new JTextArea(5, 20);
panel.add(textArea, BorderLayout.CENTER);
JPanel eastButtonPanel = createEastButtonPanel();
JPanel innerPanel = createOuterEastButtonPanel(textArea, eastButtonPanel);
panel.add(innerPanel, BorderLayout.SOUTH);
return panel;
}
private JPanel createOuterEastButtonPanel(JTextArea textArea,
JPanel eastButtonPanel) {
Dimension eastButtonPanelSize = eastButtonPanel.getPreferredSize();
Dimension textAreaSize = textArea.getPreferredSize();
int difference = textAreaSize.width = eastButtonPanelSize.width;
int left = difference / 2;
int right = difference - left;
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel.setBorder(BorderFactory.createEmptyBorder(0, left, 0, right));
panel.add(eastButtonPanel);
return panel;
}
private JPanel createEastButtonPanel() {
JPanel panel = new JPanel(new GridLayout(0, 2, 2, 2));
panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
JButton[] resourceButton = new JButton[4];
Dimension buttonSize = new Dimension(50, 50);
for (int index = 0; index < resourceButton.length; index++) {
resourceButton[index] = new JButton();
resourceButton[index].setPreferredSize(buttonSize);
panel.add(resourceButton[index]);
}
return panel;
}
}
You can do it with MigLayout and multiple JPanel.
MigLayout is very easy to use.
Firstly, create a JPanel and add your components to that JPanel.
Secondly, create another JPanel and add your other components to that JPanel.
MigLayout has rows and columns.
new MigLayout("","[][][]"/*for ex: 3 columns*/,"[][]" /*for ex 2 rows*/)
this.add(aComponent, "cell 2 1" /*for ex: that component added to third column and second row.*/);
For more information: http://www.miglayout.com/whitepaper.html
Related
I am trying to center components using a GridBagLayout in the same manner that a Box centers components when you use Box.createVerticalGlue(). I initially did use a vertical Box:
Box box = Box.createVerticalBox();
box.add(Box.createVerticalGlue());
box.add(add);
box.add(remove);
box.add(edit);
box.add(Box.createVerticalGlue());
JPanel internalPanel = new JPanel(new BorderLayout());
internalPanel.add(keywordsScrollPane, BorderLayout.CENTER);
internalPanel.add(box, BorderLayout.EAST);
But as you can see, it looks sloppy because my buttons are different sizes:
I decided to switch to GridBagLayout so I can utilize GridBagConstraints.fill. This approach fixes my button width issue, but I cannot figure out how to vertically center the buttons. I changed the grid size and placed the buttons in the middle three rows, but the buttons were still appearing at the top of the panel. I tried making use of GridBagConstraints.anchor and GridBagConstraints.weighty as well. The latter almost worked, but there are very large margins between the buttons:
I am looking for the buttons to be grouped together as they were in my Box approach. How can I achieve this with a GridBadLayout?
I am using a class I created called ConstraintsBuilder which works exactly as you would expect. It's for creating GridBagContraints with nice one-liners. Here is all the (relevant) code for your viewing pleasure:
public class KeywordsDialog extends JDialog implements ActionListener, ListSelectionListener {
private JList<String> keywords;
private JScrollPane keywordsScrollPane;
private JButton add;
private JButton remove;
private JButton edit;
private Set<String> keywordsList;
public KeywordsDialog(Window parent, Collection<String> keywordsList) {
super(parent);
this.keywordsList = keywordsList == null ? new HashSet<String>() : new HashSet<String>(keywordsList);
if (keywordsList != null && !keywordsList.isEmpty()) {
this.keywords = new JList<String>(toListModel(keywordsList));
} else {
this.keywords = new JList<String>(new DefaultListModel<String>());
}
this.keywordsScrollPane = new JScrollPane(keywords);
this.add = new JButton("Add");
this.remove = new JButton("Remove");
this.edit = new JButton("Edit");
this.edit.setEnabled(false);
this.add.setEnabled(false);
ConstraintsBuilder builder = LayoutUtils.gridBagConstraintsBuilder();
JPanel internalPanel = new JPanel(new GridBagLayout());
internalPanel.add(this.keywordsScrollPane, builder.gridX(0).gridY(0).gridHeight(3).margins(0, 0, 0, 5)
.fill(GridBagConstraints.BOTH).weightX(1D).weightY(1D).build());
internalPanel.add(this.add,
builder.reset().gridX(1).gridY(0).fill(GridBagConstraints.HORIZONTAL).weightX(1D).weightY(1D).build());
internalPanel.add(this.remove,
builder.reset().gridX(1).gridY(1).fill(GridBagConstraints.HORIZONTAL).weightX(1D).weightY(1D).build());
internalPanel.add(this.edit,
builder.reset().gridX(1).gridY(2).fill(GridBagConstraints.HORIZONTAL).weightX(1D).weightY(1D).build());
this.keywords.setBorder(BorderFactory.createTitledBorder("Keywords"));
internalPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.setLayout(new BorderLayout());
this.add(internalPanel, BorderLayout.CENTER);
Dimension screen = GuiHelper.getScreenSize(parent);
this.setSize((int) (screen.getWidth() / 4), (int) (screen.getHeight() / 3));
this.setLocationRelativeTo(parent);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
// ...
}
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the How to Use GridBagLayout section.
The easiest way to create this GUI is to treat the JTextArea separately from the JButton area.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ExampleGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ExampleGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("Example GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTextArea(), BorderLayout.CENTER);
frame.add(createButtonPanel(), BorderLayout.EAST);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createTextArea() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JTextArea textArea = new JTextArea(10, 30);
textArea.setText("keyword");
panel.add(textArea, BorderLayout.CENTER);
return new JScrollPane(panel);
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 5, 5, 5);
gbc.gridy = 0;
JButton button = new JButton("Add");
panel.add(button, gbc);
gbc.gridy++;
button = new JButton("Remove");
panel.add(button, gbc);
gbc.gridy++;
button = new JButton("Edit");
panel.add(button, gbc);
return panel;
}
}
I would make the GUI simpler. Put the three buttons into a JPanel that uses a GridLayout, one declared to use 1 column and variable number of rows, one with a desired spacing between buttons, here, 5 pixels: JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5)); and then put that JPanel into the center of a another JPanel, and GridBagLayout without constraints works well for this:
JPanel sidePanel = new JPanel(new GridBagLayout());
sidePanel.add(buttonPanel);
and put that JPanel into the right side of a border layout using JPanel. For example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FooSwing01 extends JPanel {
public FooSwing01() {
JTextArea textArea = new JTextArea(20, 50);
JScrollPane scrollPane = new JScrollPane(textArea);
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
int maxButtons = 3;
for (int i = 0; i < maxButtons; i++) {
buttonPanel.add(new JButton("Button " + (i + 1)));
}
JPanel sidePanel = new JPanel(new GridBagLayout());
sidePanel.add(buttonPanel);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(5, 5));
add(scrollPane);
add(sidePanel, BorderLayout.LINE_END);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("GUI");
frame.add(new FooSwing01());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
I am making a form by using FlowLayout. The GUI of the form is shown below:
Name: <textField>
Age: <textfiedl>
Gender: Male[] Female[]
Email: <textField>
For gender, the options are radio buttons. As you can see, the labels for age and gender should be centered. How can I do this using FlowLayout?
This is one way (of a plethora of ways it might be done). Layout details in titled borders.
Note: For anything to do with selecting a number, it's better to offer the user a spinner.
If the whole form is created using BoxLayout, one oriented as BoxLayout.PAGE_AXIS, then if each "line" of the form is a JPanel that uses FlowLayout, the components that they held should be centered by default, since JPanel uses new FlowLayout(FlowLayout.CENTER, 5, 5) as its default layout (actually it uses new FlowLayout(), but the default values for this constructor are as above -- CENTER orientation with horizontal and vertical gaps of 5).
Another option is to use a GridBagLayout, and alter the constraints' FILL and weighty properties for the component on each row.
For example:
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
public class LayoutExample extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField ageField = new JTextField(10);
private JTextField emailField = new JTextField(10);
private ButtonGroup sexBtnGroup = new ButtonGroup();
private JRadioButton maleBtn = new JRadioButton("Male");
private JRadioButton femaleBtn = new JRadioButton("Female");
public LayoutExample() {
JPanel agePanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
agePanel.add(new JLabel("Age:"));
agePanel.add(ageField);
sexBtnGroup.add(maleBtn);
sexBtnGroup.add(femaleBtn);;
JPanel sexSelectionPanel = new JPanel();
sexSelectionPanel.add(new JLabel("Select Sex:"));
sexSelectionPanel.add(maleBtn);
sexSelectionPanel.add(femaleBtn);
JPanel emailPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
emailPanel.add(new JLabel("Email:"));
emailPanel.add(emailField);
int gap = 10;
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(agePanel);
add(Box.createVerticalStrut(5));
add(sexSelectionPanel);
add(Box.createVerticalStrut(5));
add(emailPanel);
}
// make it stretch horizontally to see placement
#Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
int width = (3 * superSize.width) / 2;
int height = superSize.height;
return new Dimension(width, height);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
LayoutExample mainPanel = new LayoutExample();
JFrame frame = new JFrame("LayoutExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Example 2 using a GridBagLayout:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
public class LayoutExample2 extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField ageField = new JTextField(10);
private JTextField emailField = new JTextField(10);
private ButtonGroup sexBtnGroup = new ButtonGroup();
private JRadioButton maleBtn = new JRadioButton("Male");
private JRadioButton femaleBtn = new JRadioButton("Female");
public LayoutExample2() {
JPanel sexSelectPanel = new JPanel(new GridLayout(1, 0, 5, 5));
sexBtnGroup.add(maleBtn);
sexSelectPanel.add(maleBtn);
sexBtnGroup.add(femaleBtn);
sexSelectPanel.add(femaleBtn);
int gap = 10;
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
setLayout(new GridBagLayout());
add(new JLabel("Age:"), createGbc(0, 0));
add(ageField, createGbc(1, 0));
add(new JLabel("Sex:"), createGbc(0, 1));
add(sexSelectPanel, createGbc(1, 1));
add(new JLabel("Email:"), createGbc(0, 2));
add(emailField, createGbc(1, 2));
}
private static GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
int ins = 4;
gbc.insets = new Insets(ins, ins, ins, ins);
gbc.fill = GridBagConstraints.HORIZONTAL;
return gbc;
}
// make it stretch horizontally to see placement
#Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
int width = (4 * superSize.width) / 3;
int height = superSize.height;
return new Dimension(width, height);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
LayoutExample2 mainPanel = new LayoutExample2();
JFrame frame = new JFrame("LayoutExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
I have the GUI displaying properly for the most part, except for one thing. The TitledBorder("Numeric Type") does not display the entire title for the right JPanel. I believe it has something to do with the BorderLayout Manager. Instead of displaying "Numeric Type" within the border, just "Numeric..." displays. Any help will be greatly appreciated.
public class P3GUI extends JFrame {
private JLabel originalList;
private JTextField originalSort;
private JLabel sortedList;
private JTextField newSort;
private JPanel panel;
private JButton performSort;
private JRadioButton ascending;
private JRadioButton descending;
private ButtonGroup sort;
private JRadioButton integer;
private JRadioButton fraction;
private ButtonGroup numType;
private JPanel inputPanel, outputPanel, calculatePanel, radioPanel;
private JPanel left, right;
public P3GUI () {
super("Binary Search Tree Sort");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
originalList = new JLabel("Original List");
originalSort = new JTextField(20);
inputPanel = new JPanel();
inputPanel.add(originalList);
inputPanel.add(originalSort);
sortedList = new JLabel("Sorted List");
newSort = new JTextField(20);
newSort.setEditable(false);
outputPanel = new JPanel();
outputPanel.add(sortedList);
outputPanel.add(newSort);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(inputPanel);
panel.add(outputPanel);
add(panel, BorderLayout.NORTH);
performSort = new JButton("Perform Sort");
calculatePanel = new JPanel();
calculatePanel.add(performSort);
add(calculatePanel, BorderLayout.CENTER);
ascending = new JRadioButton("Ascending");
descending = new JRadioButton("Descending");
sort = new ButtonGroup();
sort.add(ascending);
sort.add(descending);
integer = new JRadioButton("Integer");
fraction = new JRadioButton("Fraction");
numType = new ButtonGroup();
numType.add(integer);
numType.add(fraction);
radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout());
left = new JPanel();
left.setLayout(new GridLayout(2,1));
left.setBorder(BorderFactory.createTitledBorder("Sort Order"));
left.add(ascending);
left.add(descending);
right = new JPanel();
right.setLayout(new GridLayout(2,1));
right.setBorder(BorderFactory.createTitledBorder("Numeric Type"));
right.add(integer);
right.add(fraction);
radioPanel.add(left);
radioPanel.add(right);
add(radioPanel, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new P3GUI().setVisible(true);
}
});
}
}
The problem is that the right JPanel is too small to display the entire title, and so it gets truncated. I'd suggest placing the bottom two JPanels into another that uses GridLayout, and then place them in such a way that they expand to fit the bottom of your GUI. When spread out, the title has a much greater chance of being fully displayed (but not a guarantee, mind you!).
For example, if you make the main GUI use a BorderLayout, and add this GridLayout using JPanel into the BorderLayout.CENTER position, it will fill it completely. Then the top components, the TextFields and JButton can be added to another JPanel, say one that uses a GridBagLayout, and add that to the main JPanel's BorderLayout.PAGE_START position.
For example, the following code produces this GUI:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class P3GUI2 extends JPanel {
private static final int COLS = 20;
private JTextField originalSort = new JTextField(COLS);
private JTextField newSort = new JTextField(COLS);
private JButton performSort = new JButton("Perform Sort");
private JRadioButton ascending = new JRadioButton("Ascending");
private JRadioButton descending = new JRadioButton("Descending");
private ButtonGroup sort = new ButtonGroup();
private JRadioButton integer = new JRadioButton("Integer");
private JRadioButton fraction = new JRadioButton("Fraction");
private ButtonGroup numType = new ButtonGroup();
public P3GUI2() {
JPanel topPanel = new JPanel(new GridBagLayout());
topPanel.add(new JLabel("Original List:"), createGbc(0, 0));
topPanel.add(originalSort, createGbc(1, 0));
topPanel.add(new JLabel("Sorted List:"), createGbc(0, 1));
topPanel.add(newSort, createGbc(1, 1));
performSort.setMnemonic(KeyEvent.VK_P);
JPanel btnPanel = new JPanel();
btnPanel.add(performSort);
JPanel sortOrderPanel = createTitlePanel("Sort Order");
JPanel numericPanel = createTitlePanel("Numeric Type");
sortOrderPanel.add(ascending);
sortOrderPanel.add(descending);
sort.add(ascending);
sort.add(descending);
numericPanel.add(integer);
numericPanel.add(fraction);
numType.add(integer);
numType.add(fraction);
JPanel radioPanels = new JPanel(new GridLayout(1, 0, 3, 3));
radioPanels.add(sortOrderPanel);
radioPanels.add(numericPanel);
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout(3, 3));
add(topPanel, BorderLayout.PAGE_START);
add(btnPanel, BorderLayout.CENTER);
add(radioPanels, BorderLayout.PAGE_END);
}
private JPanel createTitlePanel(String title) {
JPanel panel = new JPanel(new GridLayout(0, 1, 3, 3));
panel.setBorder(BorderFactory.createTitledBorder(title));
return panel;
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.insets = new Insets(3, 3, 3, 3);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
return gbc;
}
private static void createAndShowGui() {
P3GUI2 mainPanel = new P3GUI2();
JFrame frame = new JFrame("Binary Search Tree Sort");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Or you could place the above btnPanel into the main one BorderLayout.CENTER and then place the radioPanels into the main one BorderLayout.PAGE_END. This will display a GUI of the same appearance but it will expand differently if re-sized.
The preferred size of the panel (as determined by the layout manager) does not consider the size of the text in the TitledBorder so the title can get truncated.
Here is a custom JPanel that can be used with a TitleBorder. The getPreferredSize() method has been customized to use the maximum width of:
the default getPreferredSize() calculation
the width of the text in the TitledBorder
Here is a simple example:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TitledBorderPanel extends JPanel
{
/**
** The preferred width on the panel must consider the width of the text
** used on the TitledBorder
*/
#Override
public Dimension getPreferredSize()
{
Dimension preferredSize = super.getPreferredSize();
Border border = getBorder();
int borderWidth = 0;
if (border instanceof TitledBorder)
{
Insets insets = getInsets();
TitledBorder titledBorder = (TitledBorder)border;
borderWidth = titledBorder.getMinimumSize(this).width + insets.left + insets.right;
}
int preferredWidth = Math.max(preferredSize.width, borderWidth);
return new Dimension(preferredWidth, preferredSize.height);
}
private static void createAndShowGUI()
{
JPanel panel = new TitledBorderPanel();
panel.setBorder( BorderFactory.createTitledBorder("File Options Command List:") );
panel.setLayout( new BoxLayout(panel, BoxLayout.Y_AXIS) );
panel.add( new JLabel("Open") );
panel.add( new JLabel("Close") );
// panel.add( new JLabel("A really wierd file option longer than border title") );
JFrame frame = new JFrame("TitledBorderPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
Been trying to make 4 JPanels two on top and other 2 on the bottom and manipulate each independently of each other inputing text etc...beginner with java so if anybody can help me out that would be awesome. I posted another question before but now I am stuck again. lol
package project2;
import javax.swing.JOptionPane;
import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.JPanel; // Displays a Panel
import javax.swing.SwingConstants; // common constants used with Swing
import javax.swing.Icon; // interface used to manipulate images
import javax.swing.ImageIcon; // loads images
import java.awt.Color;
import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Container;
import java.awt.Dimension;
public class GUI
{
private JPanel Checks; //Panel to Hold Checks
private JPanel Transactions;
private JPanel History;
private JPanel Graphics;
private JLabel CLabel;
public void displayGUI()
{
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(
new GridLayout(2, 2, 5, 5));
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
Checks = getPanel(Color.YELLOW.darker().darker());
Transactions = getPanel(Color.RED);
History = getPanel(Color.GREEN.darker().darker());
Graphics = getPanel(Color.MAGENTA);
CLabel = new JLabel("Label with on red text");
contentPane.add(Checks);
contentPane.add(Transactions);
contentPane.add(History);
contentPane.add(Graphics);
frame.setSize(1400,690); //set frame size
frame.setVisible(true); //display frame
frame.setContentPane(contentPane);
}
private JPanel getPanel(Color bColor) {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(bColor);
return panel;
}
Expected Output :
Please have a look at this answer, and see if you need this or something else. Do let us know, we might try to add a bit more deeper.
import java.awt.*;
import java.util.Random;
import javax.swing.*;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 9/30/13
* Time: 6:02 PM
* To change this template use File | Settings | File Templates.
*/
public class UTBInCheck {
private JPanel contentPane;
private JPanel topLeftPanel;
private GridBagConstraints gbc;
private JTextArea infoArea;
private JScrollPane infoScroller;
private JTextField payOrderField;
private JTextField amountField;
private JButton orderSubmitButton;
private JButton orderClearButton;
private JPanel topRightPanel;
private JTextArea recentTransArea;
private JScrollPane recentTransScroller;
private JButton updateButton;
private JPanel bottomLeftPanel;
private JTextField atmWithdrawalField;
private JTextField atmDepositField;
private JTextField directDepositField;
private JTextField wireTransferField;
private JTextField electronicBillField;
private JTextField bankFeeField;
private JButton etSubmitButton;
private JButton etClearButton;
private JPanel bottomRightPanel;
private Random random;
private static final int GAP = 5;
private int checkNumber;
public UTBInCheck() {
random = new Random();
gbc = new GridBagConstraints();
gbc.insets = new Insets(GAP, GAP, GAP, GAP);
checkNumber = 811;
}
private void displayGUI() {
JFrame frame = new JFrame("University of Texas at Brownsville");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = getPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
contentPane.setLayout(new GridLayout(2, 2, GAP, GAP));
topLeftPanel = getPanel();
setTopLeftPanel(topLeftPanel);
topRightPanel = getPanel();
setTopRightPanel(topRightPanel);
bottomLeftPanel = getPanel();
setBottomLeftPanel(bottomLeftPanel);
bottomRightPanel = getPanel();
contentPane.add(topLeftPanel);
contentPane.add(topRightPanel);
contentPane.add(bottomLeftPanel);
contentPane.add(bottomRightPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void setTopLeftPanel(JPanel panel) {
panel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
panel.setLayout(new BorderLayout(GAP, GAP));
JPanel topPanel = getPanel();
topPanel.setLayout(new GridBagLayout());
JLabel universityLabel = getLabel("University of Texas at Brownsville");
addComp(topPanel, universityLabel, 0, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST);
JLabel checkNumberLabel = getLabel("Check Number : " + checkNumber);
addComp(topPanel, checkNumberLabel, 1, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST);
infoArea = new JTextArea(5, 10);
infoArea.setLineWrap(true);
infoArea.setWrapStyleWord(true);
infoScroller = new JScrollPane();
infoScroller.setViewportView(infoArea);
addComp(topPanel, infoScroller, 0, 1, 2, 1, 1.0, 0.4,
GridBagConstraints.BOTH, GridBagConstraints.WEST);
JPanel centerPanel = getPanel();
centerPanel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
centerPanel.setLayout(new GridLayout(0, 2, GAP, GAP));
JLabel payOrderLabel = getLabel("Pay to the order of : ");
payOrderField = new JTextField(10);
JLabel amountLabel = getLabel("Amount : ");
amountField = new JTextField(10);
JLabel chrisBankLabel = getLabel("Cris' Bank");
JPanel buttonPanel = getPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
orderSubmitButton = new JButton("Submit");
orderClearButton = new JButton("Clear");
buttonPanel.add(orderSubmitButton);
buttonPanel.add(orderClearButton);
centerPanel.add(payOrderLabel);
centerPanel.add(payOrderField);
centerPanel.add(amountLabel);
centerPanel.add(amountField);
centerPanel.add(chrisBankLabel);
centerPanel.add(buttonPanel);
addComp(topPanel, centerPanel, 0, 2, 2, 1, 1.0, 0.4,
GridBagConstraints.BOTH, GridBagConstraints.SOUTHWEST);
panel.add(topPanel, BorderLayout.CENTER);
}
private void addComp(JPanel panel, JComponent comp,
int gridx, int gridy, int gridwidth, int gridheight,
double weightx, double weighty, int fill, int anchor) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
panel.add(comp, gbc);
}
private void setTopRightPanel(JPanel panel) {
panel.setBorder(BorderFactory.createTitledBorder("Recent Transactions"));
panel.setLayout(new BorderLayout(GAP, GAP));
recentTransArea = new JTextArea(10, 10);
recentTransArea.setLineWrap(true);
recentTransArea.setWrapStyleWord(true);
recentTransScroller = new JScrollPane();
recentTransScroller.setViewportView(recentTransArea);
JPanel buttonPanel = getPanel();
updateButton = new JButton("Update");
buttonPanel.add(updateButton);
panel.add(recentTransScroller, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.PAGE_END);
}
private void setBottomLeftPanel(JPanel panel) {
panel.setBorder(
BorderFactory.createTitledBorder("Electronic Transactions"));
panel.setLayout(new GridLayout(0, 2, GAP, GAP));
JLabel atmWithdrawLabel = getLabel("ATM Withdrawals : ");
atmWithdrawalField = new JTextField(10);
JLabel atmdepositLabel = getLabel("ATM Deposit : ");
atmDepositField = new JTextField(10);
JLabel directDepositLabel = getLabel("Direct Deposit : ");
directDepositField = new JTextField(10);
JLabel wireTransferLabel = getLabel("Wire Transfers : ");
wireTransferField = new JTextField(10);
JLabel electronicBillLabel = getLabel("Electronic Bills : ");
electronicBillField = new JTextField(10);
JLabel bankFeeLabel = getLabel("Bank Fees : ");
bankFeeField = new JTextField(10);
JPanel buttonPanel = getPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
etSubmitButton = new JButton("Submit");
etClearButton = new JButton("Clear");
buttonPanel.add(etSubmitButton);
buttonPanel.add(etClearButton);
panel.add(atmWithdrawLabel);
panel.add(atmWithdrawalField);
panel.add(atmdepositLabel);
panel.add(atmDepositField);
panel.add(directDepositLabel);
panel.add(directDepositField);
panel.add(wireTransferLabel);
panel.add(wireTransferField);
panel.add(electronicBillLabel);
panel.add(electronicBillField);
panel.add(bankFeeLabel);
panel.add(bankFeeField);
//Adding JPanel just to occupy space;
panel.add(getPanel());
panel.add(buttonPanel);
}
private JLabel getLabel(String text) {
return (new JLabel(text, JLabel.LEFT));
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(getRandomColor());
/*panel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));*/
return panel;
}
private Color getRandomColor() {
return (new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new UTBInCheck().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
OUTPUT :
A GUI with no white space appears 'crowded'. How can I provide white space without resorting to explicitly setting the position or size of components?
Using various LayoutManagers one can provide spacing between various components.
1.) BorderLayout :
Overloaded Constructor : BorderLayout(int horizontalGap, int verticalGap)
Getter and setter methods
For Horizontal Spacing : BorderLayout.getHgap() and BorderLayout.setHgap(int hgap)
For Vertical Spacing : BorderLayout.getVgap() and BorderLayout.setVgap()
2.) FlowLayout :
Overloaded Constructor : FlowLayout(int align, int hgap, int vgap)
Getter and setter methods
For Horizontal Spacing : FlowLayout.getHgap() and FlowLayout.setHgap(int hgap)
For Vertical Spacing : FlowLayout.getVgap() and FlowLayout.setVgap()
3.) GridLayout :
Overloaded Constructor : GridLayout(int rows, int columns, int hgap, int vgap)
Getter and setter methods
For Horizontal Spacing : GridLayout.getHgap() and GridLayout.setHgap(int hgap)
For Vertical Spacing : GridLayout.getVgap() and GridLayout.setVgap()
4.) GridBagLayout :
GridBagConstraints.insets
5.) CardLayout (example) :
CardLayout(int hGap, int vGap)
Example to display all constructors in action :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutExample {
private final int hGap = 5;
private final int vGap = 5;
private String[] borderConstraints = {
BorderLayout.PAGE_START,
BorderLayout.LINE_START,
BorderLayout.CENTER,
BorderLayout.LINE_END,
BorderLayout.PAGE_END
};
private JButton[] buttons;
private GridBagConstraints gbc;
private JPanel borderPanel;
private JPanel flowPanel;
private JPanel gridPanel;
private JPanel gridBagPanel;
private JPanel cardPanel;
public LayoutExample() {
buttons = new JButton[16];
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(hGap, vGap, hGap, vGap);
}
private void displayGUI() {
JFrame frame = new JFrame("Layout Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(
new GridLayout(0, 1, hGap, vGap));
contentPane.setBorder(
BorderFactory.createEmptyBorder(hGap, vGap, hGap, vGap));
borderPanel = new JPanel(new BorderLayout(hGap, vGap));
borderPanel.setBorder(
BorderFactory.createTitledBorder("BorderLayout"));
borderPanel.setOpaque(true);
borderPanel.setBackground(Color.WHITE);
for (int i = 0; i < 5; i++) {
buttons[i] = new JButton(borderConstraints[i]);
borderPanel.add(buttons[i], borderConstraints[i]);
}
contentPane.add(borderPanel);
flowPanel = new JPanel(new FlowLayout(
FlowLayout.CENTER, hGap, vGap));
flowPanel.setBorder(
BorderFactory.createTitledBorder("FlowLayout"));
flowPanel.setOpaque(true);
flowPanel.setBackground(Color.WHITE);
for (int i = 5; i < 8; i++) {
buttons[i] = new JButton(Integer.toString(i));
flowPanel.add(buttons[i]);
}
contentPane.add(flowPanel);
gridPanel = new JPanel(new GridLayout(2, 2, hGap, vGap));
gridPanel.setBorder(
BorderFactory.createTitledBorder("GridLayout"));
gridPanel.setOpaque(true);
gridPanel.setBackground(Color.WHITE);
for (int i = 8; i < 12; i++) {
buttons[i] = new JButton(Integer.toString(i));
gridPanel.add(buttons[i]);
}
contentPane.add(gridPanel);
gridBagPanel = new JPanel(new GridBagLayout());
gridBagPanel.setBorder(
BorderFactory.createTitledBorder("GridBagLayout"));
gridBagPanel.setOpaque(true);
gridBagPanel.setBackground(Color.WHITE);
buttons[12] = new JButton(Integer.toString(12));
addComp(gridBagPanel, buttons[12], 0, 0, 1, 1
, GridBagConstraints.BOTH, 0.33, 0.5);
buttons[13] = new JButton(Integer.toString(13));
addComp(gridBagPanel, buttons[13], 1, 0, 1, 1
, GridBagConstraints.BOTH, 0.33, 0.5);
buttons[14] = new JButton(Integer.toString(14));
addComp(gridBagPanel, buttons[14], 0, 1, 2, 1
, GridBagConstraints.BOTH, 0.66, 0.5);
buttons[15] = new JButton(Integer.toString(15));
addComp(gridBagPanel, buttons[15], 2, 0, 1, 2
, GridBagConstraints.BOTH, 0.33, 1.0);
contentPane.add(gridBagPanel);
cardPanel = new JPanel(new CardLayout(hGap, vGap));
cardPanel.setBorder(
BorderFactory.createTitledBorder("CardLayout"));
cardPanel.setOpaque(true);
cardPanel.setBackground(Color.WHITE);
cardPanel.add(getPanel(Color.BLUE));
cardPanel.add(getPanel(Color.GREEN));
contentPane.add(cardPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getPanel(Color bColor) {
JPanel panel = new JPanel(new FlowLayout(
FlowLayout.CENTER, hGap, vGap));
panel.setOpaque(true);
panel.setBackground(bColor.darker().darker());
JButton swapperButton = new JButton("Next");
swapperButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
CardLayout cardLayout = (CardLayout) cardPanel.getLayout();
cardLayout.next(cardPanel);
}
});
panel.add(swapperButton);
return panel;
}
private void addComp(JPanel panel, JComponent comp
, int x, int y, int gWidth
, int gHeight, int fill
, double weightx, double weighty) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = gWidth;
gbc.gridheight = gHeight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
public static void main(String[] args) {
Runnable runnable = new Runnable(){
#Override
public void run() {
new LayoutExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
OUTPUT :
There are a number of ways in a Swing GUI to provide a separation between components, and white space around components:
JToolBar has the methods addSeparator() & addSeparator(Dimension).
JMenu uses a spacing component better suited to menus, available through addSeparator().
But more generally, look to:
The spacing as can be defined in the layout constructors.
Borders.
Here is an example of using the layout separator hGap & vGap values & borders (specifically an EmptyBorder) to provide 'white' (actually shown as red to make it very obvious) space. Adjust the spinners to see the result.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
public class WhiteSpace {
private JPanel gui = null;
private BorderLayout mainLayout =
new BorderLayout(0, 0);
private final FlowLayout buttonLayout =
new FlowLayout(FlowLayout.CENTER, 0, 0);
private final JPanel buttonPanel = new JPanel(buttonLayout);
private final SpinnerNumberModel hModel =
new SpinnerNumberModel(0, 0, 15, 1);
private final SpinnerNumberModel vModel =
new SpinnerNumberModel(0, 0, 15, 1);
private final SpinnerNumberModel hBorderModel =
new SpinnerNumberModel(0, 0, 15, 1);
private final SpinnerNumberModel vBorderModel =
new SpinnerNumberModel(0, 0, 15, 1);
private ChangeListener changeListener;
public Container getGui() {
if (gui == null) {
gui = new JPanel(mainLayout);
gui.setBackground(Color.RED);
JTree tree = new JTree();
tree.setVisibleRowCount(10);
for (int ii = tree.getRowCount(); ii > -1; ii--) {
tree.expandRow(ii);
}
gui.add(new JScrollPane(
tree,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
BorderLayout.LINE_START);
gui.add(new JScrollPane(new JTextArea(10, 30)));
gui.add(buttonPanel, BorderLayout.PAGE_START);
changeListener = (ChangeEvent e) -> {
int hGap = hModel.getNumber().intValue();
int vGap = vModel.getNumber().intValue();
int hBorder = hBorderModel.getNumber().intValue();
int vBorder = vBorderModel.getNumber().intValue();
adjustWhiteSpace(hGap, vGap, hBorder, vBorder);
};
addModel("H Gap", hModel);
addModel("V Gap", vModel);
addModel("H Border", hBorderModel);
addModel("V Border", vBorderModel);
}
return gui;
}
private void addModel(String label, SpinnerNumberModel model) {
buttonPanel.add(new JLabel(label));
final JSpinner spinner = new JSpinner(model);
spinner.addChangeListener(changeListener);
buttonPanel.add(spinner);
}
private void adjustWhiteSpace(
int hGap, int vGap, int hBorder, int vBorder) {
mainLayout.setHgap(hGap);
mainLayout.setVgap(vGap);
buttonLayout.setHgap(hGap);
gui.setBorder(new EmptyBorder
(vBorder, hBorder, vBorder, hBorder));
Container c = gui.getTopLevelAncestor();
if (c instanceof Window) {
Window w = (Window) c;
w.pack();
}
}
public static void main(String[] args) {
Runnable r = () -> {
WhiteSpace ws = new WhiteSpace();
Container gui1 = ws.getGui();
JFrame f = new JFrame("White (OK Red) Space");
f.add(gui1);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setResizable(false);
f.pack();
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}
When you use BoxLayout, Box.createVerticalGlue() method can help you to make some white space.
Another method is BorderFactory.createEmptyBorder(int top, int left, int bottom, int right). It can help you to make some white space around component.
Thanks for Andrew Thompson's remind.I've revised BoxLayout in recent days and I find that Box.createVerticalGlue() can add some white space depend on the panel's size and you can not set the explicit pixel value of the length of white space.But Box.createVerticalStrut() can do that. Here is a MCTaRE and show the effect of those two methods.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class WhiteSpace extends JFrame{
static WhiteSpace whiteSpace;
DemoPanel demoPanel;
boolean withGlue;
JSpinner spinner;
public WhiteSpace(){
initialWindow();
demoPanel = new DemoPanel();
ActionPanel actionPanel = new ActionPanel();
setLayout(new BorderLayout());
getContentPane().add(actionPanel,BorderLayout.NORTH);
getContentPane().add(demoPanel,BorderLayout.CENTER);
setVisible(true);
}
public void initialWindow(){
setSize(220, 300);
setTitle("White Space");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//Show the window in the middle of the screen
}
/**
* #param args
*/
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
whiteSpace = new WhiteSpace();
}
};
SwingUtilities.invokeLater(runnable);
}
class DemoPanel extends JPanel{
//Show the vertical white space between label1 and label2
JLabel label1;
JLabel label2;
public void initialDemoPanel(){
setBorder(BorderFactory.createTitledBorder(getBorder(), "DemoPanel", TitledBorder.LEADING, TitledBorder.TOP, new Font("Default",Font.PLAIN,10), Color.gray));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
label1 = new JLabel("This is first line");
label2 = new JLabel("This is second line");
}
public DemoPanel(){
initialDemoPanel();
add(label1);
if(withGlue){
add(Box.createVerticalGlue());
}
add(label2);
}
public DemoPanel(int strutValue){
initialDemoPanel();
add(label1);
add(Box.createVerticalStrut(strutValue));
add(label2);
}
}
class ActionPanel extends JPanel{
public ActionPanel(){
setBorder(BorderFactory.createTitledBorder(getBorder(), "ActionPanel", TitledBorder.LEADING, TitledBorder.TOP, new Font("Default",Font.PLAIN,10), Color.gray));
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
JRadioButton glueButton = new JRadioButton("With Glue");
glueButton.addActionListener(new glueButtonListener());
add(glueButton);
add(Box.createHorizontalStrut(10));
//To create horizontal white space
JLabel strutLabel = new JLabel("Strut Value");
add(strutLabel);
spinner = new JSpinner(new SpinnerNumberModel(0,0,50,1));
spinner.addChangeListener(new spinnerListener());
add(spinner);
//public SpinnerNumberModel(Number value,Comparable minimum,Comparable maximum,Number stepSize)
}
}
class glueButtonListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
spinner.setValue(new Integer(0));
withGlue = (withGlue == true ? false:true);
whiteSpace.getContentPane().remove(demoPanel);
demoPanel = new DemoPanel();
whiteSpace.getContentPane().add(demoPanel,BorderLayout.CENTER);
whiteSpace.getContentPane().validate();
}
}
class spinnerListener implements ChangeListener{
#Override
public void stateChanged(ChangeEvent e) {
int strutValue = (Integer) spinner.getValue();
whiteSpace.getContentPane().remove(demoPanel);
demoPanel = new DemoPanel(strutValue);
whiteSpace.getContentPane().add(demoPanel,BorderLayout.CENTER);
whiteSpace.getContentPane().validate();
}
}
}
Box.createHorizontalGlue() and Box.createHorizontalStrut(int height) can be used too. Besides, Box.createRigidArea(Dimension d) has the ability too create white space too.
MigLayout has multiple ways of creating space. (A space is called a gap in this layout.)
Gaps can be created at the highest level with layout constraints, it is possible to
create gaps between rows and column and gaps can be also set between individual
components with component constraints. There are also specific gaps around the borders
of a container called insets which have their own specific keyword to be set.
The following example creates all these kinds of gaps:
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutGaps2 extends JFrame {
public MigLayoutGaps2() {
initUI();
setTitle("Gaps");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
JPanel base = new JPanel(new MigLayout("flowy, ins 30, gap 15"));
setContentPane(base);
JPanel pnl1 = new JPanel();
pnl1.setBorder(
BorderFactory.createTitledBorder("Grid gaps")
);
pnl1.setLayout(new MigLayout("gap 5 5, ins 10, wrap 3"));
pnl1.add(new JButton("1"));
pnl1.add(new JButton("2"));
pnl1.add(new JButton("3"));
pnl1.add(new JButton("4"));
pnl1.add(new JButton("5"));
pnl1.add(new JButton("6"));
JPanel pnl2 = new JPanel();
pnl2.setBorder(
BorderFactory.createTitledBorder("Column gaps")
);
pnl2.setLayout(new MigLayout("wrap 3", "[]10[]"));
JLabel lbl1 = new JLabel();
lbl1.setBorder(
BorderFactory.createEtchedBorder()
);
JLabel lbl2 = new JLabel();
lbl2.setBorder(
BorderFactory.createEtchedBorder()
);
JLabel lbl3 = new JLabel();
lbl3.setBorder(
BorderFactory.createEtchedBorder()
);
pnl2.add(lbl1, "w 40, h 110");
pnl2.add(lbl2, "w 40, h 110");
pnl2.add(lbl3, "w 40, h 110");
JPanel pnl3 = new JPanel();
pnl3.setBorder(
BorderFactory.createTitledBorder("Row gaps")
);
pnl3.setLayout(new MigLayout("wrap", "", "[]15[]"));
JLabel lbl4 = new JLabel();
lbl4.setBorder(
BorderFactory.createEtchedBorder()
);
JLabel lbl5 = new JLabel();
lbl5.setBorder(
BorderFactory.createEtchedBorder()
);
JLabel lbl6 = new JLabel();
lbl6.setBorder(
BorderFactory.createEtchedBorder()
);
pnl3.add(lbl4, "w 150, h 20");
pnl3.add(lbl5, "w 150, h 20");
pnl3.add(lbl6, "w 150, h 20");
JPanel pnl4 = new JPanel();
pnl4.setBorder(
BorderFactory.createTitledBorder("Component gaps")
);
pnl4.setLayout(new MigLayout());
pnl4.add(new JLabel("Name:"), "gapright 5");
pnl4.add(new JTextField(10), "gapbottom 20, gaptop 20");
base.add(pnl1);
base.add(pnl2);
base.add(pnl3);
base.add(pnl4);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutGaps2 ex = new MigLayoutGaps2();
ex.setVisible(true);
}
});
}
}
We have four panels in the layout. Each of this panels has a MigLayout manager.
JPanel base = new JPanel(new MigLayout("flowy, ins 30, gap 15"));
This line creates container insets and vertical gaps between panels.
pnl1.setLayout(new MigLayout("gap 5 5, ins 10, wrap 3"));
Here we apply gaps for the whole grid structure and also set container gaps.
pnl2.setLayout(new MigLayout("wrap 3", "[]10[]"));
This line creates gaps between columns.
pnl3.setLayout(new MigLayout("wrap", "", "[]15[]"));
Row gaps are defined with this code.
pnl4.add(new JLabel("Name:"), "gapright 5");
pnl4.add(new JTextField(10), "gapbottom 20, gaptop 20");
Finally, it is possible to create gaps between individual components.
JGoodies FormLayout.
Author Karsten Lentzsch has a collection of presentations on UI design. In particular this PDF speaks to the need for aesthetic whitespace. Adding meaningful space while also paying attention to clutter separates the wheat from the chaff.
Whenever I have this issue, I just use JPanels. For example in a GridLayout:
JFrame frame = new JFrame;
frame.setLayout(new GridLayout(2, 0));
//We want the bottom left to be blank
frame.add(new JLabel("Top Left"));
frame.add(new JLabel("Top Right"));
//This is the position we want empty
frame.add(new JPanel());
//Now we can continue with the rest of the script