Search code examples
javaswinguser-interfacejmenu

Problems using JMenu with different choices


I´ve been working on a GUI and I´ve run into some problems with a JMenu. The GUI is in a separate class that takes all the actions of the program from the rest of the classes. My problem with using the menu is that I want it to do 2 things: first, to show the correct panels based upon a user choice and second, wait for user input to complete the chosen task.

I´ve arranged it into 13 different if.. else clauses depending on the user choice, this part works correctly with the nescessary input options (panels) shown that the user need to input data.

Part two when the user sees the right panels and wants to input information (int and Strings) is where things don´t go as intended. Instead of waiting for user input and then take actions based on those data the program rushes forward and continues. Since no data is entered, needless to say, the output is not the intended one.

I´ll provide part of the class as it´s quite large.

class Employee implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getActionCommand().equals("1"))
        {
            panelB.setVisible(false);                           
            panelC.setVisible(false);
            panelD.setVisible(false);
            display.setText(bank.infoBank());                       }

else if(e.getActionCommand().equals("2"))
        {
            panelB.setVisible(true);                            //Show all panels
            panelC.setVisible(true);
            panelD.setVisible(true);
            label2.setText("Accountnumber:   ");
            text2.setText("");
            display.setText("");


        }

        ...


else if(e.getActionCommand().equals("5"))
        {
            panelB.setVisible(true);
            panelC.setVisible(false);
            panelD.setVisible(true);
            display.setText("");


            if(e.getActionCommand().equals("Ok")  && !pNrText.getText().isEmpty())  //This is a JButton that when pressed should send the data to the method
            {
                if(checkInput(pNrText) == true)  //Method to validate input
                {
                    pNr = Long.parseLong(pNrText.getText());  //pNr is an input field
                    bank.addSavingsAccount(pNr); //this is a method from another class
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Only digits permitted!");
                }
            }

This last part (actioncommand equals 5) is one of the places where the program doesn´t wait for the user to input anything before it continues and thus receives no input at all to process.

This is part of an ATM-program built around different JMenus, in another place where an JRadioButton is used it works as intended but I can´t get it to work using the JMenu and I don´t want to use a JRadioButton with so many choices(13).

Any input greatly appreciated! /Johan


Solution

  • "This last part (actioncommand equals 5) is one of the places where the program doesn´t wait for the user to input anything before it continues and thus receives no input at all to process."

    else if(e.getActionCommand().equals("5")) {
    
        panelB.setVisible(true);
        panelC.setVisible(false);
        panelD.setVisible(true);
        display.setText("");
    
        if(e.getActionCommand().equals("Ok") {
    

    I don't know why you would expect this behavior. This is not a console program where a scanner waits for input. Event driven programming doesn't work like that. One event get's one response. There's no waiting. Everything in the actionPerformed happens exactly once. That means when you press 5, the corresponding if will perform. The OK-if will never be performed because no event will ever reach it because it's trapped in the 5-if. Quickes fix, would be to give the OK-block it's own else-if on the same level as the 5-if

    Like I said in my comment. Avoid all these if statement. Add an anonymous listener to each menu item.

    okItem.addActionListener(new ActionListener(){
        public void actionPerforemd(ActionEvent e) {
    
        }
    });
    

    But you can even excel beyond this and use Action. Personally I prefer to go this route for menu items, for a number of reasons.


    Also as an aside, you should be using a CardLayout to switch between panels.