I would appreciate if anybody can explain me MVC in swings.
I have heard that each compnent has respective model class but unable to relate it with MVC.
Kindly explain.

Dani AI

Generated

— good followup. As 's link helped you visualize MVC, here is a short, practical mapping and a few tips to make the pattern concrete in Swing.

Swing separates the data (Model) from presentation (View) and input handling (Controller), but in practice the View and Controller are often combined. Typical mappings:

  • Model: holds state and fires change events (ListModel, TableModel, TreeModel, SpinnerModel, BoundedRangeModel, ButtonModel, Document).
  • View: the Swing component and its UI delegate (painting and layout).
  • Controller: listeners, Action/Key/Mouse handlers, or the UI delegate handling input.

Models publish events and Views listen to them (e.g., TableModelListener, ListDataListener, DocumentListener, ChangeListener). Controllers update the Model; Views update when the Model fires events.

Small example (illustrates Model + View + Controller hookup):

DefaultListModel<String> model = new DefaultListModel<String>();
model.addElement("One");

JList<String> list = new JList<String>(model);

list.addListSelectionListener(new ListSelectionListener() {
  public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
      System.out.println(list.getSelectedValue()); // controller reacts, can update model
    }
  }
});

Practical tips: always modify models on the Event Dispatch Thread (use SwingUtilities.invokeLater when needed); if the UI does not refresh, check that your model fires the proper events (e.g., fireTableRowsInserted); and for larger apps consider separating controller logic into dedicated classes rather than stuffing everything into listeners.

Recommended Answers

All 2 Replies

Thanks Ezzaral.
It has given me broader picture.Now i can visualize Model View and controller seperately

Shobhit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.