Search code examples
javaswingeventsjoptionpane

Event calling through showInputDialog


I'm build one application that needs a request from the user. The first version of the app I made one input window myself, but I found better change to the showInputDialog since it's it's a pre-molded tool from JOptionFrame. Now I'm having problems with the event trigger; check out the code below:

SearchScreen:

public class SearchScreen extends EventSearch{
    ...
    public SearchScreen(){
        userQuery = (String) JOptionPane.showInputDialog("Type Keywords in english to be requested below:");    
    }
    ...
}

EventSearch:

public class EventSearch extends TabBuilder{

    public EventSearch() {

    }

    public void actionPerformed(ActionEvent Ev) {
        try {
            System.out.println("worked");
        } catch (IOException e1) {
            e1.printStackTrace(); //print failure
            JOptionPane.showMessageDialog(null, "FAIL");
        }
    };
}

TabBuilder:

public class TabBuilder implements ActionListener {
    .....
}

Then I ask, How am I supposed to call an event through showInputDialog? is it possible? Who is gonna be the listener? Thanks in Advance


Solution

  • I found my own answer - its indeed to carry on the code the Event Search class and pull the trigger to one action like this, Instead it's better to do:

    public SearchScreen(){
    
            userQuery = (String) JOptionPane.showInputDialog("Type Keywords in english to be requested below:");
    
                try {
    
                     //Your Action with the String  
    
                } catch (IOException e1) {
    
                    e1.printStackTrace(); //print failure
                        JOptionPane.showMessageDialog(null, "FAILURE");
                }
    
        }