Search code examples
javaswingnetbeans6.8

Netbeans 6.8: Actionlistener confusion


I have 2 modules: BrowserLibrary and BrowserViewer.

Browser contains gui like any browser should.

so inside BrowserLibrary module I have a class like so:

public class BrowserController implements ActionListener{

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().getVarName() == "navButton")
        System.out.println("clicked");
    }

}

and in BrowserViewer module's BrowserTopComponent.java:

private javax.swing.JButton navButton;
navButton.addActionListener(BrowserController);

I am not sure if this is the right design I should be pursuing. Should BrowserLibrary only be used to load the 3rd party JARs and nothing else ? IF that is the case would I end up with 3 modules: BrowserLibrary, Browser, BrowserViewer ? How would the dependencies work in this case ? Does Browser depend on BrowserLibrary, and BrowserViewer depend on Browser ?

So inside Browser module, I would have BrowserController class which implements ActionListener ?

Also how can I get the variable name of the e.getSource() object ? in this case "navButton" is the variable name of the JButton.

Should I be using Lookups ? How would that be incorporated here ?


Solution

  • The number and composition of modules should reflect the modularity of your design. A good question to ask is "Can this module be reused without the others?" If the answer is no, then the modules in question are tightly coupled and should probably only be one module. For example, a module that handles the HTTP protocol could be used independently of the browser. The actions for navigating in a browser are probably rather browser centric and probably cannot be used by themselves.

    Variables are run time instances, but variable names are really compile-time artifacts that programmers use to refer to a variable. That said, all swing components do have a name property that can be used for identification. Do note that it is the developer;s responsibility to ensure uniqueness.