2

I'm trying to call this event below; I create the frame with TabBuilder (since is part of my application) then it calls the Search screen which is popping up; but the event of the search with key bind or simple click on the button is not working and of course I'm doing something wrong but I don't know what since I'm a little bit new in Java. Please could anyone help me?


SearchScreen:


public class SearchScreen extends EventSearch{
    
    public static void main (String[] args){
    
        SearchScreen s= new SearchScreen();
    }
    
    public void SearchScreen(){
    
        TabBuilder tb = new TabBuilder();
          tb.searchTab();       
    }
}

EventSearch:


public class EventSearch extends TabBuilder{
    String userQuery;
    String key = "ENTER";
    KeyStroke keyStroke = KeyStroke.getKeyStroke(key);
    
    public EventSearch(){
    
        btSearch.addActionListener(this);
        txtSearch.getInputMap().put(keyStroke, key);
        txtSearch.getActionMap().put(key, enterAction);
    }
    
    Action enterAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
        try{
            System.out.println("worked");
                } catch (IOException e1) {
                    e1.printStackTrace(); //print failure
                    JOptionPane.showMessageDialog(null, "HTTP request failure.");
                }   
            }
        };
     }

TabBuilder:


public class TabBuilder implements ActionListener {

    protected JButton btSearch;
    JMenuItem close, search;
    protected JTextField txtSearch;
    protected JFrame searchFrame = new JFrame();
    
    public void TabBuilder(){
    }
    
    public void searchTab(){
    
        JLabel lbSearch;
        JPanel searchPane;
        
        btSearch= new JButton("Search");
        lbSearch= new JLabel("Type Keywords in english to be searched below:");
        lbSearch.setHorizontalAlignment(SwingConstants.CENTER);
        txtSearch= new JTextField();
        searchPane=new JPanel();
        searchPane.setBackground(Color.gray);
        searchPane.add(lbSearch);
        searchPane.add(txtSearch);
        searchPane.add(btSearch);
        searchPane.setLayout(new GridLayout(3,3));
        btSearch.setEnabled(true);
        searchFrame.add(searchPane);
        searchFrame.setTitle("SHST");
        searchFrame.setSize(400, 400);
        searchFrame.setVisible(true);
        searchFrame.setDefaultCloseOperation(1);
    }
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==close){
            System.exit(0);
        }
        if(e.getSource()==search){
            SearchScreen s = new SearchSreen();
        }
    }
}
Community
  • 1
  • 1
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow! – Andrew Thompson Jul 10 '13 at 14:33
  • Actually i don't read all the code, but reading only the main, you have a method with same signature as its constructor (void difference) and is never called.. – nachokk Jul 10 '13 at 14:36
  • @AndrewThompson Sorry, but I removed everything I could, more than that the sample wont make sense - if you disagree suggest an edit, therefore I can correct it. – Victor Oliveira Jul 10 '13 at 14:36
  • 1
    *"I removed everything I could"* There is more to an SSCCE than just making it 'short'. Try actually *reading* the article, and note that an SSCCE can easily be 123+ lines before many will accuse you of posting code that is not 'short'. Speaking of which: A single blank line of white space in source code is *always* enough. – Andrew Thompson Jul 10 '13 at 14:38
  • @nachokk could you be more specific please? I couldn't fully understand you - thanks – Victor Oliveira Jul 10 '13 at 14:38
  • @VictorOliveira : Try to change this line `txtSearch.getInputMap().put(keyStroke, key);` to `txtSearch.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, key);`, inside `EventSearch` class's constructor and see does that resolves the issue at hand...!!! – nIcE cOw Jul 10 '13 at 14:41
  • @VictorOliveira i answer, hope it help – nachokk Jul 10 '13 at 14:46
  • @VictorOliveira : Sorry about the edits, just doing my edits, when your edits clashed with mine, please do proceed, as you wanted to change your post as need be. – nIcE cOw Jul 10 '13 at 15:00
  • @nachokk still not working =/ I edited the code, please take a look again if you find anything that could be helpfull - I made some independent tests with the classes and by itself they work good..when I connect them they just start to mess up the code...The last test I ran is not openning the search screen and when it calls the extended EventSearch constructor it returns me one null pointer exception.. – Victor Oliveira Jul 10 '13 at 19:13
  • @nIcEcOw thanks for the help brow, check the edit I made few minutes ago and the answer up here I gave to nachokk. Ty – Victor Oliveira Jul 10 '13 at 19:14
  • @VictorOliveira your code will throw nullPointerException cause you never init `btSearch` – nachokk Jul 10 '13 at 19:18
  • I initiated it on the top of TabBuilder - check it out – Victor Oliveira Jul 10 '13 at 19:27
  • @nachokk is it possible to override the ActionPerformed like I did? – Victor Oliveira Jul 10 '13 at 19:29
  • @VictorOliveira no, you don't `protected JButton btSearch;` you only have this and is null.. yes you can also you should add @Override annotation when you override – nachokk Jul 10 '13 at 19:39
  • @nachokk the hell is a button, what should I declare on it? >.< its protected cause I was calling him on EventSearch – Victor Oliveira Jul 10 '13 at 19:45
  • you have to do `btSearch = new JButton()` .. – nachokk Jul 10 '13 at 19:51
  • @nachokk oh bite me - i should read more carefully the code. I get really stressed with those kind of errors; the same applied to TxtField. It's not returning anymore the nullpointerexception but the screen still doesnt pop up - I'm working on this now – Victor Oliveira Jul 10 '13 at 20:00
  • @VictorOliveira concrete inheritance has this things .. – nachokk Jul 10 '13 at 20:04
  • @nachokk which literature do you use as guideline for java brow? – Victor Oliveira Jul 10 '13 at 20:07

1 Answers1

3

You write this actionListener

public void actionPerformed(ActionEvent e){
            if(e.getSource()==close){
                System.exit(0);
        }

            if(e.getSource()==search){
                TabBuilder tb = new TabBuilder();
                tb.searchTab();

            }
        }

and you added to btnSearch.addActionListener(this) , your actionListener never would do anything.

And for your KeyBinding happens something similar , you add the action to the txtSearch and then you are asking if the source is the e.getSource()==btSearch

And for KeyBindings you can use Constants to specify when they have to be binded. JComponent.WHEN_FOCUSED, JComponent.WHEN_IN_FOCUSED_WINDOW , JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

For example :

txtSearch.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, key);

How to use KeyBindings

nachokk
  • 14,363
  • 4
  • 24
  • 53
  • +1, those seems to me too, the real issues behind the problem :-) – nIcE cOw Jul 10 '13 at 14:48
  • I forgot to change that little mistake on TabBuilder, I made that to run one test and forgot to remove after all - It was calling the searchTab twice; But I'm still missing the part of the Event - I removed the btSearch.addActionListener(this); since you were right, there is no listener to the button but the keyBind still doesn't work even after the remove of the e.getSource(); – Victor Oliveira Jul 10 '13 at 14:56
  • @VictorOliveira : Please reedit, your post, Sorry, my edits clashed with yours at the same time, Please do re-edit it, if the outcome is not what you wanted!!! – nIcE cOw Jul 10 '13 at 15:04
  • @nIcEcOw as far as I'm seeing, the re-edit up there is mine, the last one I made, You can re-edit if you want – Victor Oliveira Jul 10 '13 at 15:08
  • @VictorOliveira you still has the same code in your question... also this method is useless `public void SearchScreen(){ TabBuilder tb = new TabBuilder(); tb.searchTab(); }` – nachokk Jul 10 '13 at 15:12
  • @nachokk edit the code again please, I need to go to the unviersity to do one test and I will be back later, thanks brow – Victor Oliveira Jul 10 '13 at 15:17