Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom component for JList instead of just strings

Tags:

java

swing

I've been trying to freshen up on my Java knowledge and I've been building a small GUI program and I've been running into a bit of a problem.

Basically, I have a JList which I'm currently populating with strings from an object from one of my classes which implement AbstractListModel which we can call my ItemList class. It contains an ArrayList of objects of the type Item which implements Serializable.

But, what I'd like to do is rather than populate my JList with a bunch of strings I'd like to populate it with some kind of string + JTextField combination so I can see one property of each Item object while also being able to update another property by changing the JTextField.

Now, what I'm looking for is the simplest possible way of doing this, and I am assuming there is a (relatively) simple way to do this since it's such a common thing to want to do in a GUI application (although I wouldn't put it past Java and Swing to make it convoluted and complicated).

So, what is the correct way of doing this?

like image 546
mludd Avatar asked Jul 29 '12 00:07

mludd


1 Answers

No need to ever use String objects. Instead:

  • Put Item objects in the JList.
  • Add a ListCellRenderer to the list, to show the Item object the most user friendly way.
  • When the user selects an item, show the details in a different place (I'm thinking a panel with 2 columns of labels and text fields, and two rows - one for each attribute, and perhaps a button to Save)

The edit controls would best be encapsulated in a panel that can then hidden when not required, & put in a variety of places, e.g.

  • Below the list
  • In the main part of the GUI
  • displayed in a JOptionPane or a (modal or not) JDialog

Here is an example of placing the 'view/edit panel' (the file details) below the selection component (the table).

like image 96
Andrew Thompson Avatar answered Dec 20 '22 06:12

Andrew Thompson