Franklin
Franklin

Reputation: 1801

Programming with a Java MVC approach using NetBeans GUI builder

I've been tasked with making a GUI that essentially takes a bit of user input and does some folder/file manipulation on various drives accessible by the machine the program is being run on. While designing this GUI, I'm starting to realize that MVC will make my life much easier and anyone else who decides to modify code, but I can't really see how this can be done via NetBeans.

I've done a bit of reading up on this topic, and I can't really see any clear cut answers as to whether or not this can be done on NetBeans. Surely it can be done if I programmatically build the GUI, but that somewhat defeats the purpose of why I chose to use NetBeans.

Upvotes: 1

Views: 8267

Answers (3)

Ivan Viktorovic
Ivan Viktorovic

Reputation: 143

I also hit this problem and i found a link which gives a good example how to seperate the controller from the view using the NetBeans GUI builder.

Here is the link.

Upvotes: 0

Will Hartung
Will Hartung

Reputation: 118784

Netbeans is fine to do this.

The key thing to realize is that while all of the basic Swing components are MVC, for the most part you don't interact with them that way. A simple text field has it internal model, but that model isn't your model, the text field is more a primitive.

Your model deals with higher level events (button actions and what not), rather than button presses and arrow moves and mouse clicks.

So, for high level MVC, the primary mechanism of communication is through PropertyChangeListeners. And the basic task of building your app up is wiring the PCLs of the assorted data elements along with their GUI components together.

For example, a simple case is you have a list of items. And that list is rendered on the screen via a JTable, and that table is on a JPanel.

Your list has it's own model, i.e. it's not simply a Java List. It's not a List because standard Java Lists don't support PCL notifications. But your Model would obviously wrap such a List.

Now, the next question is how do you wire up JTable to be associated with your List model.

One, you could subclass JTable and bind it to your Model. Or, more simply, you use the JTable as a primitive, and let the enclosing JPanel manage the interaction between your Model and the JTable.

That means having your JPanel implement PropertyChangeListener, and then, when wiring everything up, you do something like this:

ListModel myModel = new ListModel();
ListPanel myPanel = new ListPanel();
myModel.addPropertyChangeListener(myPanel);

Now, whenever your ListModel is changed, is will notify the ListPanel.

On your ListPanel you can having something like:

@Override
public void propertyChange(PropertyChangeEvent evt) {
    if (evt.getPropertyName().equals(ListModel.CHANGED)) {
        ListModel model = (ListModel) evt.getSource();
        DefaultTableModel tm = (DefaultTableModel) listTable.getModel();
        tm.setRowCount(0);
        for (String s : model.getList()) {
            tm.addRow(new Object[]{s});
        }
    }
}

Now, you can see this simply reloads the entire table model, but you can make your property changes as fine grained as you want. You can also see that if this was some other model (like a Person or something) you can populate individual text fields and whatnot on the panel.

This is a pretty simple GUI, but it shows the fundamentals of how this all wires together. I think a bit of this is lost in the Swing examples which are great for one panel screens but don't scale at all when you start adding other views.

Your JPanels basically become combined VC, as your GUI gains complexity you can factor those kinds of things out, but its works pretty well for reasonable amounts of screens and such.

Upvotes: 4

miku
miku

Reputation: 188164

There are two ways in which Netbeans can help you leverage its codebase: GUI Builder (1) and NB Platform (2).

(1) Netbeans had for a while one of the better drag-n-drop GUI builders in the Java world, codenamed Matisse.

That said, it's been a long time since I worked with it - and I never really liked the generated code, it wasn't very comprehensible (which of course is not the purpose of auto-generated code). For more complex UIs we hand-wrote the layout and the work was bearable, even if not the most pleasant. For simple UIs, I'd try GUI Builder again, for complex UIs with a lot of wired logic, I'd probably still would write it by hand.

To see how the GUI Builder works, take a look at one of the many tutorial videos, e.g. this one:

(2) Netbeans Platform is to Netbeans, what RCP is to Eclipse. A rich set of components developed for an IDE, that can be reused. I briefly looked into NB Platform and we would have used it, if the project didn't change course. Maybe this SO question can shed more light on this aspect: Which Rich Client Platform to use?.


Concerning MVC. There was JSR 296, a generic Swing Application Framework, that looked somewhat promising, but was withdrawn in 2011. That did not stop people to fork it and work on it, as this project shows: Better Swing Application Framework, with a release in mid 2012. Even if you do not use such a framework, please do not put all code in one class (as you mention in you comment), but create a simple model/controller and keep the UI components separate. It does not need to be fancy for a simple app, a minimal MVC-ish separation of concerns might suffice.

Upvotes: 2

Related Questions