There seems to be two separate issues involved:
- How to describe and create a large number of actions?
- How to represent a large number of actions in GUI?
@Tom's answer is for #1, and @Fabio's for #2.
You can use any textual format for #1, xml, csv, whatever you like. It's indeed a good idea to separate that from code. XML is great for hierarchical data, so it may be overkill if you just need a flat list:
<doc>
<action name="Action 1" id="ACT1" description="blah blah" icon="icon1.gif"/>
<action name="Action 2" id="ACT2" description="yada yada" icon="icon2.gif"/>
...
</doc>
But parsing such a simple XML is basically free, so you may as well just use it. You do not, however, need a full-featured XML GUI toolkit like SwiXML, unless you want to add many other GUI widgets with complex layout to your app.
Note the attributes I have in the above sample. id
would map to a unique action command. You can display description
and icon
(I suppose you use icon already) any way you want. You could also have other properties like mnemonic, accelerator, etc., at which point using XML starts to pay off: you can add arbitrary attributes that you need.
One obvious omission in the XML is the actual actions themselves. I do not think you should put Java code in XML. It defeats the separation of concern. Instead you can define your action code in a generic way (e.g. extend AbstractAction
) and map them with the action IDs. If you do use AbstractAction
, you can trivially map your attributes to action property keys like Action.NAME
, Action.LONG_DESCRIPTION
, etc.
Now you have parsed the XML into a list of action objects, and here comes the second question: how do you display them?
JList
(per @Fabio) is indeed the most efficient way. It's much more compact than a whole bunch of individual buttons, yet unlike JComboBox
you can see many items at once, and you can easily add sort/search/filter.
But list is not very flexible. You could use a custom ListCellRenderer
to display icon and tooltip (for description), but you'll start to stretch it when you want to group items.
I think the most flexible way would be a tree table, which allows you to have multi-level hierarchy. You can start with 2 columns, the first column showing action names hierarchically, the second column showing description.
You could put the table in a collapsible panel so that it can be hidden when user wants to focus on the results.
Now w.r.t. JToolBar
, you're right that it's standard, however as Fabio's comment pointed out, it's bad usability when you have too many buttons on a toolbar (like M$ Word before ribbon).
What would be a great use of a toolbar, however, is to allow user to place actions of their choice onto the toolbar, like most popular desktop apps do. You could use a "Customize Toolbar" dialog, or simply let user drag-n-drop items from your list or table.
Unfortunately, many of the GUI constructs that I mentioned above are not available in JDK. You can find all of them (tree table, search/filter, collapsible panel, customizable toolbar, etc.) from the excellent commercial JIDE libraries (disclaimer: I don't work for them, though I sometimes wish I do), or you can find FOSS alternatives.