Want to resize components with the window? - java

I want to make my components on my JFrame resize with the resizing of the window.
How can this be done.
Currently when I make the JFrame smaller the components remain in the same place and then disappear as the frame gets smaller.
Ideally I would like to set a minimum JFrame size which would not allow the form to go any smaller but when I make it larger that components should move with the JFrame.
Add: What if I want to use an absolute layout manager. This is the layout manager that my boss prefers.
Code:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.Font;
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton btnNewButton;
private JButton btnNewButton_1;
private JButton btnNewButton_2;
private JCheckBox chckbxNewCheckBox;
private JCheckBox chckbxNewCheckBox_1;
private JCheckBox chckbxNewCheckBox_2;
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 458);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
setContentPane(contentPane);
contentPane.setLayout(null);
btnNewButton = new JButton("Save");
btnNewButton.setFont(new Font("Sylfaen", Font.BOLD, 14));
btnNewButton.setBounds(514, 386, 89, 23);
contentPane.add(btnNewButton);
btnNewButton_1 = new JButton("Open");
btnNewButton_1.setBounds(514, 352, 89, 23);
contentPane.add(btnNewButton_1);
btnNewButton_2 = new JButton("Close");
btnNewButton_2.setBounds(514, 318, 89, 23);
contentPane.add(btnNewButton_2);
btnNewButton_2.setBackground(Color.YELLOW);
btnNewButton_2.setEnabled(false);
chckbxNewCheckBox = new JCheckBox("Employeed");
chckbxNewCheckBox.setBounds(506, 7, 97, 23);
contentPane.add(chckbxNewCheckBox);
chckbxNewCheckBox_1 = new JCheckBox("Not Employeed");
chckbxNewCheckBox_1.setBounds(506, 33, 97, 23);
contentPane.add(chckbxNewCheckBox_1);
chckbxNewCheckBox_2 = new JCheckBox("Self Employeed");
chckbxNewCheckBox_2.setBounds(506, 59, 97, 23);
contentPane.add(chckbxNewCheckBox_2);
}
}
An Example of GridBagLayout:
This is an an example of the GridBagLayout. The components resize correctly but I do not see a grow property. It must do that automatically.
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}

Use a layout manager, like GridBagLayout, its have a property call "grow" for a column.

Related

JPanel with FlowLayout within JPanel with GridBagLayout not anchored properly

I'm working on a character creation panel for a role-playing game. It has a JPanel using GridBagLayout and within it a JPanel using FlowLayout.
Originally, when I didn't use FlowLayout it looked like this:
I needed to add another component on the x-axis. The weightx value would increase and become an even value. It would make all my titles off-centre and I didn't like that. I was thinking I could put some of my components inside a JPanel using FlowLayout, maybe wrap it in a JScrollPane or something nice:
Unfortunately the JPanel using FlowLayout is not being anchored by the JPanel using GridBagLayout. It went to the side of the display despite being set to anchor GridBagConstraints.BASELINE.
My Code:
public class TheLifeOfErnestRhodes extends JFrame {
private static JFrame frame = new JFrame("The Life of Ernest Rhodes");
private static Color black = new Color(40,40,40);
private static Color gold = new Color(255,223,0);
private static Color sienna = new Color(255,82,45);
private static Color stone = new Color(119,136,153);
private static Font titleFont = new Font("Serif", Font.BOLD + Font.ITALIC, 48);
private static Font textFont = new Font("Serif", Font.PLAIN, 20);
private static Font buttonFont = new Font("Serif", Font.BOLD + Font.ITALIC, 20);
private static void setFrame() {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
Container pane = frame.getContentPane();
JPanel backPanel = new JPanel();
backPanel.setBackground(black);
backPanel.setVisible(true);
pane.add(backPanel);
pane.add(charScreen());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
setFrame();
}
});
}
private static JPanel charScreen() {
GridBagLayout layout = new GridBagLayout();
JPanel charScreen = new JPanel(layout);
charScreen.setBackground(black);
charScreen.setVisible(true);
//Static labels for the two headings/titles on this screen.
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
c.insets = new Insets(80,0,20,0);
JLabel heading = new JLabel("THE LIFE OF ERNEST RHODES");
heading.setForeground(gold);
heading.setFont(titleFont);
charScreen.add(heading, c);
c.gridy++;
c.anchor = GridBagConstraints.BASELINE;
c.insets = new Insets(0,0,10,0);
JLabel title = new JLabel("CHARACTER CREATION");
title.setForeground(stone);
title.setFont(titleFont);
charScreen.add(title, c);
c.gridy++;
c.insets = new Insets (10, 20, 10, 20);
JPanel namePanel = new JPanel(new FlowLayout());
namePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JLabel labelName = new JLabel("FULL NAME:");
labelName.setForeground(stone);
labelName.setFont(textFont);
labelName.setVisible(true);
namePanel.add(labelName, c);
JTextField firstName = new JTextField("Taylor", 20);
firstName.setMinimumSize(firstName.getPreferredSize());
firstName.setBackground(black.darker());
firstName.setForeground(stone);
firstName.setFont(textFont);
firstName.setHorizontalAlignment(JTextField.CENTER);
firstName.setBorder(null);
firstName.setVisible(true);
namePanel.add(firstName);
JTextField lastName = new JTextField("Woodhouse", 20);
lastName.setMinimumSize(lastName.getPreferredSize());
lastName.setBackground(black.darker());
lastName.setForeground(stone);
lastName.setFont(textFont);
lastName.setHorizontalAlignment(JTextField.CENTER);
lastName.setBorder(null);
lastName.setVisible(true);
namePanel.add(lastName);
JLabel displayName = new JLabel("TYPE A NAME & PRESS 'ENTER'");
displayName.setForeground(stone);
displayName.setFont(textFont);
displayName.setVisible(true);
namePanel.add(displayName, c);
charScreen.add(namePanel);
c.gridx = 0;
c.gridy++;
c.fill = GridBagConstraints.NONE;
JLabel availablePts = new JLabel("AVAILABLE POINTS: 5");
availablePts.setForeground(stone);
availablePts.setFont(textFont);
availablePts.setVisible(true);
charScreen.add(availablePts, c);
c.gridx++;
JLabel allocatedPts = new JLabel("ALLOCATED POINTS:");
allocatedPts.setForeground(stone);
allocatedPts.setFont(textFont);
allocatedPts.setVisible(true);
charScreen.add(allocatedPts, c);
c.gridx++;
JLabel selectTrait = new JLabel("SELECT TRAITS:");
selectTrait.setForeground(stone);
selectTrait.setFont(textFont);
selectTrait.setVisible(true);
charScreen.add(selectTrait, c);
c.gridx = 0;
c.gridy++;
JLabel labelInv = new JLabel("Investigation");
labelInv.setForeground(gold);
labelInv.setFont(textFont);
labelInv.setVisible(true);
charScreen.add(labelInv, c);
c.gridy++;
JLabel labelPers = new JLabel("Persuasion");
labelPers.setForeground(gold);
labelPers.setFont(textFont);
labelPers.setVisible(true);
charScreen.add(labelPers, c);
c.gridy++;
JLabel LabelStl = new JLabel("Stealth");
LabelStl.setForeground(gold);
LabelStl.setFont(textFont);
LabelStl.setVisible(true);
charScreen.add(LabelStl, c);
c.gridx++;
c.gridy = 4;
JLabel allocatedInt = new JLabel("0");
allocatedInt.setForeground(stone);
allocatedInt.setFont(textFont);
allocatedInt.setVisible(true);
charScreen.add(allocatedInt, c);
c.gridy++;
JLabel allocatedPers = new JLabel("0");
allocatedPers.setForeground(stone);
allocatedPers.setFont(textFont);
allocatedPers.setVisible(true);
charScreen.add(allocatedPers, c);
c.gridy++;
JLabel allocatedAth = new JLabel("0");
allocatedAth.setForeground(stone);
allocatedAth.setFont(textFont);
allocatedAth.setVisible(true);
charScreen.add(allocatedAth, c);
c.gridx++;
c.gridy = 4;
JLabel keenEye = new JLabel("Keen Eye");
keenEye.setForeground(sienna);
keenEye.setFont(textFont);
keenEye.setVisible(true);
charScreen.add(keenEye, c);
c.gridy++;
JLabel interrogator = new JLabel("Interrogator");
interrogator.setForeground(gold);
interrogator.setFont(textFont);
interrogator.setVisible(true);
charScreen.add(interrogator, c);
c.gridy++;
JLabel sleuth = new JLabel("Sleuth");
sleuth.setForeground(gold);
sleuth.setFont(textFont);
sleuth.setVisible(true);
charScreen.add(sleuth, c);
c.gridx = 1;
c.gridy++;
JLabel reset = new JLabel("Reset Stats");
reset.setForeground(gold);
reset.setFont(textFont);
reset.setVisible(true);
charScreen.add(reset, c);
c.gridy++;
JLabel gender = new JLabel("GENDER:");
gender.setForeground(stone);
gender.setFont(textFont);
gender.setVisible(true);
charScreen.add(gender, c);
c.gridx = 0;
c.gridy++;
JLabel male = new JLabel("Male");
male.setForeground(gold);
male.setFont(textFont);
male.setVisible(true);
charScreen.add(male, c);
c.gridx++;
JLabel female = new JLabel("Female");
female.setForeground(gold);
female.setFont(textFont);
female.setVisible(true);
charScreen.add(female, c);
c.gridx++;
JLabel other = new JLabel("Other");
other.setForeground(sienna);
other.setFont(textFont);
other.setVisible(true);
charScreen.add(other, c);
c.gridx = 1;
c.gridy++;
c.anchor = GridBagConstraints.PAGE_END;
JLabel clickNext = new JLabel("Continue");
clickNext.setForeground(stone);
clickNext.setFont(titleFont);
clickNext.setVisible(true);
charScreen.add(clickNext, c);
}
}
Miscellaneous:
I tried doing the SSCCE thing. I couldn't bear the thought of someone running my code without the colors and fonts!
There are coloured JLabels used as 'buttons'. The Mac's button icons are really ugly so I went rogue. The above code is modified to only show the concerned components. In my program the JLabels have MouseListeners added to them.
If you're wondering about the game, it's a murder mystery and the player is the investigator! Obviously, they're investigating the life and death of 'Ernest Rhodes'.
Thank you all in advance!
Cheers,
Aadi
namePanel.add(labelName, c); is pointless, as namePanel is using a FlowLayout, passing it a GridBagLayoutConstraint is pointless as it's meaningless to FlowLayout
charScreen.add(namePanel); is effectively passing the "default" GridBagLayoutConstraint, meaning it will be laid out at the discretion of GridBagLayout, which isn't going to help you.
Maybe you meant charScreen.add(namePanel, c);, where c is the constraints you passed to namePanel
For example...
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class TheLifeOfErnestRhodes extends JFrame {
private static JFrame frame = new JFrame("The Life of Ernest Rhodes");
private static Color black = new Color(40, 40, 40);
private static Color gold = new Color(255, 223, 0);
private static Color sienna = new Color(255, 82, 45);
private static Color stone = new Color(119, 136, 153);
private static Font titleFont = new Font("Serif", Font.BOLD + Font.ITALIC, 48);
private static Font textFont = new Font("Serif", Font.PLAIN, 20);
private static Font buttonFont = new Font("Serif", Font.BOLD + Font.ITALIC, 20);
private void setFrame() {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// frame.setResizable(false);
Container pane = frame.getContentPane();
JPanel backPanel = new JPanel();
backPanel.setBackground(black);
backPanel.setVisible(true);
pane.add(backPanel);
pane.add(charScreen());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TheLifeOfErnestRhodes().setFrame();
}
});
}
private JPanel charScreen() {
GridBagLayout layout = new GridBagLayout();
JPanel charScreen = new JPanel(layout);
charScreen.setBackground(black);
charScreen.setVisible(true);
//Static labels for the two headings/titles on this screen.
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
c.insets = new Insets(80, 0, 20, 0);
JLabel heading = new JLabel("THE LIFE OF ERNEST RHODES");
heading.setForeground(gold);
heading.setFont(titleFont);
charScreen.add(heading, c);
c.gridy++;
c.anchor = GridBagConstraints.BASELINE;
c.insets = new Insets(0, 0, 10, 0);
JLabel title = new JLabel("CHARACTER CREATION");
title.setForeground(stone);
title.setFont(titleFont);
charScreen.add(title, c);
c.gridy++;
c.insets = new Insets(10, 20, 10, 20);
// JPanel namePanel = new JPanel(new FlowLayout());
charScreen.add(new NamePane(), c);
c.gridx = 0;
c.gridy++;
c.fill = GridBagConstraints.NONE;
JLabel availablePts = new JLabel("AVAILABLE POINTS: 5");
availablePts.setForeground(stone);
availablePts.setFont(textFont);
availablePts.setVisible(true);
charScreen.add(availablePts, c);
c.gridx++;
JLabel allocatedPts = new JLabel("ALLOCATED POINTS:");
allocatedPts.setForeground(stone);
allocatedPts.setFont(textFont);
allocatedPts.setVisible(true);
charScreen.add(allocatedPts, c);
c.gridx++;
JLabel selectTrait = new JLabel("SELECT TRAITS:");
selectTrait.setForeground(stone);
selectTrait.setFont(textFont);
selectTrait.setVisible(true);
charScreen.add(selectTrait, c);
c.gridx = 0;
c.gridy++;
JLabel labelInv = new JLabel("Investigation");
labelInv.setForeground(gold);
labelInv.setFont(textFont);
labelInv.setVisible(true);
charScreen.add(labelInv, c);
c.gridy++;
JLabel labelPers = new JLabel("Persuasion");
labelPers.setForeground(gold);
labelPers.setFont(textFont);
labelPers.setVisible(true);
charScreen.add(labelPers, c);
c.gridy++;
JLabel LabelStl = new JLabel("Stealth");
LabelStl.setForeground(gold);
LabelStl.setFont(textFont);
LabelStl.setVisible(true);
charScreen.add(LabelStl, c);
c.gridx++;
c.gridy = 4;
JLabel allocatedInt = new JLabel("0");
allocatedInt.setForeground(stone);
allocatedInt.setFont(textFont);
allocatedInt.setVisible(true);
charScreen.add(allocatedInt, c);
c.gridy++;
JLabel allocatedPers = new JLabel("0");
allocatedPers.setForeground(stone);
allocatedPers.setFont(textFont);
allocatedPers.setVisible(true);
charScreen.add(allocatedPers, c);
c.gridy++;
JLabel allocatedAth = new JLabel("0");
allocatedAth.setForeground(stone);
allocatedAth.setFont(textFont);
allocatedAth.setVisible(true);
charScreen.add(allocatedAth, c);
c.gridx++;
c.gridy = 4;
JLabel keenEye = new JLabel("Keen Eye");
keenEye.setForeground(sienna);
keenEye.setFont(textFont);
keenEye.setVisible(true);
charScreen.add(keenEye, c);
c.gridy++;
JLabel interrogator = new JLabel("Interrogator");
interrogator.setForeground(gold);
interrogator.setFont(textFont);
interrogator.setVisible(true);
charScreen.add(interrogator, c);
c.gridy++;
JLabel sleuth = new JLabel("Sleuth");
sleuth.setForeground(gold);
sleuth.setFont(textFont);
sleuth.setVisible(true);
charScreen.add(sleuth, c);
c.gridx = 1;
c.gridy++;
JLabel reset = new JLabel("Reset Stats");
reset.setForeground(gold);
reset.setFont(textFont);
reset.setVisible(true);
charScreen.add(reset, c);
c.gridy++;
JLabel gender = new JLabel("GENDER:");
gender.setForeground(stone);
gender.setFont(textFont);
gender.setVisible(true);
charScreen.add(gender, c);
c.gridx = 0;
c.gridy++;
JLabel male = new JLabel("Male");
male.setForeground(gold);
male.setFont(textFont);
male.setVisible(true);
charScreen.add(male, c);
c.gridx++;
JLabel female = new JLabel("Female");
female.setForeground(gold);
female.setFont(textFont);
female.setVisible(true);
charScreen.add(female, c);
c.gridx++;
JLabel other = new JLabel("Other");
other.setForeground(sienna);
other.setFont(textFont);
other.setVisible(true);
charScreen.add(other, c);
c.gridx = 1;
c.gridy++;
c.anchor = GridBagConstraints.PAGE_END;
JLabel clickNext = new JLabel("Continue");
clickNext.setForeground(stone);
clickNext.setFont(titleFont);
clickNext.setVisible(true);
charScreen.add(clickNext, c);
return charScreen;
}
public class NamePane extends JPanel {
public NamePane() {
JLabel labelName = new JLabel("FULL NAME:");
labelName.setForeground(stone);
labelName.setFont(textFont);
labelName.setVisible(true);
add(labelName);
JTextField firstName = new JTextField("Taylor", 20);
// firstName.setMinimumSize(firstName.getPreferredSize());
firstName.setBackground(black.darker());
firstName.setForeground(stone);
firstName.setFont(textFont);
firstName.setHorizontalAlignment(JTextField.CENTER);
firstName.setBorder(null);
firstName.setVisible(true);
add(firstName);
JTextField lastName = new JTextField("Woodhouse", 20);
// lastName.setMinimumSize(lastName.getPreferredSize());
lastName.setBackground(black.darker());
lastName.setForeground(stone);
lastName.setFont(textFont);
lastName.setHorizontalAlignment(JTextField.CENTER);
lastName.setBorder(null);
lastName.setVisible(true);
add(lastName);
JLabel displayName = new JLabel("TYPE A NAME & PRESS 'ENTER'");
displayName.setForeground(stone);
displayName.setFont(textFont);
displayName.setVisible(true);
add(displayName);
}
}
}

Put space between label in GridLayout

package committeeGUI;
import static committeeGUI.CommitteeGUI.comList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StudentMemberFrame extends JFrame {
public StudentMemberFrame() {
super("Add Student");
setSize(450, 500);
setLocation(561, 150);
super.setResizable(false);
addStudentMember();
}
public void addStudentMember() {
CommitteeGUI.frame.setEnabled(false);
final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
showConsoleArea.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//creating border and size of the border
showConsoleArea.setBorder(BorderFactory.createLineBorder(Color.black, 3));
add(showConsoleArea); //, BorderLayout.CENTER);
//setting a size to showConsoleArea.
showConsoleArea.setSize(500, 500);
final JLabel lblheading = new JLabel("STUDENT");
// showConsoleArea.add(lblheading,BorderLayout.CENTER);
/*
* creating components of company form
*/
final JLabel lblCommitteeName = new JLabel("Committee name");
final JTextField txtName = new JTextField(15);
final JLabel lblMemberName = new JLabel("Student name");
final JTextField txtMemberName = new JTextField(15);
final JLabel lblMemberNumber = new JLabel("Student number");
final JTextField txtMemberNumber = new JTextField(15);
final JLabel lblMemberCourse = new JLabel("Student course");
final JTextField txtMemberCourse = new JTextField(15);
final JButton buttAdd = new JButton("SAVE");
final JButton buttCancel = new JButton("CANCEL");
// adding components to the display area
c.gridx = 1;
c.gridy = 0;
showConsoleArea.add(lblheading, c);
c.gridx = 0;
c.gridy = 1;
showConsoleArea.add(lblCommitteeName, c);
c.gridx = 1;
c.gridy = 1;
showConsoleArea.add(txtName, c);
c.gridx = 0;
c.gridy = 2;
showConsoleArea.add(lblMemberName, c);
c.gridx = 1;
c.gridy = 2;
showConsoleArea.add(txtMemberName, c);
c.gridx = 0;
c.gridy = 3;
showConsoleArea.add(lblMemberNumber, c);
c.gridx = 1;
c.gridy = 3;
showConsoleArea.add(txtMemberNumber, c);
c.gridx = 0;
c.gridy = 4;
showConsoleArea.add(lblMemberCourse, c);
c.gridx = 1;
c.gridy = 4;
showConsoleArea.add(txtMemberCourse, c);
c.gridx = 0;
c.gridy = 5;
showConsoleArea.add(buttAdd, c);
c.gridx = 1;
c.gridy = 5;
showConsoleArea.add(buttCancel, c);
/*
* able to displaying the company frame
*/
this.show();
buttAdd.addActionListener((ActionEvent e) -> {
if (txtName.getText().equals("")
|| txtMemberName.getText().equals("")
|| txtMemberNumber.getText().equals("")
|| txtMemberCourse.getText().equals("")) //validating the data
{
CommitteeGUI.frame.setEnabled(false);
setEnabled(false);
messagebox("Enter a valid data", 0);
return;
}
if (!txtMemberNumber.getText().matches("\\d+")) {
CommitteeGUI.frame.setEnabled(false);
setEnabled(false);
messagebox("Member number must be a integer", 0);
return;
}
for (Committee com : comList) {
if (com.getName().equals(txtName.getText())) {
Student st = new Student();
st.setName(txtMemberName.getText());
st.setAcademicNo(Integer.parseInt(txtMemberNumber.getText()));
st.setCourse(txtMemberCourse.getText());
com.memberList.add(st);
messagebox("Member added successfully", 1);
setEnabled(false);
return;
}
}
messagebox("No Committee found with given name", 1);
});
//creating ActionListner to Cancel button
buttCancel.addActionListener((ActionEvent e) -> {
//frame is enabled for user.
CommitteeGUI.frame.setEnabled(true);
dispose(); //disposing the frame
} //pass the action to actionPerformed method and perform it.
);
}
#SuppressWarnings("deprecation")
public void messagebox(String label, final int conform) {
final JDialog infoBox = new JDialog();//message box
infoBox.setSize(400, 90);
infoBox.setAlwaysOnTop(true);
infoBox.setResizable(false);
infoBox.setLocation(675, 258);
JLabel space = new JLabel(" ");
JLabel label1 = new JLabel(label);
JButton buttOk = new JButton("Ok");
buttOk.addActionListener((ActionEvent e) -> {
if (conform == 1) {
// making frame operation enable.
CommitteeGUI.frame.setEnabled(true);
dispose();
}
setEnabled(true);
infoBox.hide();
});
JPanel holder = new JPanel(new FlowLayout());
holder.add(label1);
holder.add(buttOk);
infoBox.add(holder);
infoBox.show();
}
}
Above is my code. I want to put space between the heading (STUDENT) and the fields.
Attached is the snapshot of the frame:
I am not familiar with this layout. Help is much appreciated.
public void addStudentMember() {
final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
showConsoleArea.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Add below line of code change appropriate spacing
c.insets = new Insets(10, 10, 10, 10);

Getting rid of extra spaces between components when the JFrame is resized

I am using the GridBagLayout to arrange some components in a frame.
When the frame is first created, the components have a decent space in between them.
But as soon as I resize the frame there are alot of unwanted space between the components
I tried adjusting the weights and insets as suggested by some users, but it does not seem to fix the problem
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTextArea;
public class Frame1 extends JFrame {
JLabel one = new JLabel("one");
JLabel two = new JLabel("two");
JLabel three = new JLabel("three");
JTextField oneF = new JTextField(20);
JTextField twoF = new JTextField(20);
JTextField threeF = new JTextField(20);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("menu");
GridBagConstraints c = new GridBagConstraints();
public Frame1() {
setTitle("GridBagLayout Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
menuBar.add(menu);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = c.REMAINDER;
c.fill = c.HORIZONTAL;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(menuBar, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(one, c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(oneF, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(two, c);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(twoF, c);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(three, c);
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.fill = c.NONE;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = c.NORTH;
c.insets = new Insets(5,5,5,5);
add(threeF, c);
//setResizable(false);
pack();
setVisible(true);
}
}
ps:- I am new to GUI programming, so please forgive me for any noob mistakes.
edit 1: This is the what I want to have after I am done. I know the currently it does not look anyway near what I have in mind... I am still working on it
Thanks
use an nested layout (combinations of a few LayoutManagers), your picture talks me about,
still you can use GridBagLayout for components placed into left side,
in my code (simplest idea as is possible) JComponents placed on left side can't be resizable because are restricted from LayoutManager`s defaults, more in Oracle tutorial
.
.
painted from SSCCE/MCVE
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame {
private static final long serialVersionUID = 1L;
private JFrame myFrame = new JFrame("Whatever");
private JPanel parentPanel = new JPanel(new BorderLayout(10, 10)) {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
#Override
public Color getBackground() {
return new Color(255, 000, 000);
}
};
private JPanel leftPanel = new JPanel(/*default is FlowLayout*/) {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 400);
}
#Override
public Color getBackground() {
return new Color(255, 255, 000);
}
};
private JPanel leftChildPanel = new JPanel() {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 400);
}
#Override
public Color getBackground() {
return new Color(255, 255, 225);
}
};
private JPanel rightPanel = new JPanel(new BorderLayout(10, 10)) {
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 380);
}
#Override
public Color getBackground() {
return new Color(000, 255, 225);
}
};
public MyFrame() {
parentPanel.add(leftPanel, BorderLayout.WEST);
leftPanel.add(leftChildPanel);
parentPanel.add(rightPanel);
myFrame.add(parentPanel);
myFrame.setLocation(150, 150);
myFrame.pack();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new MyFrame();
});
}
}
The idea is to add empty row / columns that will grow to fill the available space:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
public class Frame1 extends JFrame {
JLabel one = new JLabel("one");
JLabel two = new JLabel("two");
JLabel three = new JLabel("three");
JTextField oneF = new JTextField(20);
JTextField twoF = new JTextField(20);
JTextField threeF = new JTextField(20);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("menu");
public Frame1() {
setTitle("GridBagLayout Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; //this defines 4 rows
//make 2 last empty row grow
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0,1.0};
//do the same for columns
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0,1.0};
getContentPane().setLayout(gridBagLayout);
menuBar.add(menu);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 5;
c.fill = c.HORIZONTAL;
c.anchor = c.NORTH;
c.insets = new Insets(5, 5, 5, 0);
getContentPane().add(menuBar, c);
//better have a new GridBagConstraints for each component added
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 1;
c1.gridwidth = 1;
c1.fill = c1.NONE;
c1.anchor = c1.NORTH;
c1.insets = new Insets(5, 5, 0, 5);
getContentPane().add(one, c1);
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 1;
c2.gridy = 1;
c2.fill = c2.NONE;
c2.anchor = GridBagConstraints.NORTHWEST;
c2.insets = new Insets(5, 5, 0, 5);
getContentPane().add(oneF, c2);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Frame1();
}
}
EDIT: in response to your edit: use the additional "growing" column for the "cover art"
The problem is your assignments of c.weightx and c.weighty. weightx and weighty determine how extra space is allocated to grid cells in a GridBagLayout when the container is made larger than necessary to accommodate the preferred sizes of the components.
The weightx and weighty should be zero for all cells except those cells which you want to grow larger when the window is made larger.
I have no real idea on how it is supposed to look like, but you could try to set for the labels c.anchor=GridBagConstraints.EAST and c.anchor=GridBagConstraints.WEST for the textfields.
Try also setting c.fill = GridBadConstraints.BOTH.

JTextArea height is only 1 line when using GridBayLayout

I am doing a program that finds words in a text file. I am using GridBagLayout for the position of the elements. When I run the program the text area shows with just one line. Even though it is set JTextArea results = new JTextArea(30, 30)
This is what it shows at the moment:
I am trying to do something like this:
Java code:
public class WordFinder extends JFrame {
private WordList words = new WordList();
private static final int WINDOW_WIDTH = 380;
private static final int WINDOW_HEIGHT = 380;
private static final int TEXT_WIDTH = 30;
private JLabel findLabel = new JLabel("Find:");
private JLabel wordsContaining = new JLabel("words containing");
private JTextField findWord = new JTextField(TEXT_WIDTH);
private JButton clear = new JButton("Clear");
private JTextArea results = new JTextArea(30, 30);
private JScrollPane scroll = new JScrollPane(results);
private JFileChooser chooseFile = new JFileChooser();
private JPanel pane = new JPanel(new GridBagLayout());
public WordFinder() {
super("Word Finder");
// Initialize the menu bar
//initMenu();
results.setEditable(false);
pane.setLayout(new GridBagLayout());
pane.setBorder(new EmptyBorder(15, 20, 0, 10));
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
results.setLineWrap(true);
results.setWrapStyleWord(true);
scroll.setViewportView(results);
// Add label "Find"
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(findLabel, c);
// Add text field
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
c.gridy = 0;
pane.add(findWord, c);
// Add clear button
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .1;
c.gridx = 2;
c.gridy = 0;
pane.add(clear, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(5, 5, 0, 0);
pane.add(wordsContaining, c);
// Add text area
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1;
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets(0, 3, 0, 5);
pane.add(scroll, c);
add(pane);
setVisible(true);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run () {
new WordFinder().show();
}
});
}
}
Any ideas in what am I missing? or perhaps I am doing something wrong?
Change c.fill = GridBagConstraints.HORIZONTAL; to c.fill = GridBagConstraints.BOTH;
Use pack instead of setSize
For example
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class WordFinder extends JFrame {
private static final int WINDOW_WIDTH = 380;
private static final int WINDOW_HEIGHT = 380;
private static final int TEXT_WIDTH = 30;
private JLabel findLabel = new JLabel("Find:");
private JLabel wordsContaining = new JLabel("words containing");
private JTextField findWord = new JTextField(TEXT_WIDTH);
private JButton clear = new JButton("Clear");
private JTextArea results = new JTextArea(30, 30);
private JScrollPane scroll = new JScrollPane(results);
private JFileChooser chooseFile = new JFileChooser();
private JPanel pane = new JPanel(new GridBagLayout());
public WordFinder() {
super("Word Finder");
// Initialize the menu bar
//initMenu();
results.setEditable(false);
pane.setLayout(new GridBagLayout());
pane.setBorder(new EmptyBorder(15, 20, 0, 10));
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
results.setLineWrap(true);
results.setWrapStyleWord(true);
scroll.setViewportView(results);
// Add label "Find"
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(findLabel, c);
// Add text field
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
c.gridy = 0;
pane.add(findWord, c);
// Add clear button
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .1;
c.gridx = 2;
c.gridy = 0;
pane.add(clear, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(5, 5, 0, 0);
pane.add(wordsContaining, c);
// Add text area
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets(0, 3, 0, 5);
pane.add(scroll, c);
add(pane);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new WordFinder().setVisible(true);
}
});
}
}
The problem you're having is caused by a combination of using the fill property GridBagConstraints.HORIZONTAL and setSize. When you use setSize, the size of the container is smaller then the JScrollPane's preferredSize and the layout manager is resorting to it's minimumSize instead.
By using GridBagConstraints.BOTH, you are allowing the layout manager to expand the component to fill the entire available space of the cell, regardless

Is there a difference where I run my program?

Regarding to my question I have before How to stop .next() and .previous() from looping It seems that whenever I cancel/finish/close my jframe I can't make my original program run again like when I run it for the first time.
I made the code below in netbeans while my original code is in eclipse. There shouldn't be difference where I run my program, right? But then.. I have that code in my original program in eclipse and it only execute .setEnable and .setText only once when I run my program. Even though both of the platforms have the same code I get different outcomes. Is this natural or not? My code below works perfectly fine but then in my original code it isn't. Is there something I'm doing wrong here? Should I post a snippet of codes from my original code for comparison?
This is my sample code though its not that short...
main class:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* #author lenovo
*/
public class main {
JFrame Card = new JFrame();
public main(){
Card.setVisible(true);
Card.setSize(605,333);
Card.setTitle("Tank Delivery");
Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - Card.getWidth())/2);
int y=(int)((dimension.getHeight() - Card.getHeight())/2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
JButton a = new JButton("OPEN");
a.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
gridbaglayoutdemo g = new gridbaglayoutdemo();
}
});
panel.add(a);
Card.add(panel);
}
public static void main(String[] args){
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new main();
}
});
}
}
my other class:
package cardlayoutalignment;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class gridbaglayoutdemo {
JFrame Card = new JFrame();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
Border margin = new EmptyBorder(10,10,10,10);
public static GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;
JPanel container;
JPanel divider = new JPanel();
JPanel bodypanel = new JPanel();
final JPanel buttonpanel = new JPanel();
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
JPanel panel_3 = new JPanel();
CardLayout cl = new CardLayout();
JTextArea text_2;
JTextArea text_3;
String change = "Finish";
final JButton btnNext;
final JButton btnBack;
int currentCard = 0;
int cardflag = 0;
AbstractAction backAction;
AbstractAction nextAction;
public gridbaglayoutdemo(){
Card.setVisible(true);
Card.setSize(605,333);
Card.setTitle("Tank Delivery");
Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - Card.getWidth())/2);
int y=(int)((dimension.getHeight() - Card.getHeight())/2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bodypanel.setLayout(new BorderLayout());
divider.setLayout(new BorderLayout());
container = new JPanel(cl);
container.setLayout(cl);
cl.show(container, "1");
panel_1.setLayout(grid);
JLabel label_1 = new JLabel("Enter 1:");
label_1.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_1.add(label_1, c);
JComboBox box_1 = new JComboBox();
box_1.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(10,10,0,0);
panel_1.add(box_1,c);
JLabel label = new JLabel("");
label.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(10,0,0,0);
panel_1.add(label, c);
panel_2.setLayout(grid);
JLabel label_2 = new JLabel("Enter 2:");
label_2.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_2.add(label_2,c);
text_2 = new JTextArea();
text_2.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 20;
c.insets = new Insets(10,10,0,0);
panel_2.add(text_2,c);
JLabel label_22 = new JLabel("");
label_22.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10,0,0,0);
panel_2.add(label_22, c);
panel_3.setLayout(grid);
JLabel label_3 = new JLabel("Enter 3:");
label_3.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_3.add(label_3,c);
text_3 = new JTextArea();
text_3.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 20;
c.insets = new Insets(10,10,0,0);
panel_3.add(text_3,c);
JLabel label_33 = new JLabel("");
label_33.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10,0,0,0);
panel_3.add(label_33, c);
buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
buttonpanel.setBorder(new EmptyBorder(0,10,0,0));
buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
buttonpanel.setBorder(new EmptyBorder(0,0,0,0));
btnBack = new JButton("< Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.previous(container);
buttonpanel.repaint();
cardflag--;
if (cardflag==0)
{btnBack.setEnabled(false);}
if(cardflag<3)
{btnNext.setText("Next >");}
}
});
btnBack.setEnabled(false);
btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnBack.setPreferredSize(new Dimension(110, 40));
btnBack.setBackground(new Color(224,223,227));
buttonpanel.add(btnBack);
btnNext = new JButton("Next >");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.next(container);
buttonpanel.repaint();
cardflag++;
if(cardflag<3)
{btnBack.setEnabled(true);}
if(cardflag==2)
{btnNext.setText(change);}
if (cardflag==3)
{cl.show(container, "3");
JOptionPane.showMessageDialog(null, "DONE");
Window dialog = SwingUtilities.windowForComponent( btnNext );
dialog.dispose();
cardflag=0;
btnNext.setText("Next >");
}
validateText();
}
});
btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnNext.setPreferredSize(new Dimension(110, 40));
btnNext.setBackground(new Color(224,223,227));
btnNext.setVisible(true);
buttonpanel.add(btnNext);
final JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardflag=0;
Window dialog = SwingUtilities.windowForComponent( btnCancel );
dialog.dispose();
}
});
btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
btnCancel.setFocusable(false);
btnCancel.setFocusTraversalKeysEnabled(false);
btnCancel.setFocusPainted(false);
btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnCancel.setPreferredSize(new Dimension(110, 40));
btnCancel.setBackground(new Color(224,223,227));
buttonpanel.add(btnCancel);
JPanel numberpanel = new JPanel();
numberpanel.setPreferredSize(new Dimension(221,0));
numberpanel.setBorder(new EmptyBorder(10,0,0,10));
numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
numberpanel.setLayout(flow);
JButton button_7 = new JButton("7");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonaction(e);
}
});
button_7.setActionCommand("7");
button_7.setFont(new Font("Arial", Font.PLAIN, 30));
button_7.setFocusable(false);
button_7.setFocusTraversalKeysEnabled(false);
button_7.setFocusPainted(false);
button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_7.setPreferredSize(new Dimension(70, 70));
button_7.setBackground(new Color(224,223,227));
numberpanel.add(button_7);
JButton button_8 = new JButton("8");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonaction(e);
}
});
button_8.setActionCommand("8");
button_8.setFont(new Font("Arial", Font.PLAIN, 30));
button_8.setFocusable(false);
button_8.setFocusTraversalKeysEnabled(false);
button_8.setFocusPainted(false);
button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_8.setPreferredSize(new Dimension(70, 70));
button_8.setBackground(new Color(224,223,227));
numberpanel.add(button_8);
JButton button_9 = new JButton("9");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonaction(e);
}
});
button_9.setActionCommand("9");
button_9.setFont(new Font("Arial", Font.PLAIN, 30));
button_9.setFocusable(false);
button_9.setFocusTraversalKeysEnabled(false);
button_9.setFocusPainted(false);
button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_9.setPreferredSize(new Dimension(70, 70));
button_9.setBackground(new Color(224,223,227));
numberpanel.add(button_9);
Card.add(bodypanel);
bodypanel.add(divider, BorderLayout.WEST);
divider.add(container, BorderLayout.NORTH);
container.add(panel_1, "1");
container.add(panel_2, "2");
container.add(panel_3, "3");
divider.add(buttonpanel, BorderLayout.SOUTH);
bodypanel.add(numberpanel, BorderLayout.EAST);
}
private void buttonaction(ActionEvent e){
try{
if(cardflag==1)
{text_2.append("" + e.getActionCommand());}
if(cardflag==2)
{text_3.append("" + e.getActionCommand());}
}catch(Exception e1){}
}
private void validateText(){
if(cardflag==2)
{String text = text_2.getText();
if (text.isEmpty()==true)
{JOptionPane.showMessageDialog(null, "Text 2 is empty!");
cl.show(container, "2");
btnNext.setText("Next >");
cardflag--;
}
}
}
}

Categories

Resources