-1

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Slim
  • 5
  • 2
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – DavidPostill Aug 17 '14 at 19:38
  • http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html – Paul Samsotha Aug 17 '14 at 19:39
  • [Use Actions for menuitems, rather than ActionListener](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html). Or at least use separate ActionListeners for your menu items. – Paul Samsotha Aug 17 '14 at 19:44
  • Given that text contains no ?, it prompts me to ask - what is our question? +1 also to @DavidPostill suggestion of an MCVE. – Andrew Thompson Aug 18 '14 at 01:38
  • @ DavidPostill The desired output is to use the user input to process information, the input is either amount of money or bankaccount number or social security number (all ints). In other words, to provide the arguments to the methods. The most frequent ´misbehaviour´is that the compiler interprets the blank input as in invalid one and for that reason failes to execute the method correctly. – Slim Aug 18 '14 at 17:40

1 Answers1

1

"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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks, I´ll be sure to check out action, have only worked with actionListener til now. – Slim Aug 18 '14 at 18:31