2

what do I have to add to the code below so that the user has to enter a specific word i.e. "London" to open the JOptionPane input dialog box.

JFrame frame = new JFrame("JTextField"); 
JTextField textfield = new JTextField(30); 
frame.add(textfield);

At the moment I can type in anything in the text field and the dialog box will appear. I only want it to open if the user enters a specific word.

I'm using the action event with action listener and action performed to open the JOptionPane Dialog box.

public class Test9 {    
    public static void main(String[] args) {
        JFrame frame = new JFrame("JTextField");
        JTextField textfield = new JTextField(30);
        frame.add(textfield);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,200);
        JPanel panel = new JPanel();
        frame.add(panel);
        panel.add(textfield);

        textfield.addActionListener(new Action4());
    }
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161

1 Answers1

2

You can do something like this.

if(museum_name.equals("London")){
    JOptionPane.showMessageDialog(null, "  You are attending the  " + museum_name);
} else{
    // show the error message
}

Its encouraged to use equals() method for String comparison. Please note, equals() is used to compare two strings for equality, while operator == compares the reference of an object in java.


Update

To show an error message if the input is not "London", you can do something like this.

static class Action4 implements ActionListener {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {
        String museum_name = ((JTextField) e.getSource()).getText();
        if (museum_name.equals("London")) {
            JOptionPane.showMessageDialog(null, "You are attending the " + museum_name);
        } else {
            JOptionPane.showMessageDialog(null, "Wrong input!");
        }
    }
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • Wasi, with strings you can use the == operator look into string pools. Try it out. The only time you will run into issues with if you declare using new operator. My answer was trying to show a language feature, and in the hopes it would spark curiosity as to why it works. – Eddie Martinez Feb 23 '17 at 17:45
  • thank you! what does the else if statement look like so that if the user inputs something other than London it shows an error – Rebecca Wahlestedt Feb 23 '17 at 17:46
  • @RebeccaWahlestedt you can show the error message in the else condition block. now, if the user inputs anything other than `London`, the message dialog won't open. – Wasi Ahmad Feb 23 '17 at 18:03
  • @RebeccaWahlestedt you can show an error message through a message dialog. I don't know what type of error message suits your need. For example, you can show an error message through the same JOptionPane that you are using in the `if` block. – Wasi Ahmad Feb 23 '17 at 18:23
  • Can you please write the code down for me? the Joption method seems fine with the message saying "does not exist" – Rebecca Wahlestedt Feb 23 '17 at 18:31
  • see my updated answer, I have checked the code, it works correctly. just change the error message as per your requirement. – Wasi Ahmad Feb 23 '17 at 18:42
  • thanks but for some reason this only works if i type London in the dialog box. i want it so that you have to type London in the JTextField – Rebecca Wahlestedt Feb 23 '17 at 18:57
  • @RebeccaWahlestedt I understood your problem now, please see my updated code and run it. – Wasi Ahmad Feb 23 '17 at 19:04
  • thank you Wasi, that worked! now i want to duplicate the code and add another specific word. the first word was "London" the next word i want to use is "Oxford". So now if the user types London it will open a dialogbox, if they type in Oxford it should open a different (new) dialog box – Rebecca Wahlestedt Feb 23 '17 at 19:44
  • @RebeccaWahlestedt this is a different question, so you can ask another question in stackoverflow. you can put the link here (in your post), so that we can help you. Please accept this answer if it helped you achieve your previous task. – Wasi Ahmad Feb 23 '17 at 19:49
  • actually I just figured it out myself (with the help I received from you) thank you! – Rebecca Wahlestedt Feb 23 '17 at 20:00
  • @RebeccaWahlestedt best way to thank is through accepting the answer :) – Wasi Ahmad Feb 23 '17 at 20:06
  • @RebeccaWahlestedt by clicking the tick sign in the left of my answer which will turn to green when you will click, which means you have accepted it. – Wasi Ahmad Feb 23 '17 at 21:50