views:

94

answers:

3

Hi,

I have the following requirement for a GUI, that the user will have a number of available actions to perform.
Currently, it is implemented a series of JButtons that the user presses.
But the actions are a lot, and in each version more "actions" would be possibly offered.
I am not sure how this is better presented/done in GUIs.
I have read that there are ways to form a gui describing relevant information in an xml file.
I am using Netbeans and swing. Is the xml a good idea, to describe the actions available and for example create the same number of buttons? If yes, how would I start on this?Additionally would a jtoobar be a good idea for the actions? I mean add as many buttons as needed in the Jtoolbar.
I guess this is a general question but I am not experienced in GUIs. Any help would be appreciated!

Thank you.

+1  A: 

I don't know if i did understand your question well, but it seems to me that the JButton approach isn't the most efficient way to do such thing. Imagine that the number of actions starts to be really big, drawing too much buttons leads to an unintuitive and non-appealing interface. An alternative would be using a JComboBox or a JList, to list all the actions not requiring much space and having a single button "do!" to execute the action.

I hope it was useful ;)

Fabio
@Fabio:This is a nice idea. But I have not seen much GUIs use this method, or am I wrong? Isn't a toolbar usually preferred?
It's just a way to do the thing, i havent seen many like that but it's a concept that i find to be interesting. It really depends on the number of actions you'll have, if they're few then the button/toolbar approach seems resonable but for a great number of actions (that means a great number of buttons) it's not the best way, in my opinion, because the interface becomes more and more confusing and spaceless. With JList or JComboBox (i do prefer JList) you keep things much more compact and organized.
Fabio
A: 

Contrary to popular belief around a decade ago, XML is not usually a good idea. An appropriate language to express this "programming" in is Java.

Tom Hawtin - tackline
@Tom: Why not xml? Also this means that for any new action the GUI must be re-created and redelivered, right? And how would I handle, 10 new actions added in a next release? Add 10 more JButtons?
@Tom: Also when you say an appropriate language to express this programming in is Java, how do you mean this? Creating JButtons?
+1  A: 

There seems to be two separate issues involved:

  1. How to describe and create a large number of actions?
  2. 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.

Geoffrey Zheng
@Geoffrey:Thank you very much!Some questions:1)concerning constructs not part of JDK as you say, what do you think about swingx? Should I try it or avoid it? Seems to have all you suggest. 2)Concerning the suggestion of treetable:why would I need 2 columns?Usually if the text(or icon)on the button is not clear on the use, a tooltip is used.What is the benefit of a treetable over a jlist in a collapsible pannel, I did not get.To make GUI prettier? 3)How would I map the id with my actual class to call at runtime?Through a second file or through code?I have not done this before and I am not sure.
@Geoffrey:If I use only a collapsible pannel and a tree table, is it worth it to include a third-party library for just 2 components?Is this usual in GUI projects or I should try to create them myself?Do you have a suggestion on a FOSS alternative?
@OP: 1) I haven't used SwingX but I can't imagine why you'd want to avoid it. 2) I said tree table is the most flexible. You could certainly use a list or a tree, but with tree table you can add more columns, e.g. one that shows a progress bar. 3) You still need to hard code the available actions, since the XML doesn't have action code. However if your action code is consistent and can be abstracted, for example it always gets some data from a tree node then sends a certain command to server with that data, then you could also put that in the XML as well. (Or you could even use Jython...)
Geoffrey Zheng
@OP: regarding the widgets, it's not too hard to roll your own, but it's usually better to use a good library. I've only used JIDE so I'm not qualified to judge others.
Geoffrey Zheng
@Geoffrey:Thank you very much for your help!Your comments are very helpful to me. Thanks for your time!