Small GUI issue I cannot fix. JTextFields - java

Small error I can't manage to do. So right now my program GUI looks like this:
Now there is a TextField under the 'Mark' column were the user can input their data. I also want the same for the weight section were I want to insert a TextField right under 'Weight' column.
However when I try and put in a TextField, both the the Textfields turn like this when the window is small:
and this when the window is enlarged:
How can I make it so that there is a textfield under Mark AND Weight?
Code:
public class Gradeanalysis implements ActionListener {
public void actionPerformed (ActionEvent e){
GridBagConstraints gbc = new GridBagConstraints();
//Adding the JPanels. Panel for instructions
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
//JLabel for the Instructions.
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
gbc.gridy = 0;
panel.add(label, gbc);
//JLabel1 for Assingment/Grade/Weight(Percent)
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\t Mark\t\t\t\t\tWeight</pre></html>");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.NORTH;
panel.add(label1, gbc);
//JLabel Numbers for the number list of assingments at the side.
JLabel numbers = new JLabel("1");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
panel.add(numbers, gbc);
//JTextfield for Mark
JTextField mark = new JTextField(2);
gbc.fill = GridBagConstraints.NONE;
gbc.gridy = 2;
panel.add(mark, gbc);
//JTextfield for Weight
JTextField weight = new JTextField(2);
gbc.gridx = 2;
panel.add(weight, gbc);
//New frame set
JFrame frame = new JFrame("Grade Calculator-- ");
frame.setVisible(true);
frame.setSize(750,700);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(panel);
}
}
Thanks for reading.

Here's the GUI I created.
I don't know where your main method is, but you must always start a Swing application with a call to the SwingUtilities invokeLater method. The invokeLater method puts the creation and execution of the Swing components on the Event Dispatch thread (EDT).
When I use the GridBagLayout, I use the addComponent method I created to create a unique GridBagConstraints for each Swing component. I don't like to remember defaults.
The order of the JFrame methods is extremely important. Memorize the order of the JFrame methods in this example.
I put the instructions in a JTextArea. This way, the instruction text splits based on the size of the JTextArea. There's no need to hard code the line breaks with HTML.
Here's the code.
package com.ggl.testing;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class GradeAnalysis implements Runnable {
private static final Insets normalInsets = new Insets(10, 10, 0, 10);
private static final Insets finalInsets = new Insets(10, 10, 10, 10);
public static void main(String[] args) {
SwingUtilities.invokeLater(new GradeAnalysis());
}
#Override
public void run() {
JFrame frame = new JFrame("Grade Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setVisible(true);
}
private JPanel createMainPanel() {
GridBagConstraints gbc = new GridBagConstraints();
// Adding the JPanels. Panel for instructions
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
int gridy = 0;
// JLabel for the Instructions.
JTextArea instructionTextArea = new JTextArea(5, 30);
instructionTextArea.setEditable(false);
instructionTextArea.setLineWrap(true);
instructionTextArea.setWrapStyleWord(true);
instructionTextArea.setText(getInstructions());
JScrollPane instructionScrollPane = new JScrollPane(instructionTextArea);
addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1,
finalInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
// JLabels for Assignment/Grade/Weight(Percent)
JLabel label1 = new JLabel("Assignment");
label1.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
JLabel label2 = new JLabel("Mark");
label2.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label2, 1, gridy, 1, 1, finalInsets,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
JLabel label3 = new JLabel("Weight");
label3.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
// JLabel Numbers for the number list of assignments at the side.
JLabel number = new JLabel("1");
number.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, number, 0, gridy, 1, 1, normalInsets,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
// JTextfield for Mark
JTextField mark = new JTextField(20);
mark.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, mark, 1, gridy, 1, 1, normalInsets,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
// JTextfield for Weight
JTextField weight = new JTextField(20);
weight.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, weight, 2, gridy++, 1, 1, normalInsets,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
return panel;
}
private String getInstructions() {
return "Instructions: Type in the grades you’ve received, along with the weights "
+ "they’ll have in the determination of your overall average. After you "
+ "press ‘Calculate’, the results will show your average so far. Every "
+ "grade you enter must be a non-negative number, and every "
+ "percentage/weight you enter must be a positive number :)";
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
}

I suggest that you
Don't use setBounds(...) on any component or GUI
Same for setSize(...)
Instead nest JPanels each using its own layouts to achieve a pleasing and easy to manage layout and GUI.
Consider putting your intro text into a JTextArea. If you want it to look like a JLabel, you can take out the background color and borders.
Best of all would be to display the tabular data in a JTable, and for that you'd want to create your own table model, one based on the AbstractTableModel and that uses an Assignment object for each row.
An example without the JTable:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.*;
#SuppressWarnings("serial")
public class GradeAnalysis2 extends JPanel {
private static final int PREF_H = 400;
private static final String DISPLAY_TEXT = "Instructions: "
+ "Type in the grades you’ve received, along with the "
+ "weights they’ll have in the determination of your "
+ "overall average.\n"
+ "After you press ‘Calculate’, the results will show "
+ "your average so far.\n"
+ "Every grade you enter must be a non-negative number, "
+ "and every percentage/weight you enter must be a "
+ "positive number :)";
private JTextArea displayArea = new JTextArea(5, 50);
private GradeTablePanel gradeTablePanel = new GradeTablePanel();
public GradeAnalysis2() {
displayArea.setText(DISPLAY_TEXT);
displayArea.setWrapStyleWord(true);
displayArea.setLineWrap(true);
displayArea.setEditable(false);
displayArea.setFocusable(false);
displayArea.setBorder(null);
displayArea.setBackground(null);
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(gradeTablePanel, BorderLayout.PAGE_START);
centerPanel.add(Box.createGlue(), BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(centerPanel);
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new AddAssignmentAction("Add")));
setLayout(new BorderLayout());
add(displayArea, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}
#Override
public Dimension getPreferredSize() {
Dimension sz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return sz;
}
int height = Math.max(sz.height, PREF_H);
return new Dimension(sz.width, height);
}
private class AddAssignmentAction extends AbstractAction {
public AddAssignmentAction(String name) {
super(name);
int mnenomic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnenomic);
}
public void actionPerformed(ActionEvent e) {
gradeTablePanel.addAssignment();
gradeTablePanel.revalidate();
gradeTablePanel.repaint();
};
}
private class GradeTablePanel extends JPanel {
private int count = 0;
// parallel collections -- a bad kludge.
// a table model would make this much cleaner
private List<JTextField> marks;
private List<JTextField> weights;
public GradeTablePanel() {
setLayout(new GridBagLayout());
JLabel assgmntLbl = new JLabel(Assignment.ASSIGNMENT, SwingConstants.CENTER);
assgmntLbl.setFont(assgmntLbl.getFont().deriveFont(Font.BOLD));
JLabel markLbl = new JLabel(Assignment.MARK, SwingConstants.CENTER);
markLbl.setFont(markLbl.getFont().deriveFont(Font.BOLD));
JLabel weightLbl = new JLabel(Assignment.WEIGHT, SwingConstants.CENTER);
weightLbl.setFont(weightLbl.getFont().deriveFont(Font.BOLD));
add(assgmntLbl, createGbc(0, 0));
add(markLbl, createGbc(1, 0));
add(weightLbl, createGbc(2, 0));
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.CENTER;
return gbc;
}
public void addAssignment() {
count++;
JLabel countLabel = new JLabel(String.valueOf(count));
JTextField markField = new JTextField(2);
JTextField weightField = new JTextField(2);
add(countLabel, createGbc(0, count));
add(markField, createGbc(1, count));
add(weightField, createGbc(2, count));
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("GradeAnalysis2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GradeAnalysis2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class Assignment {
public static final String ASSIGNMENT = "Assignment";
public static final String MARK = "Mark";
public static final String WEIGHT = "Weight";
private String assignment;
private int mark;
private double weight;
public Assignment(String assignment, int mark, double weight) {
this.assignment = assignment;
this.mark = mark;
this.weight = weight;
}
public String getAssignment() {
return assignment;
}
public void setAssignment(String assignment) {
this.assignment = assignment;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}

You can set the minimum, expected and maximum sizes for each JTextField like so:
JTextField t = new JTextField();
t.setMaximumSize(MAX);
t.setMinimumSize(MIN);
t.setBounds(x, y, width, height);
Where MIN and MAX are a Dimension Object, and the inputs to set bounds are int

Related

FlowLayout Centering a label?

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);
}
}

how can I use JTextField and JLabel together?

I need a text field on a label but when i run this code there is no text field on the screen. How can i fix it.
JFrame jf = new JFrame() ;
JPanel panel = new JPanel() ;
JLabel label = new JLabel() ;
JTextField tField = new JTextField("asd" , 10) ;
label.add( tField ) ;
panel.add( label ) ;
jf.setSize( 500,400 ) ;
jf.add( panel ) ;
jf.setVisible(true) ;
JLabel's have no default layout manager, and so while your JTextField is being added tot he JLabel, it's not showing because the label has no idea how to show it.
There can be several ways to solve this depending on what you're trying to achieve:
Give the JLabel a layout manager, and then add the JTextField to it: but then the JTextField covers the JLabel, its text (if it has any) and its icon (if it has one), not good.
Create a JPanel to hold both, and give it an appropriate layout manager: probably a good bet.
Add them both to the same JPanel, using a layout manager that can easily place them in association: another good bet. GridBagLayout works well for this.
Don't forget to also call the JLabel's setLabelFor(...) method to associate it tightly with the JTextField, as per the JLabel Tutorial
For example:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class GridBagEg {
private static void createAndShowGui() {
PlayerEditorPanel playerEditorPane = new PlayerEditorPanel();
int result = JOptionPane.showConfirmDialog(null, playerEditorPane, "Edit Player",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
// TODO: do something with info
for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle.values()) {
System.out.printf("%10s: %s%n", fieldTitle.getTitle(),
playerEditorPane.getFieldText(fieldTitle));
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
enum FieldTitle {
NAME("Name", KeyEvent.VK_N), SPEED("Speed", KeyEvent.VK_P), STRENGTH("Strength", KeyEvent.VK_T);
private String title;
private int mnemonic;
private FieldTitle(String title, int mnemonic) {
this.title = title;
this.mnemonic = mnemonic;
}
public String getTitle() {
return title;
}
public int getMnemonic() {
return mnemonic;
}
};
private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
public PlayerEditorPanel() {
setLayout(new GridBagLayout());
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Player Editor"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagConstraints gbc;
for (int i = 0; i < FieldTitle.values().length; i++) {
FieldTitle fieldTitle = FieldTitle.values()[i];
JLabel label = new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT);
JTextField textField = new JTextField(10);
label.setDisplayedMnemonic(fieldTitle.getMnemonic());
label.setLabelFor(textField);
gbc = createGbc(0, i);
add(label, gbc);
gbc = createGbc(1, i);
add(textField, gbc);
fieldMap.put(fieldTitle, textField);
}
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = (x == 0) ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL;
gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
gbc.weightx = (x == 0) ? 0.1 : 1.0;
gbc.weighty = 1.0;
return gbc;
}
public String getFieldText(FieldTitle fieldTitle) {
return fieldMap.get(fieldTitle).getText();
}
}
Which displays as
Note that the JLabels have underlines on mnemonic chars, chars that when pressed in alt-key combination will bring the focus to the JTextField that the JLabel was linked to via, setLabelFor(...), and is caused by this code:
FieldTitle fieldTitle = FieldTitle.values()[i]; // an enum that holds label texts
JLabel label = new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT); // create JLabel
JTextField textField = new JTextField(10); // create JTextField
// set the label's mnemonic -- brings focus to the linked text field
label.setDisplayedMnemonic(fieldTitle.getMnemonic());
// *** here we *link* the JLabel with the JTextField
label.setLabelFor(textField);

JPanel doesnt show components after the second button click

I want to put a JTextPane component in a JPanel with a GridBagLayout when I click on button.
My code works fine for the first button click, But after, the next components are not displayed.
Here is my 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 int counter=0;
private GridBagLayout gbl = new GridBagLayout();
RefreshPanel() {
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(textPane.length);
for(int i=0;i<textPane.length;i++) {
System.out.println(textPane[i].getText());
addComponent(panel, gbl, textPane[i], 0, counter, 1, 1,1,1);
panel.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));
}
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;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public void createFrame() {
//frame.getContentPane().setLayout(new FlowLayout());
frame.add(scrollbar, BorderLayout.CENTER);
frame.add(newItem, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300,300));
frame.setVisible(true);
}
public static void main(String [] args) {
new RefreshPanel();
}
}
Patrick: you are right! I really forgot to change the gridbagconstraints
addComponent(panel, gbl, textPane[i], 0, i, 1, 1, 1, 1);
instead of
addComponent(panel, gbl, textPane[i], 0, counter, 1, 1, 1, 1);

Multiple JPanels one of top of the other

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 :

Providing white space in a Swing GUI

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

Categories

Resources