0

Till now I have worked with some java swing code with everything in one class. I want design and develop better UI application. Trying to use MVP/MVC for swing UI, didn't found any concrete example anywhere.

I want to develop swing UI form with submit button. i.e. this main panel representing user form contains three panels: - Header panel with combo box and text fields. - Center panel with table pane. - End panel with some combo box and text fields.

So basically I have written 4 UI classes with no action listeners, one for main and three for header,center and end panel. Executing Main panel show me all the components.

I want to provide runtime data to combo box and when user submit I should get selected values from combo box and other fields. How should I design using MVP/MVC. Could any one provide some explanation or give me a link to any reference or example?

ginger522
  • 3
  • 3

1 Answers1

0

Look at this: Java - Learning MVC

The MVC pattern requires these 3 classes.

  • theModel: handles data
  • theView: handles GUI
  • theController: handles listeners.

You may need some understanding of the Observer pattern first.

the design may be as follows:

public class Test {

    public Test(){
       MyModel      model      = new MyModel();
       MyController controller = new MyController(model);
    }
}

controller class

public class MyController implements ActionListener {
    private MyView view;
    private MyModel model;

    MyController(MyModel theModel){
        model = theModel;
        view = new MyView(this);
        model.register(view);
    }

    // implement actionPerformed here
}

view class

public class View implements Observer {
    private MyController controller;

    public View(){
         // Swing Components here
         JButton button = new JButton();
         button.addActionListener(controller);
         add(button);
    }
    // notifyObserver method implementation
}

model class

public class MyModel extends Observable {
    // handle state/data changes here

}
Community
  • 1
  • 1
Stelium
  • 1,207
  • 1
  • 12
  • 23