views:

40

answers:

2

when you create a gui application in netbeans IDE,

it generates 2 files, the App.java and View.java

is it okay to include the application logic in view.java ? my intuition tells me this is a bad idea. However, I don't know how I can call the methods or somehow "hook" the App.java with View.java

So instead of having to put all my code for the actionPerformed on a button, I would just insert a method from App.java

I am quite new to Java. If someone has a good tutorial or book on Netbeans, I think I will buy it.

A: 

Depends how much code there is. If you can do whole applications with only few UI components, then you can put your logic into view.

But in slightly larger (well actually almost every) applications you need to define own classes and then create their instances. If so, try tu put all your logical behaviour in some classes and just create there instances in your View class.

For example, if you want to write some list into JTextArea, it is not very good to both create it there and write it there.

Bad approach :

public void actionPerformed(java.awt.event.ActionEvent evt) {
  // whole logic done here - bad
  List<String> list = new ArrayList<String>();
  list.add("John");
  list.add("Mike");
  list.add("Joe");
  // output - correct
  for (String s : list) {
    jTextArea.append(s);
  }
}

Good approach :

public void actionPerformed(java.awt.event.ActionEvent evt) {
  // list with data injected from another class which handles that
  List<String> list = new ListHandler().getNamesList();
  // output - correct
  for (String s : list) {
    jTextArea.append(s);
  }
}
Xorty
+1  A: 

Another good approach is to learn and use Action Classes. See How to Use Actions

Such a Action class encapsulates a unit of work ("login","print",...) and you simply attach it to one or more gui elements (JButton, JMenu, ...). If you use this concept, your application can grow more easily. Separating application logic, GUI and data is always a good idea.

A incomplete Example

public class ShowListAction extends AbstractAction {

JTextArea listArea;
YourListHandler listHandler;

public ShowListAction() {
    this.putValue(Action.NAME,"Show List");
    // this.putValue(Action.SMALL_ICON, yourIcon); // You can set various Properties for your Action...
    this.setEnabled(enabled); // You can enable/disable the Action and hence any JButton connected to it ....
}

public void setListArea(JTextArea listArea) {
    this.listArea = listArea;
}

public void setListHandler(YourListHandler listHandler) {
    this.listHandler = listHandler;
}

public void actionPerformed(ActionEvent e) {
  // Here comes the actual work

  // list with data injected from another class which handles that
  List<String> list = listHandler.getNamesList();
  // output - correct
  for (String s : list) {
    listArea.append(s);
  }
}

}

To use this, you need to create/fetch a instance of the Action within your view and attach it to e.g. a JButton with

yourButton.setAction(theAction)
tweber
Yep, Actions are very useful.
Xorty
do you have an example
beanyas
i edited my post to include a example
tweber