How to call actionPerformed() within actionPerfomed() - java

I have a problem with my code below.
There are three menu items (Customer, Merchandize and Employee) in the Add Information Menu. Clicking on them (using addActionListener) should show various text fields/radio buttons/combo-boxes (which are required to fill in information) and a submit button.
After submitting the required information and clicking the submit button it should print the information to a Pop-up window.
I am stuck at the last point where it should call the actionPerformed method again and print the values to a Pop-up window. Can anyone help?
EDITED##I have edited my code. My problem starts from line no. 216 to line no. 225. When I click on the button "submit3" of the Customer menuitem, the pop-up appears but does not show the contents of the string that contains contents of "txt1". How do I pass the values of my components to actionPerformed so that it can print them in a new pop-up window?
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Retail extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menuBar = new JMenuBar();
JMenu addmenu = new JMenu("Add Information");
JMenu delmenu = new JMenu("Delete Information");
JMenu savemenu = new JMenu("Save Information");
JMenuItem emp = new JMenuItem("Employee");
JMenuItem merc = new JMenuItem("Merchandise");
JMenuItem cust = new JMenuItem("Customer");
Container contentPane = getContentPane();
JPanel p2 = new JPanel();
public Retail()
{
super();
contentPane.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setBorder(new TitledBorder("Select Menu"));
p1.setPreferredSize(new Dimension(500, 100));
contentPane.add(p1,BorderLayout.NORTH);
p2.setBorder(new TitledBorder("Entry Screen"));
p2.setPreferredSize(new Dimension(500,500));
contentPane.add(p2,BorderLayout.CENTER);
p2.setLayout(new BorderLayout());
p1.add(menuBar);
menuBar.add(addmenu);
menuBar.add(delmenu);
menuBar.add(savemenu);
addmenu.add(emp);
addmenu.addSeparator();
addmenu.add(merc);
addmenu.addSeparator();
addmenu.add(cust);
addmenu.addSeparator();
emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
JButton submit1 = new JButton("Submit Employee Information");
JButton submit2 = new JButton("Submit Merchandise Information");
JButton submit3 = new JButton("Submit Customer Information");
if(e.getSource() == emp)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender:");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rb1);
bgroup.add(rb2);
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit1);
p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);
submit1.addActionListener(this);
}
if(e.getSource() == merc)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit2);
p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);
submit2.addActionListener(this);
}
if(e.getSource() == cust)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit3);
p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);
final String s;
s = txt1.getText();
submit3.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent args0)
{
JOptionPane.showMessageDialog(rootPane,s);
}
});
}
}
public static void main(String[] args)
{
Retail frame = new Retail();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Retail Information");
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
}
}

This last test:
if(e.getSource()==submit1)
will never succeed because submit1 is a JButton that you just constructed at the start of actionPerformed and thus cannot be the source for the current event.
Instead of constructing new layout components like this, I suggest that you use a CardLayout for p2 and just flip to the appropriate card in your action handler. That way you can register listeners for all the buttons once and you will get notified of all events properly.
Also, instead of having one giant actionPerformed that tests for the source, you should register separate ActionListeners for each UI component. That keeps the logic (and the code) a lot cleaner.
EDIT
For instance, instead of this:
emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);
you could do this:
emp.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// logic for click on emp button
}
});
merc.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// logic for click on merc button
}
});
// etc.
You then don't need implements ActionListener for your main class.
Then, if you use a CardLayout for p2, you can, at the start of your program, attach action listeners to every one of the interface elements. The logic for responding to any particular action then becomes much simpler--merely updating the appropriate UI elements and switching which "card" to show in p2. See the docs for CardLayout for more info on this last part.

You can either call .doClick() of appropriate next menu item
OR
define separete methods (e.g. doEmpAction() and doCustAction() ) which are called from the appropriate actionPerformed() methods and call them one from another. So doCustAction just call doEmpAction.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Retail extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menuBar = new JMenuBar();
JMenu addmenu = new JMenu("Add Information");
JMenu delmenu = new JMenu("Delete Information");
JMenu savemenu = new JMenu("Save Information");
JMenuItem emp = new JMenuItem("Employee");
JMenuItem merc = new JMenuItem("Merchandise");
JMenuItem cust = new JMenuItem("Customer");
Container contentPane = getContentPane();
JPanel p2 = new JPanel();
public Retail() {
super();
contentPane.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setBorder(new TitledBorder("Select Menu"));
p1.setPreferredSize(new Dimension(500, 100));
contentPane.add(p1, BorderLayout.NORTH);
p2.setBorder(new TitledBorder("Entry Screen"));
p2.setPreferredSize(new Dimension(500, 500));
contentPane.add(p2, BorderLayout.CENTER);
p2.setLayout(new BorderLayout());
p1.add(menuBar);
menuBar.add(addmenu);
menuBar.add(delmenu);
menuBar.add(savemenu);
addmenu.add(emp);
addmenu.addSeparator();
addmenu.add(merc);
addmenu.addSeparator();
addmenu.add(cust);
addmenu.addSeparator();
emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
JButton submit1 = new JButton("Submit Employee Information");
JButton submit2 = new JButton("Submit Merchandise Information");
JButton submit3 = new JButton("Submit Customer Information");
if (e.getSource() == emp) {
p2.removeAll();
p2.updateUI();
String[] states = {"MA", "AZ", "CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8, 1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1, 2));
JLabel lb7 = new JLabel("Gender:");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rb1);
bgroup.add(rb2);
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8, 1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit1);
p2.add(p3, BorderLayout.WEST);
p2.add(p5, BorderLayout.EAST);
submit1.addActionListener(this);//instead of this line use next line to add actionlistener.
}
if (e.getSource() == merc) {
p2.removeAll();
p2.updateUI();
String[] states = {"MA", "AZ", "CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8, 1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1, 2));
JLabel lb7 = new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8, 1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit2);
p2.add(p3, BorderLayout.WEST);
p2.add(p5, BorderLayout.EAST);
submit2.addActionListener(this);//instead of this line use next line to add actionlistener.
}
if (e.getSource() == cust) {
p2.removeAll();
p2.updateUI();
String[] states = {"MA", "AZ", "CA"};
JLabel lb1 = new JLabel("First Name:");
final JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8, 1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1, 2));
JLabel lb7 = new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8, 1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit3);
p2.add(p3, BorderLayout.WEST);
p2.add(p5, BorderLayout.EAST);
//submit3.addActionListener(this);//instead of this line use next line to add actionlistener.Commment this line.
submit3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("------>" + txt1.getText());
//JOptionPane.showMessageDialog(rootPane, txt1.getText());
new MyDialog(txt1.getText());
}
});
// submit3.addActionListener(new SubmitActionListener(txt1.getText()));//out action listener
}
if (e.getSource() == submit1) {
JOptionPane.showMessageDialog(rootPane, " button is clicked");
}
}
class MyDialog extends JDialog {
public MyDialog(String textbox1) {
JLabel label = new JLabel(textbox1);
add(label);
setModalityType(ModalityType.APPLICATION_MODAL);
setTitle("Info");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 200);
setVisible(true);
}
}
public static void main(String[] args) {
Retail frame = new Retail();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Retail Information");
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
}
}
This works for submit3 button.Modify others as your need.

Related

I got "error:cannot find symbol" for the JButton inside a frame outside of a JTabbedPane [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 4 years ago.
So I have a frame, inside the frame there are tabs. I tried to put Jbutton outside of the tab but it wouldn't recognize my Jbutton. I keep on getting
error: Cant find symbol.
I don't understand why that happened. Would someone explain?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.border.*;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class menu extends JFrame implements ActionListener{
JFrame f;
public menu()
{ f=new JFrame();
JPanel p0 = new JPanel(); p0.setBackground(Color.WHITE);
JPanel p01 = new JPanel(); p01.setBackground(Color.WHITE);
JPanel p01_0 = new JPanel(); p01_0.setBackground(Color.WHITE);
JPanel p01_2 = new JPanel(); p01_2.setBackground(Color.WHITE);
JPanel p01_3 =new JPanel(); p01_3.setBackground(Color.WHITE);
JPanel p01_4 =new JPanel(); p01_4.setBackground(Color.WHITE);
JPanel p01_5 =new JPanel(); p01_5.setBackground(Color.WHITE);
JPanel p01A = new JPanel(); p01A.setBackground(Color.WHITE);
JPanel p02 = new JPanel(); p02.setBackground(Color.WHITE);
JPanel p03 = new JPanel(); p03.setBackground(Color.WHITE);
JPanel p1=new JPanel();
p1.setBackground(Color.WHITE);
JPanel p1A=new JPanel();
p1A.setBackground(Color.WHITE);
JPanel p1A1=new JPanel(); p1A1.setBackground(Color.WHITE); JPanel
p1A2=new JPanel(); p1A2.setBackground(Color.WHITE);
JPanel p1A3=new JPanel(); p1A3.setBackground(Color.WHITE); JPanel
p1A4=new JPanel(); p1A4.setBackground(Color.WHITE);
JPanel p1A5=new JPanel(); p1A5.setBackground(Color.WHITE); JPanel
p1A6=new JPanel(); p1A6.setBackground(Color.WHITE);
JPanel p1A7=new JPanel(); p1A7.setBackground(Color.WHITE); JPanel
p1A8=new JPanel(); p1A8.setBackground(Color.WHITE);
JPanel p1A9=new JPanel(); p1A9.setBackground(Color.WHITE); JPanel
p1A10=new JPanel(); p1A10.setBackground(Color.WHITE);
JPanel p2=new JPanel();
p2.setBackground(Color.WHITE);
JPanel p2A=new JPanel();
p2A.setBackground(Color.WHITE);
JPanel p2A0=new JPanel(); p2A0.setBackground(Color.WHITE); JPanel
p2A1=new JPanel(); p2A1.setBackground(Color.WHITE);
JPanel p2A2=new JPanel(); p2A2.setBackground(Color.WHITE); JPanel
p2A3=new JPanel(); p2A3.setBackground(Color.WHITE);
JPanel p2A4=new JPanel(); p2A4.setBackground(Color.WHITE); JPanel
p2A5=new JPanel(); p2A5.setBackground(Color.WHITE);
JPanel p2A6=new JPanel(); p2A6.setBackground(Color.WHITE); JPanel
p2A7=new JPanel(); p2A7.setBackground(Color.WHITE);
JPanel p2A8=new JPanel(); JPanel p2A9=new JPanel(); JPanel p2A10=new
JPanel(); JPanel p2A11=new JPanel();
JPanel p3=new JPanel();
p3.setBackground(Color.WHITE);
JPanel p3A=new JPanel();
p3A.setBackground(Color.WHITE);
JPanel p3A0=new JPanel(); p3A0.setBackground(Color.WHITE); JPanel
p3A1=new JPanel(); p3A1.setBackground(Color.WHITE);
JPanel p3A2=new JPanel(); p3A2.setBackground(Color.WHITE); JPanel
p3A3=new JPanel(); p3A3.setBackground(Color.WHITE);
JPanel p3A4=new JPanel(); p3A4.setBackground(Color.WHITE);JPanel
p3A5=new JPanel(); p3A5.setBackground(Color.WHITE);
JPanel p3A6=new JPanel(); p3A6.setBackground(Color.WHITE);JPanel
p3A7=new JPanel(); p3A7.setBackground(Color.WHITE);
JPanel p4=new JPanel();
p4.setBackground(Color.WHITE);
JPanel p4A=new JPanel();
p4A.setBackground(Color.WHITE);
JPanel p4A0=new JPanel(); p4A0.setBackground(Color.WHITE); JPanel
p4A1=new JPanel(); p4A1.setBackground(Color.WHITE); JPanel p4A11=new
JPanel(); p4A11.setBackground(Color.WHITE);
JPanel p4A2=new JPanel(); p4A2.setBackground(Color.WHITE); JPanel
p4A3=new JPanel(); p4A3.setBackground(Color.WHITE);
JPanel p4A4=new JPanel(); p4A4.setBackground(Color.WHITE); JPanel
p4A5=new JPanel(); p4A5.setBackground(Color.WHITE);
JPanel p4A6=new JPanel(); p4A6.setBackground(Color.WHITE); JPanel
p4A7=new JPanel(); p4A7.setBackground(Color.WHITE);
JPanel p4B=new JPanel();
p4B.setBackground(Color.WHITE);
JPanel p4B11=new JPanel(); p4B11.setBackground(Color.WHITE); JPanel
p4B1=new JPanel(); p4B1.setBackground(Color.WHITE); JPanel p4B2=new
JPanel(); p4B2.setBackground(Color.WHITE);
JPanel p4B3=new JPanel(); p4B3.setBackground(Color.WHITE); JPanel
p4B4=new JPanel(); p4B4.setBackground(Color.WHITE);
JPanel p4B5=new JPanel(); p4B5.setBackground(Color.WHITE);
JPanel p4C=new JPanel();
p4C.setBackground(Color.WHITE);
JPanel p4D=new JPanel();
p4D.setBackground(Color.WHITE);
JPanel p4D11=new JPanel(); p4D11.setBackground(Color.WHITE); JPanel
p4D1=new JPanel(); p4D1.setBackground(Color.WHITE);
JPanel p4D2=new JPanel(); p4D2.setBackground(Color.WHITE);
JPanel p4F=new JPanel(); p4F.setBackground(Color.WHITE);
Font myFontResta = new Font ( "AR DARLING",Font.BOLD, 60);
Font detailfont = new Font ( "Century Gothic", Font.BOLD, 25);
Font mealfont = new Font ( "Century Gothic", Font.BOLD, 20);
Font drinksfont = new Font ( "Century Gothic", Font.BOLD, 20);
Font drinksfont2 = new Font ( "Century Gothic", Font.BOLD, 25);
LineBorder lineBorder1=new LineBorder(Color.BLACK);
LineBorder lineBorder2=new LineBorder(Color.BLACK);
LineBorder lineBorder3=new LineBorder(Color.BLACK);
JTabbedPane tp = new JTabbedPane();
tp.setFont( new Font( "Century Gothic", Font.BOLD, 24 ) );
tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
JLabel logo1 = new JLabel(new ImageIcon("wotakoi.png"));
JLabel RName1 = new JLabel("Restaurant");
JLabel table = new JLabel("Table's Number :");
JTextField number = new JTextField(" " ,10);
JLabel Detail = new JLabel("Order Detail");
JLabel food = new JLabel("Food(s) :");
JTextField FoodList = new JTextField(" ",20);
JLabel drnk = new JLabel("Drink(s) :");
JTextField DrinkList = new JTextField(" ",20);
JLabel Price = new JLabel("Total Price :");
JTextField total = new JTextField(" ",20);
JLabel space1 = new JLabel(" "); JLabel space2 = new JLabel(" "); JLabel
space3 = new JLabel(" "); JLabel space4 = new JLabel(" ");
JLabel space5 = new JLabel(" "); JLabel space6 = new JLabel(" "); JLabel
space7 = new JLabel(" "); JLabel space8 = new JLabel(" ");
JLabel space9 = new JLabel(" "); JLabel space10 = new JLabel(" ");
JButton confirm = new JButton("Confirm Order");
confirm.setBackground(Color.GREEN);
confirm.setContentAreaFilled(false);
confirm.setOpaque(true);
JButton reset = new JButton("Reset");
reset.setBackground(Color.RED);
reset.setContentAreaFilled(false);
reset.setOpaque(true);
JButton home = new JButton("Select Table");
//WESTERN
ImageIcon bburger = new ImageIcon("wotakoi.png");
JLabel BBURGER = new JLabel(bburger);
JButton BBURGERp1 = new JButton("+");
BBURGERp1.setBackground(Color.WHITE);
BBURGERp1.setContentAreaFilled(false);
BBURGERp1.setOpaque(true);
JTextField tf22 = new JTextField(" ",2);
JButton BBURGERm1 = new JButton("-");
BBURGERm1.setBackground(Color.WHITE);
BBURGERm1.setContentAreaFilled(false);
BBURGERm1.setOpaque(true);
ImageIcon cburger = new ImageIcon("wotakoi.png");
JLabel CBURGER = new JLabel(cburger);
JButton CBURGERp1 = new JButton("+");
CBURGERp1.setBackground(Color.WHITE);
CBURGERp1.setContentAreaFilled(false);
CBURGERp1.setOpaque(true);
JTextField tf23 = new JTextField(" ",2);
JButton CBURGERm1 = new JButton("-");
CBURGERm1.setBackground(Color.WHITE);
CBURGERm1.setContentAreaFilled(false);
CBURGERm1.setOpaque(true);
ImageIcon bchop = new ImageIcon("wotakoi.png");
JLabel BCHOP = new JLabel(bchop);
JButton BCHOPp1 = new JButton("+");
BCHOPp1.setBackground(Color.WHITE);
BCHOPp1.setContentAreaFilled(false);
BCHOPp1.setOpaque(true);
JTextField tf24 = new JTextField(" ",2);
JButton BCHOPm1 = new JButton("-");
BCHOPm1.setBackground(Color.WHITE);
BCHOPm1.setContentAreaFilled(false);
BCHOPm1.setOpaque(true);
ImageIcon cchop = new ImageIcon("wotakoi.png");
JLabel CCHOP = new JLabel(cchop);
JButton CCHOPp1 = new JButton("+");
CCHOPp1.setBackground(Color.WHITE);
CCHOPp1.setContentAreaFilled(false);
CCHOPp1.setOpaque(true);
JTextField tf25 = new JTextField(" ",2);
JButton CCHOPm1 = new JButton("-");
CCHOPm1.setBackground(Color.WHITE);
CCHOPm1.setContentAreaFilled(false);
CCHOPm1.setOpaque(true);
ImageIcon lchop = new ImageIcon("wotakoi.png");
JLabel LCHOP = new JLabel(lchop);
JButton LCHOPp1 = new JButton("+");
LCHOPp1.setBackground(Color.WHITE);
LCHOPp1.setContentAreaFilled(false);
LCHOPp1.setOpaque(true);
JTextField tf26 = new JTextField(" ",2);
JButton LCHOPm1 = new JButton("-");
LCHOPm1.setBackground(Color.WHITE);
LCHOPm1.setContentAreaFilled(false);
LCHOPm1.setOpaque(true);
ImageIcon spbolognese = new ImageIcon("wotakoi.png");
JLabel SPBOLOGNESE = new JLabel(spbolognese);
JButton SPBOLOGNESEp1 = new JButton("+");
SPBOLOGNESEp1.setBackground(Color.WHITE);
SPBOLOGNESEp1.setContentAreaFilled(false);
SPBOLOGNESEp1.setOpaque(true);
JTextField tf27 = new JTextField(" ",2);
JButton SPBOLOGNESEm1 = new JButton("-");
SPBOLOGNESEm1.setBackground(Color.WHITE);
SPBOLOGNESEm1.setContentAreaFilled(false);
SPBOLOGNESEm1.setOpaque(true);
ImageIcon spcarbonara = new ImageIcon("wotakoi.png");
JLabel SPCARBONARA = new JLabel(spcarbonara);
JButton SPCARBONARAp1 = new JButton("+");
SPCARBONARAp1.setBackground(Color.WHITE);
SPCARBONARAp1.setContentAreaFilled(false);
SPCARBONARAp1.setOpaque(true);
JTextField tf28 = new JTextField(" ",2);
JButton SPCARBONARAm1 = new JButton("-");
SPCARBONARAm1.setBackground(Color.WHITE);
SPCARBONARAm1.setContentAreaFilled(false);
SPCARBONARAm1.setOpaque(true);
//DRINKS
JLabel drinkspace1=new JLabel(" ");
JLabel drinkspace2=new JLabel(" ");
JLabel drinkspace3=new JLabel(" ");
JLabel hotOrcold = new JLabel("Choose Hot or Cold");
JLabel tea = new JLabel("Tea ");
JLabel spacetea = new JLabel(" ");
JLabel spacetea2 = new JLabel(" ");
//Tea RadioButton
JRadioButton hottea = new JRadioButton("Hot");
hottea.setBackground(Color.WHITE);
hottea.setContentAreaFilled(false);
hottea.setOpaque(true);
JRadioButton coldtea = new JRadioButton("Cold");
coldtea.setBackground(Color.WHITE);
coldtea.setContentAreaFilled(false);
coldtea.setOpaque(true);
//Tea PLUS MINUS Button
JButton teap1 = new JButton("+");
teap1.setBackground(Color.WHITE);
teap1.setContentAreaFilled(false);
teap1.setOpaque(true);
JTextField tf29 = new JTextField(" ",2);
JButton team1 = new JButton("-");
team1.setBackground(Color.WHITE);
team1.setContentAreaFilled(false);
team1.setOpaque(true);
//Tea Group
ButtonGroup teagroup = new ButtonGroup();
teagroup.add(hottea);
teagroup.add(coldtea);
JLabel coffee = new JLabel("Coffee ");
JLabel spacecoffee = new JLabel(" ");
JLabel spacecoffee2 = new JLabel(" ");
//Coffee RadioButton
JRadioButton hotcoffee = new JRadioButton("Hot");
hotcoffee.setBackground(Color.WHITE);
hotcoffee.setContentAreaFilled(false);
hotcoffee.setOpaque(true);
JRadioButton coldcoffee = new JRadioButton("Cold");
coldcoffee.setBackground(Color.WHITE);
coldcoffee.setContentAreaFilled(false);
coldcoffee.setOpaque(true);
//Coffee PLUS MINUS Button
JButton coffeep1 = new JButton("+");
coffeep1.setBackground(Color.WHITE);
coffeep1.setContentAreaFilled(false);
coffeep1.setOpaque(true);
JTextField tf30 = new JTextField(" ",2);
JButton coffeem1 = new JButton("-");
coffeem1.setBackground(Color.WHITE);
coffeem1.setContentAreaFilled(false);
coffeem1.setOpaque(true);
//Coffee Group
ButtonGroup coffeegroup = new ButtonGroup();
coffeegroup.add(hotcoffee);
coffeegroup.add(coldcoffee);
JLabel cocoa = new JLabel("Cocoa ");
JLabel spacecocoa = new JLabel(" ");
JLabel spacecocoa2 = new JLabel(" ");
//Cocoa RadioButton
JRadioButton hotcocoa = new JRadioButton("Hot");
hotcocoa.setBackground(Color.WHITE);
hotcocoa.setContentAreaFilled(false);
hotcocoa.setOpaque(true);
JRadioButton coldcocoa = new JRadioButton("Cold");
coldcocoa.setBackground(Color.WHITE);
coldcocoa.setContentAreaFilled(false);
coldcocoa.setOpaque(true);
//Cocoa PLUS MINUS Button
JButton cocoap1 = new JButton("+");
cocoap1.setBackground(Color.WHITE);
cocoap1.setContentAreaFilled(false);
cocoap1.setOpaque(true);
JTextField tf31 = new JTextField(" ",2);
JButton cocoam1 = new JButton("-");
cocoam1.setBackground(Color.WHITE);
cocoam1.setContentAreaFilled(false);
cocoam1.setOpaque(true);
//Cocoa Group
ButtonGroup cocoagroup = new ButtonGroup();
cocoagroup.add(hotcocoa);
cocoagroup.add(coldcocoa);
JLabel nescafe = new JLabel("Nescafe ");
JLabel spacenescafe = new JLabel(" ");
JLabel spacenescafe2 = new JLabel(" ");
//Nescafe RadioButton
JRadioButton hotnescafe = new JRadioButton("Hot");
hotnescafe.setBackground(Color.WHITE);
hotnescafe.setContentAreaFilled(false);
hotnescafe.setOpaque(true);
JRadioButton coldnescafe = new JRadioButton("Cold");
coldnescafe.setBackground(Color.WHITE);
coldnescafe.setContentAreaFilled(false);
coldnescafe.setOpaque(true);
//Nescafe PLUS MINUS Button
JButton nescafep1 = new JButton("+");
nescafep1.setBackground(Color.WHITE);
nescafep1.setContentAreaFilled(false);
nescafep1.setOpaque(true);
JTextField tf32 = new JTextField(" ",2);
JButton nescafem1 = new JButton("-");
nescafem1.setBackground(Color.WHITE);
nescafem1.setContentAreaFilled(false);
nescafem1.setOpaque(true);
//Nescafe Group
ButtonGroup nescafegroup = new ButtonGroup();
nescafegroup.add(hotnescafe);
nescafegroup.add(coldnescafe);
JLabel milo = new JLabel("Milo ");
JLabel spacemilo = new JLabel(" ");
JLabel spacemilo2 = new JLabel(" ");
//Milo RadioButton
JRadioButton hotmilo = new JRadioButton("Hot");
hotmilo.setBackground(Color.WHITE);
hotmilo.setContentAreaFilled(false);
hotmilo.setOpaque(true);
JRadioButton coldmilo = new JRadioButton("Cold");
coldmilo.setBackground(Color.WHITE);
coldmilo.setContentAreaFilled(false);
coldmilo.setOpaque(true);
//Milo PLUS MINUS Button
JButton milop1 = new JButton("+");
milop1.setBackground(Color.WHITE);
milop1.setContentAreaFilled(false);
milop1.setOpaque(true);
JTextField tf33 = new JTextField(" ",2);
JButton milom1 = new JButton("-");
milom1.setBackground(Color.WHITE);
milom1.setContentAreaFilled(false);
milom1.setOpaque(true);
//Milo Group
ButtonGroup milogroup = new ButtonGroup();
milogroup.add(hotmilo);
milogroup.add(coldmilo);
JLabel greentea = new JLabel("Green Tea");
JLabel spacegreentea = new JLabel(" ");
JLabel spacegreentea2 = new JLabel(" ");
//Greentea RadioButton
JRadioButton hotgreentea = new JRadioButton("Hot");
hotgreentea.setBackground(Color.WHITE);
hotgreentea.setContentAreaFilled(false);
hotgreentea.setOpaque(true);
JRadioButton coldgreentea = new JRadioButton("Cold");
coldgreentea.setBackground(Color.WHITE);
coldgreentea.setContentAreaFilled(false);
coldgreentea.setOpaque(true);
//Greentea PLUS MINUS Button
JButton greenteap1 = new JButton("+");
greenteap1.setBackground(Color.WHITE);
greenteap1.setContentAreaFilled(false);
greenteap1.setOpaque(true);
JTextField tf34 = new JTextField(" ",2);
JButton greenteam1 = new JButton("-");
greenteam1.setBackground(Color.WHITE);
greenteam1.setContentAreaFilled(false);
greenteam1.setOpaque(true);
//Greentea Group
ButtonGroup greenteagroup = new ButtonGroup();
greenteagroup.add(hotgreentea);
greenteagroup.add(coldgreentea);
//Juice
JLabel juice = new JLabel("Juice");
JLabel spacejuice = new JLabel(" ");
JLabel orangejuice = new JLabel("Orang Juice ");
JLabel spaceorangejuice = new JLabel(" ");
JLabel spaceorangejuice2 = new JLabel(" ");
//Orange Juice PLUS MINUS Button
JButton orangejuicep1 = new JButton("+");
orangejuicep1.setBackground(Color.WHITE);
orangejuicep1.setContentAreaFilled(false);
orangejuicep1.setOpaque(true);
JTextField tf35 = new JTextField(" ",2);
JButton orangejuicem1 = new JButton("-");
orangejuicem1.setBackground(Color.WHITE);
orangejuicem1.setContentAreaFilled(false);
orangejuicem1.setOpaque(true);
JLabel lemonade = new JLabel("Lamonade ");
JLabel spacelemonade = new JLabel(" ");
JLabel spacelemonade2 = new JLabel(" ");
//Lemonade PLUS MINUS Button
JButton lemonadep1 = new JButton("+");
lemonadep1.setBackground(Color.WHITE);
lemonadep1.setContentAreaFilled(false);
lemonadep1.setOpaque(true);
JTextField tf36 = new JTextField(" ",2);
JButton lemonadem1 = new JButton("-");
lemonadem1.setBackground(Color.WHITE);
lemonadem1.setContentAreaFilled(false);
lemonadem1.setOpaque(true);
JLabel applejuice = new JLabel("Apple Juice ");
JLabel spaceapplejuice = new JLabel(" ");
JLabel spaceapplejuice2 = new JLabel(" ");
//Apple Juice PLUS MINUS Button
JButton applejuicep1 = new JButton("+");
applejuicep1.setBackground(Color.WHITE);
applejuicep1.setContentAreaFilled(false);
applejuicep1.setOpaque(true);
JTextField tf37 = new JTextField(" ",2);
JButton applejuicem1 = new JButton("-");
applejuicem1.setBackground(Color.WHITE);
applejuicem1.setContentAreaFilled(false);
applejuicem1.setOpaque(true);
JLabel bananajuice = new JLabel("Banana Juice ");
JLabel spacebananajuice = new JLabel(" ");
JLabel spacebananajuice2 = new JLabel(" ");
//Banana Juice PLUS MINUS Button
JButton bananajuicep1 = new JButton("+");
bananajuicep1.setBackground(Color.WHITE);
bananajuicep1.setContentAreaFilled(false);
bananajuicep1.setOpaque(true);
JTextField tf38 = new JTextField(" ",2);
JButton bananajuicem1 = new JButton("-");
bananajuicem1.setBackground(Color.WHITE);
bananajuicem1.setContentAreaFilled(false);
bananajuicem1.setOpaque(true);
//Ice Shave
JLabel iceshave = new JLabel("Ice Shave");
JLabel spaceiceshave = new JLabel(" ");
JLabel cendolfloat = new JLabel("Ice Shave Cendol Float ");
JLabel spacecendolfloat = new JLabel(" ");
JLabel spacecendolfloat2 = new JLabel(" ");
//Cendol Float PLUS MINUS Button
JButton cendolfloatp1 = new JButton("+");
cendolfloatp1.setBackground(Color.WHITE);
cendolfloatp1.setContentAreaFilled(false);
cendolfloatp1.setOpaque(true);
JTextField tf39 = new JTextField(" ",2);
JButton cendolfloatm1 = new JButton("-");
cendolfloatm1.setBackground(Color.WHITE);
cendolfloatm1.setContentAreaFilled(false);
cendolfloatm1.setOpaque(true);
f.setLayout(new BorderLayout());
p0.add(logo1); p0.add(RName1);
RName1.setFont(myFontResta);
RName1.setForeground(Color.BLUE);
p02.setLayout(new BorderLayout());
p01_0.add(Detail);
Detail.setFont(detailfont);
Detail.setForeground(Color.BLACK);
p01_2.setLayout(new GridLayout(11,2));
p01_2.add(table);
table.setFont(mealfont);
table.setForeground(Color.BLACK);
p01_2.add(number);
p01_2.add(space1);p01_2.add(space2);
p01_2.add(food);
food.setFont(mealfont);
food.setForeground(Color.BLACK);
p01_2.add(FoodList);
p01_2.add(space3);p01_2.add(space4);
p01_2.add(drnk);
drnk.setFont(mealfont);
drnk.setForeground(Color.BLACK);
p01_2.add(DrinkList);
p01_2.add(space5);p01_2.add(space6);
p01_2.add(Price);
Price.setFont(mealfont);
Price.setForeground(Color.BLACK);
p01_2.add(total);
p01_2.add(space7); p01_2.add(space8);
p01_2.add(space9); p01_2.add(space10);
p03.add(p01_2);
p02.add(p01_0, BorderLayout.NORTH);
p02.add(p03, BorderLayout.CENTER);
p01_4.add(confirm);
p01_4.add(reset);
p01_2.add(p01_4);
//TAB 2 = Noodles
tp.add(new JScrollPane(p2),"Noodles");
p2A.setLayout(new GridLayout(11,1));
p2A1.add(MB); p2A1.add(MBp1); p2A1.add(tf11); p2A1.add(MBm1);
p2A2.add(MGB); p2A2.add(MGBp1); p2A2.add(tf12); p2A2.add(MGBm1);
p2A3.add(MH); p2A3.add(MHp1); p2A3.add(tf13); p2A3.add(MHm1);
p2A4.add(MK); p2A4.add(MKp1); p2A4.add(tf14); p2A4.add(MKm1);
p2A5.add(MR); p2A5.add(MRp1); p2A5.add(tf15); p2A5.add(MRm1);
p2A6.add(MS); p2A6.add(MSp1); p2A6.add(tf16); p2A6.add(MSm1);
p2A7.add(MTY); p2A7.add(MTYp1); p2A7.add(tf17); p2A7.add(MTYm1);
p2A8.add(BG); p2A8.add(BGp1); p2A8.add(tf18); p2A8.add(BGm1);
p2A9.add(BGS); p2A9.add(BGSp1); p2A9.add(tf19); p2A9.add(BGSm1);
p2A10.add(BS); p2A10.add(BSp1); p2A10.add(tf20); p2A10.add(BSm1);
p2A11.add(BTY); p2A11.add(BTYp1); p2A11.add(tf21); p2A11.add(BTYm1);
p2A.add(p2A1); p2A.add(p2A2); p2A.add(p2A3); p2A.add(p2A4); p2A.add(p2A5); p2A.add(p2A6);
p2A.add(p2A7); p2A.add(p2A8); p2A.add(p2A9); p2A.add(p2A10); p2A.add(p2A11);
p2.setLayout(new BorderLayout());
p2.add(p2A, BorderLayout.CENTER);
//TAB 3 = Western
tp.add(new JScrollPane(p3),"Western");
p3A.setLayout(new GridLayout(7,1));
p3A1.add(BBURGER); p3A1.add(BBURGERp1); p3A1.add(tf22);
p3A1.add(BBURGERm1);
p3A2.add(CBURGER); p3A2.add(CBURGERp1); p3A2.add(tf23);
p3A2.add(CBURGERm1);
p3A3.add(BCHOP); p3A3.add(BCHOPp1); p3A3.add(tf24); p3A3.add(BCHOPm1);
p3A4.add(CCHOP); p3A4.add(CCHOPp1); p3A4.add(tf25); p3A4.add(CCHOPm1);
p3A5.add(LCHOP); p3A5.add(LCHOPp1); p3A5.add(tf26); p3A5.add(LCHOPm1);
p3A6.add(SPBOLOGNESE); p3A6.add(SPBOLOGNESEp1); p3A6.add(tf27);
p3A6.add(SPBOLOGNESEm1);
p3A7.add(SPCARBONARA); p3A7.add(SPCARBONARAp1); p3A7.add(tf28);
p3A7.add(SPCARBONARAm1);
p3A.add(p3A1); p3A.add(p3A2); p3A.add(p3A3); p3A.add(p3A4);
p3A.add(p3A5); p3A.add(p3A6);
p3A.add(p3A7);
p3.setLayout(new BorderLayout());
p3.add(p3A, BorderLayout.CENTER);
//TAB 4 = Drinks
tp.add(new JScrollPane(p4),"Drinks");
p4.setLayout(new BorderLayout());
p4A.setLayout(new GridLayout(8,1));
p4A11.add(hotOrcold);
hotOrcold.setFont(drinksfont2);
p4A1.add(tea); p4A1.add(spacetea); p4A1.add(hottea); p4A1.add(coldtea);
p4A1.add(spacetea2); p4A1.add(teap1); p4A1.add(tf29); p4A1.add(team1);
tea.setFont(drinksfont);
p4A2.add(coffee); p4A2.add(spacecoffee); p4A2.add(hotcoffee);
p4A2.add(coldcoffee); p4A2.add(spacecoffee2); p4A2.add(coffeep1);
p4A2.add(tf30); p4A2.add(coffeem1);
coffee.setFont(drinksfont);
p4A3.add(cocoa); p4A3.add(spacecocoa); p4A3.add(hotcocoa);
p4A3.add(coldcocoa); p4A3.add(spacecocoa2); p4A3.add(cocoap1);
p4A3.add(tf31); p4A3.add(cocoam1);
cocoa.setFont(drinksfont);
p4A4.add(nescafe); p4A4.add(spacenescafe); p4A4.add(hotnescafe);
p4A4.add(coldnescafe); p4A4.add(spacenescafe2); p4A4.add(nescafep1);
p4A4.add(tf32); p4A4.add(nescafem1);
nescafe.setFont(drinksfont);
p4A5.add(milo); p4A5.add(spacemilo); p4A5.add(hotmilo);
p4A5.add(coldmilo); p4A5.add(spacemilo2); p4A5.add(milop1); p4A5.add(tf33);
p4A5.add(milom1);
milo.setFont(drinksfont);
p4A6.add(greentea); p4A6.add(spacegreentea); p4A6.add(hotgreentea);
p4A6.add(coldgreentea); p4A6.add(spacegreentea2); p4A6.add(greenteap1);
p4A6.add(tf34); p4A6.add(greenteam1);
greentea.setFont(drinksfont);
p4A7.add(drinkspace1);
p4A.add(p4A11); p4A.add(p4A1); p4A.add(p4A2); p4A.add(p4A3);
p4A.add(p4A4); p4A.add(p4A5); p4A.add(p4A6); p4A.add(p4A7);
p4A11.setBorder(lineBorder1); p4A1.setBorder(lineBorder1);
p4A2.setBorder(lineBorder1); p4A3.setBorder(lineBorder1);
p4A4.setBorder(lineBorder1); p4A5.setBorder(lineBorder1);
p4A6.setBorder(lineBorder1);
//JUICE
p4C.setLayout(new BorderLayout());
p4B.setLayout(new BorderLayout());
p4D.setLayout(new BorderLayout());
p4F.setLayout(new BorderLayout());
p4B.setLayout(new GridLayout(6,1));
p4B11.add(juice); juice.setFont(drinksfont2);
p4B1.add(orangejuice); orangejuice.setFont(drinksfont);
p4B1.add(spaceorangejuice); spaceorangejuice.setFont(drinksfont);
p4B1.add(spaceorangejuice2); spaceorangejuice2.setFont(drinksfont);
p4B1.add(orangejuicep1); p4B1.add(tf35); p4B1.add(orangejuicem1);
p4B2.add(lemonade); lemonade.setFont(drinksfont);
p4B2.add(spacelemonade); spacelemonade.setFont(drinksfont);
p4B2.add(spacelemonade2); spacelemonade2.setFont(drinksfont);
p4B2.add(lemonadep1); p4B2.add(tf36); p4B2.add(lemonadem1);
p4B3.add(applejuice); applejuice.setFont(drinksfont);
p4B3.add(spaceapplejuice); spaceapplejuice.setFont(drinksfont);
p4B3.add(spaceapplejuice2); spaceapplejuice2.setFont(drinksfont);
p4B3.add(applejuicep1); p4B3.add(tf37); p4B3.add(applejuicem1);
p4B4.add(bananajuice); bananajuice.setFont(drinksfont);
p4B4.add(spacebananajuice); spacebananajuice.setFont(drinksfont);
p4B4.add(spacebananajuice2); spacebananajuice2.setFont(drinksfont);
p4B4.add(bananajuicep1); p4B4.add(tf38); p4B4.add(bananajuicem1);
p4B5.add(drinkspace2);
p4B.add(p4B11); p4B.add(p4B1); p4B.add(p4B2); p4B.add(p4B3);
p4B.add(p4B4); p4B.add(p4B5);
p4B11.setBorder(lineBorder1); p4B1.setBorder(lineBorder1);
p4B2.setBorder(lineBorder1); p4B3.setBorder(lineBorder1);
p4B4.setBorder(lineBorder1);
//Ice Shave
p4D.setLayout(new GridLayout(3,1));
p4D11.add(iceshave); iceshave.setFont(drinksfont2);
p4D1.add(cendolfloat); cendolfloat.setFont(drinksfont);
p4D1.add(spacecendolfloat);
p4D1.add(cendolfloatp1); p4D1.add(tf39); p4D1.add(cendolfloatm1);
p4D2.add(drinkspace3);
p4D.add(p4D11); p4D.add(p4D1); p4D.add(p4D2);
p4D11.setBorder(lineBorder1); p4D1.setBorder(lineBorder1);
p4F.add(p4D);
p4.add(p4A, BorderLayout.WEST);
p4.add(p4C, BorderLayout.SOUTH);
p4C.add(p4B, BorderLayout.WEST);
p4C.add(p4F, BorderLayout.SOUTH);
p4F.add(p4D, BorderLayout.WEST);
f.add(p0, BorderLayout.NORTH);
f.add(p02, BorderLayout.CENTER);
f.add(tp, BorderLayout.WEST);
f.getContentPane().setBackground(Color.WHITE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(980,800);
f.setVisible(true);
confirm.addActionListener(this);
BBURGERp1.addActionListener(this);
BBURGERm1.addActionListener(this);
CBURGERp1.addActionListener(this);
CBURGERm1.addActionListener(this);
BCHOPp1.addActionListener(this); BCHOPm1.addActionListener(this);
CCHOPp1.addActionListener(this); CCHOPm1.addActionListener(this);
LCHOPp1.addActionListener(this); LCHOPm1.addActionListener(this);
SPBOLOGNESEp1.addActionListener(this);
SPBOLOGNESEm1.addActionListener(this);
SPCARBONARAp1.addActionListener(this);
SPCARBONARAm1.addActionListener(this);
teap1.addActionListener(this); team1.addActionListener(this);
coffeep1.addActionListener(this); team1.addActionListener(this);
cocoap1.addActionListener(this); cocoam1.addActionListener(this);
nescafep1.addActionListener(this);
nescafem1.addActionListener(this);
milop1.addActionListener(this); milom1.addActionListener(this);
greenteap1.addActionListener(this);
greenteam1.addActionListener(this);
orangejuicep1.addActionListener(this);
orangejuicem1.addActionListener(this);
lemonadep1.addActionListener(this);
lemonadem1.addActionListener(this);
applejuicep1.addActionListener(this);
applejuicem1.addActionListener(this);
bananajuicep1.addActionListener(this);
bananajuicem1.addActionListener(this);
cendolfloatp1.addActionListener(this);
cendolfloatm1.addActionListener(this);
}
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if(source == confirm)
{
seat frame = new seat();
frame.setTitle("Restaurant Ordering System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,600);
frame.setVisible(true);
frame.setLocation(100,100);
}
else
{
}
}
public static void main(String[] args) {
new menu();
}
First of all, your code is a mess. Unreadable. And you are violating a whole lot of style rules, which is going to discourage anyone from trying to read your code.
(And that kind of makes asking people to help you problematic ..... doesn't it!)
Your "Cannot find symbol" errors seem to have a common explanation. Here's the first one:
menu.java:544: error: cannot find symbol
p2A1.add(MB); p2A1.add(MBp1); p2A1.add(tf11); p2A1.add(MBm1);
^
symbol: variable MB
location: class menu
When I trawl through the code, I cannot find any declaration for a variable called MB. Or mb. Or anything resembling this.
The other errors are similar.
Basically, you have either dumped in a bunch of statements that refer to things that are not declared ... or you have somehow managed to lose the declarations.
I don't think there is much we can do you help you with your code. It is ... incomprehensible.
However, if you need to understand the various things that can cause the Java compiler to produce that error message, read this:
What does a "Cannot find symbol" compilation error mean?

How do I output the text input and combobox on JList (java)

import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JList;
public class PetUI extends JFrame implements ActionListener{
JButton btnAdd = new JButton("Add");
JButton btnCancel = new JButton("Cancel");
JTextField txtbox1 = new JTextField();
JTextField txtbox2 = new JTextField();
public PetUI(){
super("Pet Shop");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//inputs
panel1.setLayout(new GridLayout(3,4));
add(panel1,BorderLayout.NORTH);
panel1.add(new JLabel("Pet Type: "));
String [] animals = {"Cat","Dog","Rabbit"};
JComboBox petlist = new JComboBox(animals);
panel1.add(petlist);
panel1.add(new JLabel("Breed: "));
panel1.add(txtbox1);
panel1.add(new JLabel("Name: "));
panel1.add(txtbox2);
//buttons
add(panel2,BorderLayout.CENTER);
panel2.add(btnAdd);
btnAdd.addActionListener(this);
panel2.add(btnCancel);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
}
public static void main(String [] args){
new PetUI();
}
}
How do I do this? Output it to a JList.
My teacher didn't taught this to us.
And I tried to search it online but it didn't worked out.
I've been through so many YT tutorials already and it didn't worked out.
Welcome to SO. Here is something very basic to get you started:
public class PetUI extends JFrame implements ActionListener{
JButton btnAdd = new JButton("Add");
JButton btnCancel = new JButton("Cancel");
JTextField txtbox1 = new JTextField();
JTextField txtbox2 = new JTextField();
JList<String> list;
DefaultListModel<String> listModel;
JComboBox<String> petList;
public PetUI(){
super("Pet Shop");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//inputs
panel1.setLayout(new GridLayout(3,4));
add(panel1,BorderLayout.NORTH);
panel1.add(new JLabel("Pet Type: "));
String [] animals = {"Cat","Dog","Rabbit"};
petList = new JComboBox<>(animals);
panel1.add(petList);
panel1.add(new JLabel("Breed: "));
panel1.add(txtbox1);
panel1.add(new JLabel("Name: "));
panel1.add(txtbox2);
//buttons
add(panel2,BorderLayout.CENTER);
panel2.add(btnAdd);
btnAdd.addActionListener(this);
panel2.add(btnCancel);
//JList
listModel = new DefaultListModel<>();
list = new JList<>(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
panel3.add(list);
add(panel3,BorderLayout.SOUTH);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e){
StringBuilder sb = new StringBuilder((String)petList.getSelectedItem());
sb.append(" ") //add space
.append(txtbox1.getText())
.append(" ")
.append(txtbox2.getText());
listModel.addElement(sb.toString());
}
public static void main(String [] args){
new PetUI();
}
}

BoxLayout Left Alignment

I'm trying to left align the class and neutral buttons so they are in line with the left most card button. For some reason, setAlignmentX only shifts the buttons half way. Here is the code. Is there away to align the buttons?
private String[] listEntries = {"a","a","a","a","a"};
private JButton remove = new JButton("Remove");
private JList list;
private JButton b1 = new JButton("Class");
private JButton b2 = new JButton("Neutral");
private JPanel page = new JPanel(new CardLayout());
private DefaultListModel listModel = new DefaultListModel();
public Main () {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.PAGE_AXIS));
leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.PAGE_AXIS));
rightPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
leftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel title = new JLabel("Deck Constructor", SwingConstants.CENTER);
title.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//Set up Deck List
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(-1);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(150, 80));
JLabel listTitle = new JLabel("List");
listTitle.setLabelFor(list);
listScroller.setAlignmentX(LEFT_ALIGNMENT);
rightPanel.add(listTitle);
rightPanel.add(listScroller);
rightPanel.add(remove);
//Set up Card Selection
JPanel buttonPanel = new JPanel();
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
b1.setActionCommand("Class");
b2.setActionCommand("Neutral");
b1.addActionListener(this);
b2.addActionListener(this);
buttonPanel.add(b1);
buttonPanel.add(b2);
JPanel classCards = new JPanel(new GridLayout(2,3, 10, 10));
JButton card1 = new JButton("Card 1");
card1.addActionListener(this);
card1.setActionCommand("addCard");
JButton card2 = new JButton("Card 2");
JButton card3 = new JButton("Card 3");
JButton card4 = new JButton("Card 4");
JButton card5 = new JButton("Card 5");
JButton card6 = new JButton("Card 6");
classCards.add(card1);
classCards.add(card2);
classCards.add(card3);
classCards.add(card4);
classCards.add(card5);
classCards.add(card6);
JPanel neutral = new JPanel();
neutral.setBackground(Color.BLUE);
page.add(classCards, "Class");
page.add(neutral, "Neutral");
leftPanel.add(buttonPanel);
leftPanel.add(page);
setPreferredSize(new Dimension(640,640/12*9));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(leftPanel,BorderLayout.CENTER);
getContentPane().add(rightPanel,BorderLayout.EAST);
getContentPane().add(title,BorderLayout.NORTH);
pack();
setVisible(true);
}
It is not perfect solution, but you can use for example:
If you want to keep buttons default size:
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,1,2));
and delete:
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
If you want to fill buttonPanel with buttons:
JPanel buttonPanel = new JPanel(new GridLayout(1,2,2,2));
buttonPanel.setBorder(new EmptyBorder(2,1,2,1));
In (FlowLayout.LEADING,1,2) and in EmptyBorder(2,1,2,1))the 1,2 values are added to match buttonPanel and classCard hgap and vgap.

JButton exits JPanel

I have multiple cancel, ok, and save buttons. When the user clicks one of these buttons, I need it to exit the JPanel, but not the whole program. I have tried multiple times and everything I do it exits the whole program. My code is below. I know there is a lot of stuff on my code and it can get confusing. Thanks in advance.
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
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.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
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);
// Make all necessary buttons
JButton next = new JButton("Next");
JButton save = new JButton("Save");
JButton save2 = new JButton("Save");
JButton save3 = new JButton("Save");
JButton save4 = new JButton("Save");
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");
JButton ecancel = new JButton("Cancel");
JButton fcancel = new JButton("Cancel");
JButton gcancel = new JButton("Cancel");
JButton hcancel = new JButton("Cancel");
//Make the drop down lists
String[] baudrates = {"57600", "115200", "128000", "256000"};
JComboBox baudlist = new JComboBox(baudrates);
String[] baudrates2 = {"57600", "115200", "128000", "256000"};
JComboBox baudlist2 = new JComboBox(baudrates2);
String[] bytesizes = {"7", "8"};
JComboBox bytelist = new JComboBox(bytesizes);
String[] bytesizes2 = {"7", "8"};
JComboBox bytelist2 = new JComboBox(bytesizes2);
String[] stopbit = {"1", "2"};
JComboBox stoplist = new JComboBox(stopbit);
String[] stopbit2 = {"1", "2"};
JComboBox stoplist2 = new JComboBox(stopbit2);
String[] flows = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist = new JComboBox(flows);
String[] flows2 = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist2 = new JComboBox(flows2);
String[] paritys = {"None", "Even", "Odd"};
JComboBox paritylist = new JComboBox(paritys);
String[] paritys2 = {"None", "Even", "Odd"};
JComboBox paritylist2 = new JComboBox(paritys2);
//Make all necessary labels
JLabel cardLabel = new JLabel("Card Number: ");
JLabel expLabel = new JLabel("Exp. Date (MM/YY): ");
JLabel cvvLabel = new JLabel("CVV: ");
JLabel ipLabel = new JLabel("IP Address: ");
JLabel connectLabel = new JLabel("Connect Time: ");
JLabel sendLabel = new JLabel("Send Time Out: ");
JLabel receiveLabel = new JLabel("Receive Time Out: ");
JLabel portLabel = new JLabel("Port: ");
JLabel baudrate = new JLabel("Baud Rate: ");
JLabel bytesize = new JLabel("Byte Size: ");
JLabel stopbits = new JLabel("Stop Bits: ");
JLabel flow = new JLabel("Flow Con..: ");
JLabel parity = new JLabel("Parity: ");
JLabel stoLabel = new JLabel("Send Time Out: ");
JLabel rtoLabel = new JLabel("Receive Time Out: ");
JLabel portLabel2 = new JLabel("Port: ");
JLabel baudrate2 = new JLabel("Baud Rate: ");
JLabel bytesize2 = new JLabel("Byte Size: ");
JLabel stopbits2 = new JLabel("Stop Bits: ");
JLabel flow2 = new JLabel("Flow Con..: ");
JLabel parity2 = new JLabel("Parity: ");
JLabel stoLabel2 = new JLabel("Send Time Out: ");
JLabel rtoLabel2 = new JLabel("Receive Time Out: ");
JLabel portLabel3 = new JLabel("Port: ");
JLabel vendor = new JLabel("Vendor ID: ");
JLabel product = new JLabel("Product ID: ");
JLabel stoLabel3 = new JLabel("Send Time Out: ");
JLabel rtoLabel3 = new JLabel("Receive Time Out: ");
JLabel amountLabel = new JLabel("Amount: ");
JLabel textLabel = new JLabel("Display Text: ");
//Make all necessary TextFields
JTextField card = new JTextField(10);
JTextField expDate = new JTextField(10);
JTextField cvv = new JTextField(10);
JTextField ip = new JTextField(10);
JTextField ct = new JTextField(10);
JTextField rto = new JTextField(10);
JTextField sto = new JTextField(10);
JTextField port = new JTextField(10);
JTextField sendto = new JTextField(10);
JTextField reto = new JTextField(10);
JTextField comport = new JTextField(10);
JTextField sendto2 = new JTextField(10);
JTextField reto2 = new JTextField(10);
JTextField comport2 = new JTextField(10);
JTextField vendorid = new JTextField(10);
JTextField productid = new JTextField(10);
JTextField sendtime = new JTextField(10);
JTextField receiveto = new JTextField(10);
JTextField amountbox = new JTextField(10);
JTextField textBox = new JTextField(10);
JTextArea logbox = new JTextArea();
//Add components to the panels
final JPanel ethernetSettings = new JPanel();
ethernetSettings.setLayout(new GridLayout(6, 2));
ethernetSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
ethernetSettings.add(ipLabel);
ethernetSettings.add(ip);
ethernetSettings.add(connectLabel);
ethernetSettings.add(ct);
ethernetSettings.add(receiveLabel);
ethernetSettings.add(rto);
ethernetSettings.add(sendLabel);
ethernetSettings.add(sto);
ethernetSettings.add(portLabel);
ethernetSettings.add(port);
ethernetSettings.add(save);
ethernetSettings.add(ecancel);
final JPanel usbHIDSettings = new JPanel();
usbHIDSettings.setLayout(new GridLayout(5, 2));
usbHIDSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbHIDSettings.add(vendor);
usbHIDSettings.add(vendorid);
usbHIDSettings.add(product);
usbHIDSettings.add(productid);
usbHIDSettings.add(stoLabel3);
usbHIDSettings.add(sendtime);
usbHIDSettings.add(rtoLabel3);
usbHIDSettings.add(receiveto);
usbHIDSettings.add(save4);
usbHIDSettings.add(hcancel);
final JPanel usbCDCSettings = new JPanel();
usbCDCSettings.setLayout(new GridLayout(9, 2));
usbCDCSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbCDCSettings.add(baudrate2);
usbCDCSettings.add(baudlist);
usbCDCSettings.add(bytesize2);
usbCDCSettings.add(bytelist);
usbCDCSettings.add(stopbits2);
usbCDCSettings.add(stoplist);
usbCDCSettings.add(flow2);
usbCDCSettings.add(flowlist);
usbCDCSettings.add(parity2);
usbCDCSettings.add(paritylist);
usbCDCSettings.add(stoLabel2);
usbCDCSettings.add(sendto2);
usbCDCSettings.add(rtoLabel2);
usbCDCSettings.add(reto2);
usbCDCSettings.add(portLabel3);
usbCDCSettings.add(comport2);
usbCDCSettings.add(save3);
usbCDCSettings.add(gcancel);
final JPanel rsSettings = new JPanel();
rsSettings.setLayout(new GridLayout(9, 2));
rsSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
rsSettings.add(baudrate);
rsSettings.add(baudlist2);
rsSettings.add(bytesize);
rsSettings.add(bytelist2);
rsSettings.add(stopbits);
rsSettings.add(stoplist2);
rsSettings.add(flow);
rsSettings.add(flowlist2);
rsSettings.add(parity);
rsSettings.add(paritylist2);
rsSettings.add(stoLabel);
rsSettings.add(sendto);
rsSettings.add(rtoLabel);
rsSettings.add(reto);
rsSettings.add(portLabel2);
rsSettings.add(comport);
rsSettings.add(save2);
rsSettings.add(fcancel);
JRadioButton ethernet = new JRadioButton("Ethernet");
ethernet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog esettings = new JDialog(frame);
esettings.setTitle("Ethernet Settings");
esettings.add(ethernetSettings);
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.add(rsSettings);
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.add(usbCDCSettings);
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.add(usbHIDSettings);
usbh.setSize(400, 400);
usbh.pack();
usbh.setVisible(true);
}
});
final JPanel PortSettings = new JPanel();
PortSettings.setLayout(new GridLayout(3, 4));
PortSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(bcancel);
final JPanel accountPanel = new JPanel();
accountPanel.setLayout(new GridLayout(4, 2));
accountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
accountPanel.add(cardLabel);
accountPanel.add(card);
accountPanel.add(expLabel);
accountPanel.add(expDate);
accountPanel.add(cvvLabel);
accountPanel.add(cvv);
accountPanel.add(bok);
accountPanel.add(ccancel);
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
final JPanel apprvordecl = new JPanel();
apprvordecl.setLayout(new GridLayout(3, 2));
apprvordecl.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
apprvordecl.add(apprve);
apprvordecl.add(decline);
apprvordecl.add(textLabel);
apprvordecl.add(textBox);
apprvordecl.add(aok);
apprvordecl.add(acancel);
final JPanel amountPanel = new JPanel();
amountPanel.setLayout(new GridLayout(2, 2));
amountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
amountPanel.add(amountLabel);
amountPanel.add(amountbox);
amountPanel.add(cok);
amountPanel.add(dcancel);
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);
llpPanel.add(logbox);
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
}
}
I have tried multiple times and everything I do it exits the whole program.
I don't see you code for closing the dialog so I can't guess what you might be doing wrong.
In your ActionListener you would invoke the dispose method on the dialog. The code would be something like:
JButton button = (JButton)event.getSource();
Window dialog = SwingUtilities.windowForComponent( button );
dialog.dispose();
A different approach might be to use a JOptionPane, then you don't need to code your own ActionListeners. You can easily add a panel to an option pane. The code below shows two approaches:
import java.awt.*;
import javax.swing.*;
public class OptionPanePanel
{
private static void createAndShowUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo( null );
frame.setVisible( true );
// Build a custom panel
JPanel panel = new JPanel( new GridLayout(2, 2) );
panel.add( new JLabel("First Name") );
JTextField firstName = new JTextField(10);
// firstName.addAncestorListener( new RequestFocusListener(false) );
panel.add( firstName );
panel.add( new JLabel("Last Name") );
JTextField lastName = new JTextField(10);
panel.add( lastName );
int result = JOptionPane.showConfirmDialog(
null, // use your JFrame here
panel,
"Enter Name",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.YES_OPTION)
{
System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
System.out.println("Canceled");
}
// Let Option Pane build the panel for you
Object[] msg = {"First Name:", firstName, "Last Name:", lastName};
result = JOptionPane.showConfirmDialog(
frame,
msg,
"Enter Name...",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.YES_OPTION)
{
System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
System.out.println("Canceled");
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
One problem with this approach is that focus will be on the OK button instead of the text field. To get around this you can use the RequestFocusListener found in Dialog Focus.

Content in JPanel won't appear

There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame
{
public static void main(String arg[])
{
SimpleGUI f = new SimpleGUI("GUI components");
f.setSize(600,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
SimpleGUI(String s)
{
setTitle(s);
setLayout(new GridLayout(3,2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel ("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel ("Enter age:");
JTextField tf2= new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold",true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5,20);
JList list = new JList(new Object[] {"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play",ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
}
}
I've updated your code. Have a look at this version:
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame {
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SimpleGUI f = new SimpleGUI("GUI components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public SimpleGUI(String s) {
setTitle(s);
setLayout(new GridLayout(3, 2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel("Enter age:");
JTextField tf2 = new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold", true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5, 20);
JList list = new JList(new Object[]{"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play", ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
/**
* Need to add the following lines
*/
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
this.setVisible(true);
}
}
A couple of pointers:
You need to add your components to your JFrame for them to actually show up.
Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"
Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?

Categories