• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI and good OO design? Seperate panel classes or all in frame?

 
Ish Del
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a design question in regards to working with a gui. Ill use an example to illustrate my question better and ask what you all do in your code. Lets say I had a Frame and i wanted to have 2 panels. 1 on the left with a list of movies and one to the right of that with box art. I want to have a listlistener on the left panel so that as i click the different movie titles the box art on the right panel changes.

For something like this should I:

A) Make a display class that extends frame, a JPanel class for the list panel , a JPanel class for the art panel. ?

I always start with this sort of design but then end up getting stuck. I generally need to start passing references of the list panel object in the frame display class to methods in the art panel object or vice versa. It just doesnt seem like good design but I could be wrong. It is much easier for me to just use the one display class and create the 2 panels in that class. that way i dont have to do all this reference passing.

I maybe totally missing an easy way to do this as I am new to this. But how would you guys tackle something like this?


Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically any application should have only one frame. For everything else, you should be using dialogs or switching child containers with card layout or something similar as required.
Thus all children would have one thing in common, the parent frame.
Provide a getInstance method in the parent frame. When any selection change happens in the list panel, obtain a reference to this parent frame instance and pass the required data to the parent frame, by providing and invoking appropriate pass through methods in the parent frame. The parent frame then can delegate this data for other (box art in your case) panel to be processed.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be to use a Model object that represents the data in the List. Create it first, then pass it to both the Panel that displays the List and the Panel that displays the Details. The List panel uses the Model to produce the list, and when a value on the List is selected, it passes that information on to the Model. The Details panel listens to the Model for the selection change event, then displays whatever the latest selection is. The Display panel, List panel, and Frame don't need to maintain references to each other, just to the shared data.

The DefaultListModel is a good place to start for creating your representation of the List of movies, as it maintains a collection of Objects and can be passed into a JList to generate the display from the data. It does not have a concept of a 'selected' item so you might want to sub-class into a 'SelectableListModel' or something like that. You would want to make sure that there is a way of maintaining the selected item, methods for getting and setting the selected item, and an event called when the selection happens. You might also consider if your application allows multi-select, and handle that. In this way, your could create selection listeners on your JList which simply call the SelectableListModel's setSelection method, and that would trigger a 'ListSelectionChanged' event from the Model. The Details panel would be listening for the ListSelectionChanged event from the Model, and would know when and how to get the data to display.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!