JButton exits JPanel - java

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.

Related

access input that get from user in UI in java and show them in console

i want save input that i get from user and save them in element .
i want to access elements that user write in my UI.
and if i want save the elements in array list which kind of array list i should build.
in my UI i have text field name and text field middle name and combo box city has got 3 city name and and a radio box that it depend sex.
in final show them in console what should i do ?
this all of my code:
package ui;
import java.awt.*;
import javax.swing.*;
public class UI extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500, 600);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
JRadioButton rb1 = new JRadioButton("man");
JRadioButton rb2 = new JRadioButton("weman");
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel,BorderLayout.CENTER);
frame.add(spanel,BorderLayout.SOUTH);
}
}
You need to use listeners to create code that runs when ui component is pressed. I added listener to every component. try it:
public class UI extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
south.addActionListener(e->{System.out.println("Save button is pressed");});
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
combo.addActionListener(e -> {
System.out.println(((JComboBox<String>) e.getSource()).getSelectedItem());
});
JRadioButton rb1 = new JRadioButton("man");
rb1.addActionListener(e -> {
System.out.println("man: " + ((JRadioButton) e.getSource()).isSelected());
});
JRadioButton rb2 = new JRadioButton("weman");
rb2.addActionListener(e -> {
System.out.println("weman: " + ((JRadioButton) e.getSource()).isSelected());
});
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
name.addActionListener(e -> {
System.out.println("name: " + ((JTextField) e.getSource()).getText());
});
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
family.addActionListener(e -> {
System.out.println("family: " + ((JTextField) e.getSource()).getText());
});
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
comment.addActionListener(e -> {
System.out.println("comment: " + ((JTextField) e.getSource()).getText());
});
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel, BorderLayout.CENTER);
frame.add(spanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
You will have to put JRadioButton into RadioGroup to select one of them.
At first, you need to check your GUI, because from skimming it works not good. Later you can get data from the different component using an appropriate listener for each component or you can use any General button to reading data from all components and print it in a console or anywhere else.
To get data from combo, for example, will be here:
String data = combo.getSelectedIndex();
More information you can get here:
How can I get the user input in Java?

Radio Button does not appear in JFrame

My JRadioButton does not appear in the JFrame.
It should appear below the Method of payment. If the user picks the creditRadioButton, the creditCardLabel and the creditTextField appears.
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class FinishTransaction extends JFrame implements ActionListener{
JLabel totalLabel;
JLabel nameLabel;
JLabel addressLabel;
JLabel contactLabel;
JLabel custPaymentLabel;
JLabel changeLabel;
JLabel methodLabel;
JLabel creditCardLabel;
JTextField totalTextField;
JTextField nameTextField;
JTextField addressTextField;
JTextField contactTextField;
JTextField custPaymentTextField;
JTextField changeTextField;
JTextField creditCardTextField;
final ButtonGroup bGroup = new ButtonGroup();
final JRadioButton[] buttons = new JRadioButton[2];
JButton checkoutButton;
static FinishTransaction fin = new FinishTransaction();
public FinishTransaction(){
super("Finish Transaction-Print Receipt");
setLayout(null);
totalLabel = new JLabel("Total Payment: ");
nameLabel = new JLabel("Name: ");
addressLabel = new JLabel("Address: ");
contactLabel = new JLabel("Contact: ");
custPaymentLabel = new JLabel("Payment: ");
changeLabel = new JLabel("Change: ");
totalTextField = new JTextField("");
nameTextField = new JTextField("");
addressTextField = new JTextField("");
contactTextField = new JTextField("");
custPaymentTextField = new JTextField("");
changeTextField = new JTextField("");
methodLabel = new JLabel("Method of payment: ");
creditCardLabel = new JLabel("Credit Card number: ");
creditCardTextField = new JTextField("");
checkoutButton = new JButton("Checkout");
buttons[0] = new JRadioButton("Cash");
buttons[1] = new JRadioButton("Credit Card");
totalTextField.setEditable(false);
changeTextField.setEditable(false);
creditCardLabel.setVisible(false);
creditCardTextField.setVisible(false);
nameLabel.setBounds(50,60,100,20); nameTextField.setBounds(130,60,200,20);
addressLabel.setBounds(50,80,100,20); addressTextField.setBounds(130,80,200,20);
contactLabel.setBounds(50,100,100,20); contactTextField.setBounds(130,100,200,20);
methodLabel.setBounds(50,130,200,20);
buttons[0].setBounds(50,150,100,20);
creditCardLabel.setBounds(90,170,150,20); creditCardTextField.setBounds(210,170,200,20);
buttons[1].setBounds(50,190,100,20);
totalLabel.setBounds(50,230,100,20); totalTextField.setBounds(140,230,200,20);
custPaymentLabel.setBounds(50,250,100,20); custPaymentTextField.setBounds(130,250,200,20);
changeLabel.setBounds(50,270,100,20); changeTextField.setBounds(130,270,200,20);
checkoutButton.setBounds(130,310,200,20);
add(nameLabel);
add(addressLabel);
add(contactLabel);
add(methodLabel);
bGroup.add(buttons[0]);
bGroup.add(buttons[1]);
add(creditCardLabel);
add(totalLabel);
add(custPaymentLabel);
add(changeLabel);
add(nameTextField);
add(addressTextField);
add(contactTextField);
add(creditCardTextField);
add(totalTextField);
add(custPaymentTextField);
add(changeTextField);
add(checkoutButton);
buttons[0].setSelected(true);
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
checkoutButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(checkoutButton.getName().equals(((Component)e.getSource()).getName())){
}
if(buttons[0].isSelected()){
creditCardLabel.setVisible(true);
creditCardTextField.setVisible(true);
}
}
public static void main(String args[]){
fin.setVisible(true);
fin.setResizable(false);
fin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fin.setSize(500,400);
}
}
The output:
You did not add buttons[0] and buttons[1] to the panel. Try to add them after this line add(methodLabel); and check the result.
add(buttons[0]);
add(buttons[1]);

Insert JTextArea into a JPanel with a JLabel

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

Java Gui writing to output file

My program is showing that it is running successfully, but my dialogue box will not show up. I am new to Java and I am not sure where I am going wrong can someone help? I am trying to write to a file that will store the input from the user
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package database;
/**
*
*
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
`enter code here`import javax.swing.*;
`public class Database extends JFrame implements ActionListener{
FlowLayout flow = new FlowLayout();
JLabel fName = new JLabel("First Name");
JTextField fName1 = new JTextField(15);
JLabel blankSpaces1 = new JLabel(" ");
JLabel lName = new JLabel("Last Name");
JTextField lName1 = new JTextField(15);
JLabel blankSpaces2 = new JLabel(" ");
JLabel age = new JLabel("Age");
JTextField age1 = new JTextField(3);
JLabel blankSpaces3 = new JLabel(" ");
JLabel email = new JLabel("Email Address");
JTextField email1 = new JTextField(30);
JLabel blankSpaces4 = new JLabel(" ");
JLabel phone = new JLabel("Cell Phone Number");
JTextField phone1 = new JTextField(10);
JLabel blankSpaces5 = new JLabel(" ");
JLabel blankSpaces6 = new JLabel(" ");
JButton enter = new JButton("Enter");
JButton reset = new JButton("Reset");
JButton exitButton = new JButton("Exit");
Container con = getContentPane();
public void Database (){
Database Dab = new Database();
con.setLayout(flow);
setTitle("Antwain's Database");
Dab.setSize(800,300);
setVisible(true);
con.add(fName);
con.add(blankSpaces1);
con.add(fName1);
con.add(blankSpaces1);
con.add(lName);
con.add(lName1);
con.add(blankSpaces2);
con.add(age);
con.add(age1);
con.add(blankSpaces3);
con.add(email);
con.add(email1);
con.add(blankSpaces4);
con.add(phone);
con.add(phone1);
con.add(blankSpaces5);
con.add(blankSpaces6);
con.add(blankSpaces2);
con.add(enter);
con.add(exitButton);
con.add(reset);
enter.addActionListener(this);
exitButton.addActionListener(this);
reset.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e1){
Object source = e1.getSource();
if(source == enter){
String fName = fName1.getText();
String lName = lName1.getText();
String age = age1.getText();
String email = email1.getText();
String phone = phone1.getText();
}
else
if(source == reset){
fName1.setText("");
lName1.setText("");
age1.setText("");
email1.setText("");
phone1.setText("");
}
else
{
// if the user clicks on the Exit button (source is Exit button)
System.exit(0);
}
}
public static void main(String[] args) {
Database dab = new Database();
}
}
A lot more you need to go, fixed something you wrongly did in your code. Hope you can identify your mistakes.
public class DataBaseFrame extends JFrame implements ActionListener {
FlowLayout flow = new FlowLayout();
JLabel fName = new JLabel("First Name");
JTextField fName1 = new JTextField(15);
JLabel blankSpaces1 = new JLabel("");
JLabel lName = new JLabel("Last Name");
JTextField lName1 = new JTextField(15);
JLabel blankSpaces2 = new JLabel("");
JLabel age = new JLabel("Age");
JTextField age1 = new JTextField(3);
JLabel blankSpaces3 = new JLabel("");
JLabel email = new JLabel("Email Address");
JTextField email1 = new JTextField(30);
JLabel blankSpaces4 = new JLabel("");
JLabel phone = new JLabel("Cell Phone Number");
JTextField phone1 = new JTextField(10);
JLabel blankSpaces5 = new JLabel("");
JLabel blankSpaces6 = new JLabel("");
JButton enter = new JButton("Enter");
JButton reset = new JButton("Reset");
JButton exitButton = new JButton("Exit");
public DataBaseFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Antwain's Database");
JPanel panel = new JPanel(new FlowLayout());
panel.add(fName);
panel.add(blankSpaces1);
panel.add(fName1);
panel.add(blankSpaces1);
panel.add(lName);
panel.add(lName1);
panel.add(blankSpaces2);
panel.add(age);
panel.add(age1);
panel.add(blankSpaces3);
panel.add(email);
panel.add(email1);
panel.add(blankSpaces4);
panel.add(phone);
panel.add(phone1);
panel.add(blankSpaces5);
panel.add(blankSpaces6);
panel.add(blankSpaces2);
panel.add(enter);
panel.add(exitButton);
panel.add(reset);
getContentPane().add(panel);
enter.addActionListener(this);
exitButton.addActionListener(this);
reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e1) {
Object source = e1.getSource();
if (source == enter) {
String fName = fName1.getText();
String lName = lName1.getText();
String age = age1.getText();
String email = email1.getText();
String phone = phone1.getText();
} else if (source == reset) {
fName1.setText("");
lName1.setText("");
age1.setText("");
email1.setText("");
phone1.setText("");
} else {
// if the user clicks on the Exit button (source is Exit button)
System.exit(0);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
DataBaseFrame dab = new DataBaseFrame();
dab.setSize(800, 300);
dab.setVisible(true);
}
});
}
}
new Database();
calls an empty Constructor, because you dont define none.
Change public void Database() {} to public Database()
Also, you have to eliminate Database Dab = new Database(); from your Constructor and change the Line Dab.setSize(800,300); to setSize(800,300);
Your Code will work when you make this changes but you should really work on your Programming Style and Code Formatting.

How to call actionPerformed() within actionPerfomed()

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.

Categories