Aman Tewary
Aman Tewary

Reputation: 303

Event Handler Program in java...Programs give no errors but after the event action is not displayed

I imported all the necessary classes and there are no errors. But when I press enter on any of the text field, the prompt window is blank instead of a string that it intend to show. I don't if the problem is with JOptionPane or what. When I run my code for the first time it was working fine on the second run, there is a blank prompt.

public class fish extends JFrame{

    private JTextField item1;
    private JTextField item2;
    private JTextField item3;
    private JPasswordField password;

    public fish(){
        super("Title");
        setLayout(new FlowLayout());

        item1=new JTextField(10);
        add (item1);
        item2=new JTextField("Enter Txt here");
        add (item2);
        item3=new JTextField("Uneditable",20);
        item3.setEditable(false);
        add (item3);

        password = new JPasswordField("MyPass");
        add(password);

        thehandler handler = new thehandler();
        item1.addActionListener(handler);
        item3.addActionListener(handler);
        item2.addActionListener(handler);
        password.addActionListener(handler);
    }

    private class thehandler implements ActionListener{

        public void actionPerformed(ActionEvent event){

            String string = "";

            if(event.getSource()==item1)
                String.format("field1: %s", event.getActionCommand());
            else if(event.getSource()==item2)
                    String.format("field2: %s", event.getActionCommand());
            else if(event.getSource()==item3)
                    String.format("field3: %s", event.getActionCommand());
            else if(event.getSource()==password)
                    String.format("Password Field: %s", event.getActionCommand());

            JOptionPane.showMessageDialog(null, string);
        }
    }
}

public class apples{
    public static void main(String args[]){

        fish tuna = new fish();
        tuna.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tuna.setSize(350,100);
        tuna.setVisible(true);
    }
}

Upvotes: 1

Views: 152

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're displaying an empty String. Here:

public void actionPerformed(ActionEvent event){

    String string = "";

    if(event.getSource()==item1) 
        String.format("field1: %s", event.getActionCommand());
    else if(event.getSource()==item2)
            String.format("field2: %s", event.getActionCommand());
    else if(event.getSource()==item3)
            String.format("field3: %s", event.getActionCommand());
    else if(event.getSource()==password)
            String.format("Password Field: %s", event.getActionCommand());

    JOptionPane.showMessageDialog(null, string);
}

Where do you assign anything to the String variable, string? Also, you never get the text from the JTextField but rather an actionCommand String that was never assigned.

i.e.,

public void actionPerformed(ActionEvent event){

    String string = "";

    if(event.getSource()==item1) {
        string = String.format("field1: %s", item1.getText())
    }
    else if(event.getSource()==item2) {
        string = String.format("field2: %s", item2.getText());
    }
    else if(event.getSource()==item3) {
        string = String.format("field3: %s", item3.getText());
    }
    else if(event.getSource()==password) {
        string = String.format("Password Field: %s", new String(password.getPassword()));
    }

    JOptionPane.showMessageDialog(null, string);
}
  • As an aside, in a real program, you never will want to do new String(password.getPassword()), as this makes your program very unsafe as the password will be easy to steal.
  • Aside #2: at this stage in your programming education, you'll want to wrap every block in { } curly braces. Better to be safe than sorry.
  • Aside #3: You will want to learn and use Java coding conventions, including giving your class names that start with upper case letters. This way others will be better able to understand your code at a glance.

Upvotes: 3

Related Questions