2

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


}
Komal Rathi
  • 4,164
  • 13
  • 60
  • 98

3 Answers3

3

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • how can I use different ActionListeners? Can you give me an example? I am new to the subject (as you could make out from my code). – Komal Rathi Nov 22 '12 at 06:22
  • But all my components that need to be displayed at run time are defined inside the actionperformed method because when a menuitem is clicked, a particular set of componnts is displayed on panel p2. These components cannot be accessed in the main class constructor because they are locally defined inside actionperformed.. – Komal Rathi Nov 22 '12 at 22:33
  • @user1844024 - I'm suggesting that you move the definition of those items out of `actionPerformed`. There's no good reason for all that UI building code to be in that method. By using a CardLayout, you can have all those components defined but only a few visible at any one time. Moving the UI code to be external to `actionPerformed` would have several benefits: it would separate the logic of reacting to events from the code to construct the ui; it would simplify the action listener; it would allow you to eliminate the clumsy `switch` statement; it would be more modular and maintainable. – Ted Hopp Nov 23 '12 at 04:56
  • @TedHopp Hi Sir. Is it possible to call a method from the method **actionPerformed()**? For example:- I want to call a _void_ method **add()** and pass 3 variables to it `add(int a, int b, int c)`, and then fetch those variables and do the process that I want to in the **add()** method. Thanks in advance. – Vibhav Chaddha Jan 11 '17 at 09:33
  • @TedHopp Sir, I cracked it. Thanks anyways. :) – Vibhav Chaddha Jan 11 '17 at 09:47
  • @VibhavChaddha - Glad you figured it out. To answer your question anyway: yes, you can do anything in `actionPerformed()` that you can do in any other method. The only thing you need to be careful about is that `this` refers to the anonymous `ActionListener` class. You need to use a qualifier if you need to refer to the enclosing instance: e.g., `Retail.this`. – Ted Hopp Jan 11 '17 at 15:29
2

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.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I recommend against ever calling doClick... you should have Action objects behind your buttons, menus, etc, which don't know (or care) about which UI elements triggered them. – sjr Nov 22 '12 at 06:09
  • @sjr: `doClick()` works well _with_ `Action`, as shown [here](http://stackoverflow.com/a/5797965/230513) and [here](https://sites.google.com/site/drjohnbmatthews/keypad-panel). – trashgod Nov 22 '12 at 12:25
  • @sjr: I have edited my code, can you please check and help me with the new problem..?? – Komal Rathi Nov 23 '12 at 03:37
1
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.

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52
  • how do I pass values from the components? (I would like to reiterate that I am fairly new to the subject..) you can explain it to me in words if you don't want to write the code. Thanks :-) – Komal Rathi Nov 22 '12 at 06:26
  • Pass them as paremeters in the constructor of SubmitListener.So you could use submitted values inside actionPerformed method.Hope i explained myself clearly. – Dangling Piyush Nov 22 '12 at 06:47
  • Can i simply call the constructor instead of calling it in addactionlistener method??So before calling the SubmitActionListener constructor, i dont have to add an actionlistener to submit1 And I can add th listener inside the constructor which receives the components..ok? – Komal Rathi Nov 22 '12 at 22:25
  • I have edited my code, can you please check and help me with the new problem..?? – Komal Rathi Nov 23 '12 at 03:38
  • I have edited my code.It is working for submit3 button.Cheers. – Dangling Piyush Nov 23 '12 at 05:08