I created a JTabbedPane and a few JPanel objects. Since each panel pretty much looked the same the fields also are called the same. I thought it was OK since all the fields are local variables. Now I am in the controller class and I am having a issue with:
The best way to add action listeners to all the fields in each JPanel and
figure out which tabbed pane is selected.
Here is the GUI:
package mainGUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class MainMenuGUI {
JTabbedPane tabbedPane = new JTabbedPane();
JPanel findUserPanel;
JPanel deleteUserPanel;
JPanel addUserPanel;
JFrame frame = new JFrame();
JPanel tabbedPanel = new JPanel();
//Controller class for MainMenuGUI
ControllerMainMenuGUI listen = new ControllerMainMenuGUI(this);
public MainMenuGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
findUserPanel = createFindUserPanel();
deleteUserPanel = createDeleteUserPanel();
addUserPanel = createAddUserPanel();
tabbedPane.addTab("Find User", findUserPanel);
tabbedPane.addTab("Delete User", deleteUserPanel);
tabbedPane.addTab("Add User", addUserPanel);
tabbedPanel.add(tabbedPane);
frame.add(tabbedPanel);
frame.pack();
// opens frame in the center of the screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
JPanel createFindUserPanel() {
findUserPanel = new JPanel();
findUserPanel.setPreferredSize(new Dimension(300, 300));
findUserPanel.setLayout(new GridLayout(5, 7));
JLabel firstlbl = new JLabel("First Name");
JLabel lastlbl = new JLabel("Last Name");
JLabel addresslbl = new JLabel("Address");
JLabel agelbl = new JLabel("Age");
JTextField firstNametxt = new JTextField(15);
JTextField lastNametxt = new JTextField(15);
JTextField addresstxt = new JTextField(30);
JTextField age = new JTextField(3);
JButton btn = new JButton("Submit");
JScrollPane window = new JScrollPane();
window.setViewportBorder(new LineBorder(Color.RED));
window.setPreferredSize(new Dimension(150, 150));
findUserPanel.add(firstlbl);
findUserPanel.add(firstNametxt);
findUserPanel.add(lastlbl);
findUserPanel.add(lastNametxt);
findUserPanel.add(addresslbl);
findUserPanel.add(addresstxt);
findUserPanel.add(agelbl);
findUserPanel.add(age);
findUserPanel.add(window, BorderLayout.CENTER);
findUserPanel.add(btn);
return findUserPanel;
}
JPanel createDeleteUserPanel() {
deleteUserPanel = new JPanel();
deleteUserPanel.setPreferredSize(new Dimension(300, 300));
deleteUserPanel.setLayout(new GridLayout(5, 7));
JLabel firstlbl = new JLabel("First Name");
JLabel lastlbl = new JLabel("Last Name");
JLabel addresslbl = new JLabel("Address");
JLabel agelbl = new JLabel("Age");
JTextField firstNametxt = new JTextField(15);
JTextField lastNametxt = new JTextField(15);
JTextField addresstxt = new JTextField(30);
JTextField age = new JTextField(3);
JButton btn = new JButton("Submit");
JScrollPane window = new JScrollPane();
window.setViewportBorder(new LineBorder(Color.RED));
window.setPreferredSize(new Dimension(150, 150));
deleteUserPanel.add(firstlbl);
deleteUserPanel.add(firstNametxt);
deleteUserPanel.add(lastlbl);
deleteUserPanel.add(lastNametxt);
deleteUserPanel.add(addresslbl);
deleteUserPanel.add(addresstxt);
deleteUserPanel.add(agelbl);
deleteUserPanel.add(age);
deleteUserPanel.add(window, BorderLayout.CENTER);
deleteUserPanel.add(btn);
return deleteUserPanel;
}
JPanel createAddUserPanel() {
addUserPanel = new JPanel();
addUserPanel.setPreferredSize(new Dimension(300, 300));
addUserPanel.setLayout(new GridLayout(5, 7));
JLabel firstlbl = new JLabel("First Name");
JLabel lastlbl = new JLabel("Last Name");
JLabel addresslbl = new JLabel("Address");
JLabel agelbl = new JLabel("Age");
JTextField firstNametxt = new JTextField(15);
JTextField lastNametxt = new JTextField(15);
JTextField addresstxt = new JTextField(30);
JTextField age = new JTextField(3);
// nametxt.setPreferredSize(new Dimension(1,10));
JButton btn = new JButton("Submit");
JScrollPane window = new JScrollPane();
window.setViewportBorder(new LineBorder(Color.RED));
window.setPreferredSize(new Dimension(150, 150));
addUserPanel.add(firstlbl);
addUserPanel.add(firstNametxt);
addUserPanel.add(lastlbl);
addUserPanel.add(lastNametxt);
addUserPanel.add(addresslbl);
addUserPanel.add(addresstxt);
addUserPanel.add(agelbl);
addUserPanel.add(age);
addUserPanel.add(window, BorderLayout.CENTER);
addUserPanel.add(btn);
return addUserPanel;
}
}
Controller class:
package mainGUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ControllerMainMenuGUI {
MainMenuGUI mainMenuGUI;
int currentTabbedIndex = 0;
ControllerMainMenuGUI(MainMenuGUI mainMenuGUI){
this.mainMenuGUI = mainMenuGUI;
}
//inner class will handle MainMenuGUI actions
class Actions implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
}
private void addTabbedPaneListeners() {
mainMenuGUI.tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent ce) {
currentTabbedIndex =
mainMenuGUI.tabbedPane.getSelectedIndex();
System.out.println("Current tab is:" + currentTabbedIndex);
}
});
}
}
}
Related
I'll try to explain the problem as best i can.
Basically what I have is a system that takes in customer data (Customer is an object) and then adds it to an array. I have that working no problems. What I'm trying to get working is that I also have an option that should enable the user to edit a customers details. The way I'm trying to implement this is; in a separate GUI, I have a comboBox which will be populated with all of the customers first names that are currently in the arrayList. This also works.
Here is my problem - what I want to happen is that when one of the customers first names is chosen from the comboBox, it should find that object, and then fill out the JTextFields with its respective data, e.g. that customers first name should pop into the first name textField and his second name into the second name TextField etc..
I cant figure out this problem so any help is appreciated!
This is only one of a few classes, the rest of which work.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.JLabel;
import java.awt.SystemColor;
import java.text.ParseException;
import java.util.ArrayList;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.UIManager;
import java.awt.GridLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
public class editCustomers extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
ArrayList<Customers> customerList;
private MaskFormatter mask = null;
/**
* Create the frame.
*/
public editCustomers(ArrayList<Customers> aList) {
customerList = aList;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 600);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(6, 0, 0, 0));
JPanel panel_12 = new JPanel();
panel.add(panel_12);
panel_12.setLayout(new BorderLayout(0, 0));
JLabel lblSelectACustomer = new JLabel("Select a Customer to Edit");
panel_12.add(lblSelectACustomer, BorderLayout.WEST);
JComboBox comboBox = new JComboBox();
//comboBox.setModel(new DefaultComboBoxModel());
for (Customers temp : customerList) {
comboBox.addItem(temp.getfName());
}
panel_12.add(comboBox, BorderLayout.EAST);
JPanel panel_3 = new JPanel();
panel.add(panel_3);
panel_3.setLayout(new BorderLayout(0, 0));
JLabel lbl1 = new JLabel("Edit Customers First Name");
lbl1.setVerticalAlignment(SwingConstants.TOP);
Dimension d = lbl1.getPreferredSize();
lbl1.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_3.add(lbl1, BorderLayout.WEST);
JPanel panel_8 = new JPanel();
panel_3.add(panel_8, BorderLayout.CENTER);
textField = new JTextField();
panel_8.add(textField);
textField.setColumns(30);
JPanel panel_4 = new JPanel();
panel.add(panel_4);
panel_4.setLayout(new BorderLayout(0, 0));
JLabel lbl2 = new JLabel("Edit Customers Second Name");
lbl2.setVerticalAlignment(SwingConstants.TOP);
lbl2.getPreferredSize();
lbl2.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_4.add(lbl2, BorderLayout.WEST);
JPanel panel_9 = new JPanel();
panel_4.add(panel_9, BorderLayout.CENTER);
textField_1 = new JTextField();
panel_9.add(textField_1);
textField_1.setColumns(30);
JPanel panel_5 = new JPanel();
panel.add(panel_5);
panel_5.setLayout(new BorderLayout(0, 0));
JLabel lbl3 = new JLabel("Edit Customers Address");
lbl3.setVerticalAlignment(SwingConstants.TOP);
lbl3.getPreferredSize();
lbl3.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_5.add(lbl3, BorderLayout.WEST);
JPanel panel_10 = new JPanel();
panel_5.add(panel_10, BorderLayout.CENTER);
textField_2 = new JTextField();
panel_10.add(textField_2);
textField_2.setColumns(30);
JPanel panel_6 = new JPanel();
panel.add(panel_6);
panel_6.setLayout(new BorderLayout(0, 0));
JLabel lbl4 = new JLabel("Edit Customers Date of Birth");
lbl4.setVerticalAlignment(SwingConstants.TOP);
lbl4.getPreferredSize();
lbl4.setPreferredSize(new Dimension(d.width + 50, d.height));
panel_6.add(lbl4, BorderLayout.WEST);
JPanel panel_11 = new JPanel();
panel_6.add(panel_11, BorderLayout.CENTER);
textField_3 = new JTextField();
try {
mask = new MaskFormatter("##/##/####");
mask.setPlaceholderCharacter(' ');
} catch (ParseException e) {
e.printStackTrace();
}
JFormattedTextField textField_3 = new JFormattedTextField(mask);
textField_3.setText("dd/mm/yyyy");
panel_11.add(textField_3);
textField_3.setColumns(30);
JPanel panel_2 = new JPanel();
panel.add(panel_2);
panel_2.setLayout(new BorderLayout(0, 0));
JPanel panel_7 = new JPanel();
panel_2.add(panel_7, BorderLayout.CENTER);
JButton btnConfirm = new JButton("Confirm");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel_7.add(btnConfirm);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
panel_7.add(btnCancel);
JPanel panel_1 = new JPanel();
panel_1.setBackground(SystemColor.activeCaption);
contentPane.add(panel_1, BorderLayout.NORTH);
JLabel lblEditACurrent = new JLabel("Edit a Current Customer");
lblEditACurrent.setForeground(Color.BLACK);
lblEditACurrent.setFont(new Font("Arial", Font.PLAIN, 19));
lblEditACurrent.setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
panel_1.add(lblEditACurrent);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
editCustomers frame = new editCustomers(new ArrayList<Customers>());
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
You can try something like this ....
Change line 63 - 67 as following. Put the customers into the combobox instead of just names.
JComboBox<Customer> comboBox = new JComboBox<>();
//comboBox.setModel(new DefaultComboBoxModel());
for (Customers temp : customerList) {
comboBox.addItem(temp);
}
Then implement an ActionListener for the combobox as following.
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Customer customer = comboBox.getSelectedItem();
// Use the above customer object's fields to populate your text fields
}
});
I am writing a program for class, and I have about all of it done but one part that is really confusing me.
I have to create a system for flooring that a person can choose the floor type, enter their length and width of their floor and it calculates and produces a order summary.
Now, I am able to get all of this but the calculations correct (and I haven't even started on the database connection because of it.)
My professor is extremely vague in her directions and even the answers to her questions, so as a last ditch effort I thought I would try here.
I cannot for the life of me figure it out so ANY help is greatly appreciated.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class FloorMart
{
private static JFrame frame = null;
private static Integer cost;
private static String floorSize;
private static String floorType;
private double floorLength;
private double floorWidth;
public static void main(String[] args)
{
frame = new JFrame("FloorMart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(320, 300);
JTabbedPane tPane = new JTabbedPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setPreferredSize(new Dimension(200, 200));
JLabel jLabel = new JLabel();
jLabel.setText("Welcome to the FloorMart ordering system! ");
panel1.add(jLabel);
JLabel jLabel1 = new JLabel();
jLabel1.setText("Enter your Name: ");
panel1.add(jLabel1);
JTextField text1 = new JTextField(10);
panel1.add(text1);
JLabel jLabel2 = new JLabel();
jLabel2.setText("Enter your Pnone Number: ");
panel1.add(jLabel2);
JTextField text2 = new JTextField(10);
panel1.add(text2);
tPane.addTab("Customer", panel1);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Floor Type? ");
panel2.add(jLabel);
ButtonGroup group = new ButtonGroup();
JRadioButton RadioButton = new JRadioButton("Carpet - $10 per sq ft", true);
panel2.add(RadioButton);
group.add(RadioButton);
JRadioButton RadioButton1 = new JRadioButton("Hardwood - $20 per sq ft");
panel2.add(RadioButton1);
group.add(RadioButton1);
tPane.addTab("Floor Type", panel2);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Enter the length and width of your floor! ");
panel3.add(jLabel);
JLabel jLabel3 = new JLabel();
jLabel3.setText("Enter the floor length: ");
panel3.add(jLabel3);
JTextField length = new JTextField(10);
panel3.add(length);
//length.setText(cost.toString()); //
JLabel jLabel4 = new JLabel();
jLabel4.setText("Enter the floor width: ");
panel3.add(jLabel4);
JTextField width = new JTextField(10);
panel3.add(width);
//width.setText(cost.toString()); //
tPane.addTab("Floor Size", panel3);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Total Cost: ");
panel4.add(jLabel);
JTextField text3 = new JTextField(10);
panel4.add(text3);
JButton button = new JButton("Order Summary");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
JPanel panelNew = new JPanel();
panelNew.setLayout(new BoxLayout(panelNew, BoxLayout.Y_AXIS));
JLabel jLabeln = new JLabel("Order Summary");
JLabel jLabeln1 = new JLabel("Customer Name: " + text1.getText());
JLabel jLabeln2 = new JLabel("Phone Number: " + text2.getText());
JLabel jLabeln3 = new JLabel("Floor Type: " + floorType);
JLabel jLabeln4 = new JLabel("Floor Area: " + floorSize);
JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost) + "0");
JLabel jLabeln6 = new JLabel("Thank you for shopping at FloorMart!");
panelNew.add(jLabeln);
panelNew.add(jLabeln1);
panelNew.add(jLabeln2);
panelNew.add(jLabeln3);
panelNew.add(jLabeln4);
panelNew.add(jLabeln5);
panelNew.add(jLabeln6);
frame.invalidate();
frame.remove(panel1);frame.remove(panel2);frame.remove(panel3);
frame.remove(panel4);
frame.remove(tPane);
frame.add(panelNew);
frame.revalidate();
frame.repaint();
}
});
panel4.add(button);
tPane.addTab("Total", panel4);
ChangeListener changeListener= new ChangeListener()
{
public void stateChanged(ChangeEvent changeEvent)
{
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
int index = sourceTabbedPane.getSelectedIndex();
if(index == 2)
{
double floorLength = Double.parseDouble(length.getText());
double floorWidth = Double.parseDouble(width.getText());
floorSize = floorLength * floorWidth;
if(RadioButton.isSelected())
{
cost = 10; //per sq ft
floorType = RadioButton.getText();
}
else if(RadioButton1.isSelected())
{
cost = 20; //per sq ft
floorType = RadioButton1.getText();
}
text3.setText(cost.toString());
}
}
};
tPane.addChangeListener(changeListener);
frame.add(tPane);
frame.setVisible(true);
}
}
I can see two basic things that needs to be changed:
a. this does not compile (floorSize should be double)
String floorSize;
double floorLength = Double.parseDouble(length.getText());
double floorWidth = Double.parseDouble(width.getText());
floorSize = floorLength * floorWidth;
b. The calculation of floorSize should not be triggered by ChangeListener (which causes the calculation to be done before values are entered).
Use "Order Summary" JButton to trigger the calculation.
Here is a working version of the code. See my comments:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class FloorMart
{
private static JFrame frame = null;
private static Integer cost; //better use int
//////////////////////////////////////
//changed from string to double
private static double floorSize;
////////////////////////////////////
private static String floorType;
private double floorLength;
private double floorWidth;
public static void main(String[] args)
{
frame = new JFrame("FloorMart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(320, 300);
JTabbedPane tPane = new JTabbedPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setPreferredSize(new Dimension(200, 200));
JLabel jLabel = new JLabel();
jLabel.setText("Welcome to the FloorMart ordering system! ");
panel1.add(jLabel);
JLabel jLabel1 = new JLabel();
jLabel1.setText("Enter your Name: ");
panel1.add(jLabel1);
JTextField text1 = new JTextField(10);
panel1.add(text1);
JLabel jLabel2 = new JLabel();
jLabel2.setText("Enter your Pnone Number: ");
panel1.add(jLabel2);
JTextField text2 = new JTextField(10);
panel1.add(text2);
tPane.addTab("Customer", panel1);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Floor Type? ");
panel2.add(jLabel);
ButtonGroup group = new ButtonGroup();
JRadioButton RadioButton = new JRadioButton("Carpet - $10 per sq ft", true);
panel2.add(RadioButton);
group.add(RadioButton);
JRadioButton RadioButton1 = new JRadioButton("Hardwood - $20 per sq ft");
panel2.add(RadioButton1);
group.add(RadioButton1);
tPane.addTab("Floor Type", panel2);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Enter the length and width of your floor! ");
panel3.add(jLabel);
JLabel jLabel3 = new JLabel();
jLabel3.setText("Enter the floor length: ");
panel3.add(jLabel3);
JTextField length = new JTextField(10);
panel3.add(length);
//length.setText(cost.toString()); //
JLabel jLabel4 = new JLabel();
jLabel4.setText("Enter the floor width: ");
panel3.add(jLabel4);
JTextField width = new JTextField(10);
panel3.add(width);
//width.setText(cost.toString()); //
tPane.addTab("Floor Size", panel3);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Total Cost: ");
panel4.add(jLabel);
JTextField text3 = new JTextField(10);
panel4.add(text3);
JButton button = new JButton("Order Summary");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//////////////////////////////////////////////////////////////
//calculation moved to here
double floorLength = Double.parseDouble(length.getText());
double floorWidth = Double.parseDouble(width.getText());
floorSize = floorLength * floorWidth;
if(RadioButton.isSelected())
{
cost = 10; //per sq ft
floorType = RadioButton.getText();
}
else if(RadioButton1.isSelected())
{
cost = 20; //per sq ft
floorType = RadioButton1.getText();
}
text3.setText(cost.toString());
//////////////////////////////////////////////////////////////
JPanel panelNew = new JPanel();
panelNew.setLayout(new BoxLayout(panelNew, BoxLayout.Y_AXIS));
JLabel jLabeln = new JLabel("Order Summary");
JLabel jLabeln1 = new JLabel("Customer Name: " + text1.getText());
JLabel jLabeln2 = new JLabel("Phone Number: " + text2.getText());
JLabel jLabeln3 = new JLabel("Floor Type: " + floorType);
JLabel jLabeln4 = new JLabel("Floor Area: " + floorSize);
///////////////////////////////////////////////////////////////////////////
// changed JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost*floorSize) + "0");
//to:
JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost*floorSize) + "0");
///////////////////////////////////////////////////////////////////////////
JLabel jLabeln6 = new JLabel("Thank you for shopping at FloorMart!");
panelNew.add(jLabeln);
panelNew.add(jLabeln1);
panelNew.add(jLabeln2);
panelNew.add(jLabeln3);
panelNew.add(jLabeln4);
panelNew.add(jLabeln5);
panelNew.add(jLabeln6);
frame.invalidate();
frame.remove(panel1);frame.remove(panel2);frame.remove(panel3);
frame.remove(panel4);
frame.remove(tPane);
frame.add(panelNew);
frame.revalidate();
frame.repaint();
}
});
panel4.add(button);
tPane.addTab("Total", panel4);
/////////////////////////////////
//removed ChangeListener
////////////////////////////////
frame.add(tPane);
frame.setVisible(true);
}
}
I am trying to write a program with JPanels and for the life of me, I can't seem to get the JPanels to go into the proper positions. I don't have a clue what I am doing wrong.
Here is some of the code I have so far:
package mainGUIWindowFrames;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class CustomerWindow extends JFrame
{
//Attribute
private JTextField textTF;
private JButton copyButton;
private JLabel copyLabel;
private Border panelEdge;
//Constructor
public CustomerWindow()
{
this.setBounds(100,100,800,600);
panelEdge = BorderFactory.createEtchedBorder();
JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
mainPanel.add(createCustomerPanel(), BorderLayout.NORTH);
mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
mainPanel.add(createSearchPanel(), BorderLayout.WEST);
}
//Operational Methods
public JPanel createCustomerPanel()
{
JPanel customerPanel = new JPanel();
JLabel customerL = new JLabel("Clients",SwingConstants.CENTER);
customerL.setForeground(Color.blue);
customerL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
customerPanel.add(customerL);
customerPanel.setBorder(panelEdge);
return customerPanel;
}
public JPanel createCustomerInfoPanel()
{
JPanel infoPanel = new JPanel();
infoPanel.setBorder(panelEdge);
JLabel infoL = new JLabel("Clients",SwingConstants.CENTER);
infoL.setForeground(Color.blue);
infoL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
infoPanel.add(infoL);
//add a text field
textTF = new JTextField(50);
infoPanel.add(textTF);
//add a button
copyButton = new JButton("Copy Text");
copyButton.addActionListener(new ButtonListener());
infoPanel.add(copyButton);
copyLabel = new JLabel("-----------------");
infoPanel.add(copyLabel);
return infoPanel;
}
public JPanel createSearchPanel()
{
JPanel lowerPanel = new JPanel();
JLabel label = new JLabel("Text Transferred from JList:");
//the spot where the data shows up
lowerPanel.add(label);
return lowerPanel;
}
The only Panel that shows up is the CreateCustomerPanel(). I have no idea what I need to do to get the other two panels to work.
If you could help me out that would be great!!
well I eventually wound up solving it by creating another panel and moving the panels I had out of the main constructor.
public CustomerWindow() {
panelEdge = BorderFactory.createEtchedBorder();
}
public JPanel createNorthPanel()
{
JPanel customerPanel = new JPanel();
JLabel customerL = new JLabel("Clients",SwingConstants.CENTER);
customerL.setForeground(Color.blue);
customerL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
customerPanel.add(customerL);
customerPanel.setBorder(panelEdge);
return customerPanel;
}
//Operational Methods
public JPanel createCustomerPanel()
{
JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
mainPanel.add(createNorthPanel(), BorderLayout.NORTH);
mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
mainPanel.add(createSearchPanel(), BorderLayout.WEST);
return mainPanel;
}
public JPanel createCustomerInfoPanel()
{
JPanel infoPanel = new JPanel();
Box vBox = Box.createVerticalBox();
infoPanel.setBorder(panelEdge);
JLabel infoL = new JLabel("Clients",SwingConstants.CENTER);
infoL.setForeground(Color.blue);
infoL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
infoPanel.add(infoL);
//add a text field
clientId = new JTextField(10);
vBox.add(clientId);
vBox.add(Box.createVerticalStrut(25));
fName = new JTextField(10);
vBox.add(fName);
vBox.add(Box.createVerticalStrut(25));
lName = new JTextField(10);
vBox.add(lName);
vBox.add(Box.createVerticalStrut(25));
address = new JTextField(10);
vBox.add(address);
vBox.add(Box.createVerticalStrut(25));
postalCode = new JTextField(10);
vBox.add(postalCode);
vBox.add(Box.createVerticalStrut(25));
number = new JTextField(10);
vBox.add(number);
vBox.add(Box.createVerticalStrut(25));
type = new JTextField(10);
vBox.add(type);
infoPanel.add(vBox);
return infoPanel;
Ive still got a lot of work to do but thanks to all those who helped me out!!
Based on your example, nothing should show up, as you've not added mainPanel to anything.
Once I did that, I was able to get
to show up...
Modified code example
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
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 javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
public class CustomerWindow extends JFrame {
//Attribute
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
CustomerWindow frame = new CustomerWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private JTextField textTF;
private JButton copyButton;
private JLabel copyLabel;
private Border panelEdge;
//Constructor
public CustomerWindow() {
this.setBounds(100, 100, 800, 600);
panelEdge = BorderFactory.createEtchedBorder();
JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
mainPanel.add(createCustomerPanel(), BorderLayout.NORTH);
mainPanel.add(createCustomerInfoPanel(), BorderLayout.EAST);
mainPanel.add(createSearchPanel(), BorderLayout.WEST);
add(mainPanel);
}
//Operational Methods
public JPanel createCustomerPanel() {
JPanel customerPanel = new JPanel();
JLabel customerL = new JLabel("Clients", SwingConstants.CENTER);
customerL.setForeground(Color.blue);
customerL.setFont(new Font("Copperplate Gothic Bold", Font.BOLD, 48));
customerPanel.add(customerL);
customerPanel.setBorder(panelEdge);
return customerPanel;
}
public JPanel createCustomerInfoPanel() {
JPanel infoPanel = new JPanel();
infoPanel.setBorder(panelEdge);
JLabel infoL = new JLabel("Clients", SwingConstants.CENTER);
infoL.setForeground(Color.blue);
infoL.setFont(new Font("Copperplate Gothic Bold", Font.BOLD, 48));
infoPanel.add(infoL);
//add a text field
textTF = new JTextField(50);
infoPanel.add(textTF);
//add a button
copyButton = new JButton("Copy Text");
// copyButton.addActionListener(new ButtonListener());
infoPanel.add(copyButton);
copyLabel = new JLabel("-----------------");
infoPanel.add(copyLabel);
return infoPanel;
}
public JPanel createSearchPanel() {
JPanel lowerPanel = new JPanel();
JLabel label = new JLabel("Text Transferred from JList:");
//the spot where the data shows up
lowerPanel.add(label);
return lowerPanel;
}
}
You didn't add the mainpanel in the constructor.
add(mainPanel);
I am trying to display the drawing that I have posted. When my code runs and the user clicks Account, the panel only displays the buttons "OK" and "Cancel" (see screenshot). I have added three JTextAreas with a JLabel for each to the panel accountPanel but they don't display. My code is below.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JLabel input = new JLabel();
final JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.add(input);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
ethernet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog esettings = new JDialog(frame);
esettings.setTitle("Ethernet Settings");
esettings.setSize(400, 400);
esettings.pack();
esettings.setVisible(true);
}
});
JRadioButton rs = new JRadioButton("RS232");
rs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog rsettings = new JDialog(frame);
rsettings.setTitle("RS232 Settings");
rsettings.setSize(400, 400);
rsettings.pack();
rsettings.setVisible(true);
}
});
JRadioButton usbcdc = new JRadioButton("USB_CDC");
usbcdc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbc = new JDialog(frame);
usbc.setTitle("USB_CDC Settings");
usbc.setSize(400, 400);
usbc.pack();
usbc.setVisible(true);
}
});
JRadioButton usbhid = new JRadioButton("USB_HID");
usbhid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbh = new JDialog(frame);
usbh.setTitle("USB_HID Settings");
usbh.setSize(400, 400);
usbh.pack();
usbh.setVisible(true);
}
});
JButton next = new JButton("Next");
JButton aok = new JButton("OK");
JButton bok = new JButton("OK");
JButton cok = new JButton("OK");
JButton acancel = new JButton("Cancel");
JButton bcancel = new JButton("Cancel");
JButton ccancel = new JButton("Cancel");
JButton dcancel = new JButton("Cancel");
JLabel cardLabel = new JLabel("Card Number: ");
JLabel expLabel = new JLabel("Exp. Date: ");
JLabel cvvLabel = new JLabel("CVV: ");
JTextArea card = new JTextArea();
card.add(cardLabel);
JTextArea expDate = new JTextArea();
expDate.add(expLabel);
JTextArea cvv = new JTextArea();
cvv.add(cvvLabel);
final JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(bcancel);
final JPanel accountPanel = new JPanel();
accountPanel.add(bok);
accountPanel.add(ccancel);
accountPanel.add(card);
accountPanel.add(expDate);
accountPanel.add(cvv);
final JPanel apprvordecl = new JPanel();
apprvordecl.add(apprve);
apprvordecl.add(decline);
apprvordecl.add(aok);
apprvordecl.add(acancel);
final JPanel amountPanel = new JPanel();
amountPanel.add(cok);
amountPanel.add(dcancel);
input.setFont(new java.awt.Font("Tahoma", 3, 18));
input.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
input.setText("Input / Output Log");
JButton initialize = new JButton("Initialize");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JButton shutdown = new JButton("Shut Down");
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog port = new JDialog(frame);
port.setTitle("Port Settings");
port.setSize(400, 400);
port.add(PortSettings);
port.pack();
port.setVisible(true);
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
account.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog accountDialog = new JDialog(frame);
accountDialog.setTitle("Account");
accountDialog.setSize(400, 400);
accountDialog.add(accountPanel);
accountDialog.pack();
accountDialog.setVisible(true);
}
});
JButton amount = new JButton("Amount");
amount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog amount2 = new JDialog(frame);
amount2.setTitle("Amount");
amount2.setSize(400, 400);
amount2.add(amountPanel);
amount2.pack();
amount2.setVisible(true);
}
});
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
approvordecl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog apprv = new JDialog(frame);
apprv.setTitle("Approve / Decline");
apprv.setSize(400, 400);
apprv.add(apprvordecl);
apprv.pack();
apprv.setVisible(true);
}
});
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
ButtonGroup group = new ButtonGroup();
group.add(usbhid);
group.add(usbcdc);
group.add(ethernet);
group.add(rs);
ButtonGroup approvegroup = new ButtonGroup();
approvegroup.add(apprve);
approvegroup.add(decline);
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.add(online);
llpPanel.add(offline);
llpPanel.add(status);
llpPanel.add(reboot);
llpPanel.add(account);
llpPanel.add(amount);
llpPanel.add(reset);
llpPanel.add(approvordecl);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
JPanel uses FlowLayout by default which respects preferred sizes. The default width of the preferred size for the JTextArea's is 0 x 0. You need to give the JTextComponent's a preferred size for them to appear. Use the constructor that specifys rows & columns:
JTextArea card = new JTextArea(5, 10);
Don't forget to make the JTextArea scrollable by enclosing it in a JScrollPane:
accountPanel.add(new JScrollPane(card));
What's wrong with my code? The panel named 'leftPanel' is not showing up when i run the program whereas 'flightPanel
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TravelAgentSystem {
public static void main(String[] args){
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setTitle("Main Menu");
mainFrame.setVisible(true);
mainFrame.setSize(500,500);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel flightPanel = new JPanel();
flightPanel.setLayout(new GridLayout(2,2));
JButton timeb, priceb, hotelb, exitb;
timeb = new JButton("Time");
priceb = new JButton ("Price");
hotelb = new JButton ("Hotels Menu");
exitb = new JButton ("Exit Program");
class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton)event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("Exit Program")) {System.exit(0);}
}
}
buttonHandler handler = new buttonHandler();
exitb.addActionListener(handler);
flightPanel.add(timeb);
flightPanel.add(priceb);
flightPanel.add(hotelb);
flightPanel.add(exitb);
mainFrame.add(flightPanel, BorderLayout.CENTER);
JPanel travelPanel = new JPanel();
travelPanel.setLayout(new GridLayout(2,2));
travelPanel.setVisible(true);
JPanel timePanel = new JPanel();
timePanel = new JPanel();
timePanel.setLayout(new GridLayout(2,1));
timePanel.setVisible(true);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(2,1));
leftPanel.setVisible(true);
JLabel Lfrom, Lto, LDeparture;
Lfrom = new JLabel("From");
Lto = new JLabel("To");
LDeparture = new JLabel("Departure Date (DD/MM/YY)");
String[] fromOptions = {"East Midlands","Birmingham","Heathrow","Manchester"};
String[] toOptions = {"New York", "Dahab", "Rome", "Sydney", "Tokyo"};
JComboBox fromDest = new JComboBox(fromOptions);
JComboBox toDest = new JComboBox(toOptions);
JPanel datePanel = new JPanel();
datePanel.setLayout(new FlowLayout()); // So i can fix size of dateField
JTextField dateField = new JTextField();
dateField.setPreferredSize(new Dimension(100,20));
datePanel.add(dateField);
travelPanel.add(Lfrom);
travelPanel.add(fromDest);
travelPanel.add(Lto);
travelPanel.add(toDest);
timePanel.add(LDeparture);
timePanel.add(datePanel);
leftPanel.add(travelPanel);
leftPanel.add(timePanel);
mainFrame.add(leftPanel, BorderLayout.EAST);
}
}
Call mainFrame.setVisible(true); in last. I.e,
mainFrame.add(leftPanel, BorderLayout.EAST);
mainFrame.setVisible(true);
Here's what I get :