• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple Columns in a JCombobox

 
William Hartung
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been searching for an answer to this problem for months. I am converting a number of applications that were originally designed in MS Access to Java Swing. I have everything resolved (including reporting - thanks to JasperReports!), except for this:

I need to have a JCombobox with more than one column. I have read other solutions that specify using a custom listcellrenderer, but I have not had luck with this. Could someone please post an example of this solution? Other posts usually have someone that asks "Why on earth would you need such an animal?" For those people, let me explain:

Above all, the reason usually boils down to the fact that my users need to have the fastest (most efficient) interface possible for user entry, while maintaining accuracy. These people beat on the keys all day long, and I can't afford to have unnecessary forms pop up for them to select certain items. To be as fast an efficient as possible, they occasionally need more than one column in a combobox. The reason a combobox is used instead of a table is for screen real estate. For example - the user is presented with a screen-filling form, with many components. This form accesses a database of information. One of the comboboxes represents the current job. This could be like 134, 645, etc� and this is what the user enters at this point, and then hits tab to move on. The problem is that while the user develops a sixth sense for entering job numbers, there are hundreds of jobs and we can't afford unnecessary mistakes. The user needs one column with the job number, and the next column to supply additional information, such as the job name, so that the user is confident that the correct job was entered. I usually make these comboboxes auto popup when focused. Also, this user must start and finish the form in just a few seconds, and the mouse is almost never touched.

In MS Access, this is very easy. You specify a query in the combobox to supply the columns of data in the combobox, and you select the column containing the bound value (the value of the actual selection). You also have additional control to hide certain columns, show or hide column headers, and to specify column widths.

The only thing that I could come up with in Swing that came close was to use a plain JCombobox, change the font to monospace, and pad the values with spaces to make my "columns" line up. I kept track of my "bound values" with methods to resolve the real value from the selected index. I was hoping for a better solution that would look like the rest of my form and have border lines between the columns. Auto-completion is also necessary, but I did find some code online for this that works with a normal JCombobox.

TIA for any help.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a multi-column combobox

 
William Hartung
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a different approach than what I have seen before, so thanks for that. I don't quite have enough of an understanding of this code to tweak it, but so far I see two problems with this:

The user needs to enter a number, which represents the number in the first column, to select the item. Ex: if it was job 127 - "Fountain Hills Park", the user would just key in 127. This code doesn't respond to direct keyboard entry for selection. Even without auto-completion code, a plain JCombobox will respond to the first character entry for selection (and keep tapping 1 to cycle between 125, 126, 127 etc...). The users will not be using the mouse, and the arrow keys are not an option, unless the number they entered is wrong.

Secondly, and less importantly, it would be best if only one column is shown in the combobox when it is not popped open. All three columns are shown here, but the goal would be for the additional columns to only appear when the combobox is opened.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for autocomplete, this is a real *dodgy* workaround, but it might give you
something to play with.
the textfield is only for focus testing

not sure if you can display only part of the cell

 
Jim Huff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I would personally do (and this isn't a super technical answer for how to have multiple columns, but more an opinion on an easier way to get your issue taken care of), is to create a second component, like another combo box, or a label, or whatever, and populate it with your job name when the job number is selected from the first combo box. You could have two combo boxes that update eachother when the selected item is changed, or just have the job number combo box trigger a label to be populated with the correct name.

I actually had a very similar problem to yours, and I found the post here very helpful, but it seemed overkill for what I needed so I just went the route mentioned above. I have jobs, employees, work sites, etc. defined each with a name and a ref code, and you can select some of them from combo boxes when defining things like pay rates, but I couldn't just show the name because two employees could have the same name, and even two work sites could maybe have the same name with different reference codes for some kind of accounting reason.

Hope that might be of some help, but if it doesn't fit your needs, I definitely still appreciate the reference here for how to do it the more complicated but right way.

 
Jim Huff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh wow; I thought this was posted yesterday because I only looked at the February 28, but now I see it was posted in 2005. I assume you've found your answer sometime in the past 7 years.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!