Java Tips Weblog

  • Blog Stats

    • 2,614,399 hits
  • Categories

  • Archives

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 (it allows you to change the value of a given row in the model)
  • 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:

JList list = new JList( myDefaultListModel );
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

Get The Code

EditListAction.java

See Also

List Action

6 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.

    Cheers,
    – 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.

  3. Anonymous said

    Hello Rob,

    thanks for the code. I just tested under java 25 and found that EditListAction doesn’t work if a String array is passed to the JList constructor. Maybe that’s what you meant when writing you must use the DefaultListModel. Still I thought that if only String data will be saved I could use a String[] parameter. So I would prefer the JList creation in the example implementation code to look like JList list = new JList(myDefaultListModel); to prevent any misunderstanding.

    Thanks again

    Jörg

    • Rob Camick said

      When creating a JList with a String array a simple anonymous inner class is used for the model and only contains getSize() and getElementAt(…) methods. There are no methods to update the model, which is why the simple implementation requires the DefaultListModel.

    • Anonymous said

      EditListAction

      If you which to use a different model or different data
      If you wish to use a different model or different data,

      In case you’d like to “modernize” the ActionListener:
      /*
      * Create the popup editor
      */
      private void createEditPopup()
      { ….
      editTextField.addActionListener(e ->

      (Typing code looks horrible, so I drop the rest.)

      ListAction

      By default the Enter will will be used to invoke the Action
      By default the Enter key will be used to invoke the Action

      from the keyboard although you can specify and KeyStroke you wish.
      from the keyboard, although you can specify any KeyStroke you wish.

      All the best

      Jörg

Leave a comment

 
Design a site like this with WordPress.com
Get started