Java Tips Weblog

  • Blog Stats

    • 2,065,823 hits
  • Categories

  • Archives

  • Advertisements

List Editor

Posted by Rob Camick on October 19, 2008

Wouldn’t it be nice to be able to edit the values in a JList? Well a list was designed to be display only so it would involve a major rewrite of the component to support this requirement. For this reason, the most common suggestion found in the forums is to create a single column JTable and use it as a list. This is a very powerfull solution as you inherit all the default rendering and editing capabilities of a table. But what if you just want simple editing of the values, is there another way?

I might just have a solution for you. It was inspired by a posting in the Java forums about making a table header editable. The solution uses a JPopupMenu with a JTextField for the editor. The popup is positioned over the row that was selected so it looks like you are editing the list in place. You change the value and then save it to the list using the Enter key. Or, you can cancel the editor at any time by using the Escape key like you can in any popup.

The default implementation has some restrictions to using it:

  • you must use the DefaultListModel
  • only String data will be saved back to the list

Still interested? The EditListAction class is easily added to the list as the default Action, by using it together with the ListAction class discussed in an earlier blog entry. The following code was used to create the editor pictured in the image:

Action edit = new EditListAction();
ListAction la = new ListAction(list, edit);

If you need to change the default implementation to support a different ListModel or data type then you need to extend EditListAction and make the following changes:

  • specify the ListModel you need by using the setModelClass(…) method
  • override the applyValueToModel(…) method to match your requirements

Try The Demo

Launch – Using Java™ Web Start (JRE 6 required)

Get The Code

See Also

List Action


2 Responses to “List Editor”

  1. Torgil Zethson said

    Hi camickr,

    This is a very nice idea!

    About the two limitations, one way to get around that would be to add the following method to the class:

    protected void applyNewValue(ListModel model, int row, String newValue)

    and call that method from actionPerformed, where you now call

    model.set(row, editTextField.getText())

    That would allow sub-classes to do customized handling of the new value, e.g. calling a method on the underlying object in the model, passing in the new string, or converting the entered string value to the proper object before it is entered into the model.

    applyNewValue() could be abstract, or the current functionality could be provided as a default implementation. Either way, I think this would make this class even more useful.

    – Torgil

  2. camickr said

    Thanks for the idea. I made the change but also require you to now specify the ListModel to be supported (this was hardcoded before). The editor will not display unless the current model matches the supported model.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: