Can I add to Combobox's model a different value than String? - java

I've created a Java Application Project in Netbeans. This project has the main class JFrame form. There is, inter alia, Combo Box. I need to add a quadratic function to this Combobox. And I really don't want to use for squared anything like this
^2 .
I've thought, that function might be represented by ,for example, .png file.(Like setting icon of buttons).
Is it possible to add a picture to the model of Combo Box?
Can be model of Combobox different than String?
Or any idea, how to add a function to the Combo Box without ^ or _ ?
I have this idea for few days, but I really don't know how to solve this.

The short answer is "yes".
The responsibility for displaying the value in the combo box comes down to the ListCellRenderer
Take a look at How to use Combo Boxes for details and Providing a Custom Renderer for specifics
Image from How to use Combo Boxes trail
Of course, how you achieve this is entirely up to you. I would encourage you to put something easily identifiable in the model and then let the renderer figure out how to render it. This is kind of important as you will need to know what the user has selected and identify it at some stage, and an Image might not be the best solution for this.

Related

Best way to show results with a list of options

I am trying to make a "Oregon Trail" like game with JAVA for my Computer Science class. It's all going well so far, but I would like some suggestions on ways of doing the following:
The words at the bottom "Health", "Stats", etc are buttons. I was wondering what the best way of making those buttons present information would be. Is there a way I could show them in that information in one of the bottom squares, and when a different button is clicked change the info to that one? Or would it be best to have popup frames to display the information?
IMHO, I'd go with display the content or info of the button in the panels above.
It keeps the information together in a single place and generally makes it easier to manage, no disappearing windows behind other windows for example.
You have any number of options depending on the information you want to display. You could simply use a none editable JTextArea if the information is just text, or a JList if you want to list items if the data is more structured, a JTable and even a JTree if you want to group the data into some kind of groupable hierarchy.
You could use combinations of each, based on your needs

JList text alignment

I have a JList with items that I want to show two values. Is there a way to have it show a string name and then have a right justified string to show a value. Looking something like this:
Title__________________120
Title2_________________135
Is it possible to pass in two string to an item and have the first string display on the left and the second one on the right?
Sure, implement a custom renderer. You might return a JPanel with BorderLayout as the rendering component, with the LHS text in the WEST, and the RHS text in the EAST.
Another way is to shove HTML into the default renderer (a JLabel), using an HTML table that stretches across 100% of the width. Though the custom renderer would be a better choice for a number of reasons (e.g. not presuming the type of the default renderer is a label).
BTW - perhaps you should consider using a JTable for this kind of functionality. No hacks or custom classes needed.
..does the jtable allow selecting items?
Of course! Here is an example taken directly from How to Use Tables in the tutorial. 'Jane' is selected.
A table is a little more effort to set up and get right, but it is well worth the effort.
Would a JTable perform just as a JList ..
No, the table ultimately provides more functionality. But the things it does which a list can also do, work (for the user) in much the same way.

JList with custom cell renderer vs Jtable

My current application uses a JList and everything is well (the only customization I did was to set the italic font on some of the rows).
Now I want to "upgrade" the user interface and instead of just labels in the List, I want a checkbox and a text field to be able to update the entry.
I started changing the code and adding a custom cell renderer and a custom cell model. My current problem is that the JPanel that the cell renderer is returning is not using the whole width of the container, so several list items are actually shown on the same line.
But now, I am wondering whether I should just change the whole thing to use JTable. I still need to add / remove items in the list though...
Any suggestion which one is better ? and if going with the JList, how should I go about fixing my current problem ?
In my experience using JTable is usually easier as it allows more complex data and functionality out-of-the-box. Usually when I try to do something the JList can't do, I just switch to JTable without a second thought. What you want sounds like something that should be pretty trivial to implement in a table. I suggest you try it out with some mock data to see if you can make it look and work the way you like (especially in case you want it to look like a list).
Try calling setLayoutOrientation(JList.VERTICAL) on your JList. That will restrict JList to a single column.

Java Swing: what class can be used to implement this?

What Swing class can I use to implement something like this?
Add To List http://img22.imageshack.us/img22/3260/swingwidget.jpg
EDIT: Hmm..for some reason I cannot add the image here. Well, here's the link:
Basically, I need a list table, where each column can be of different type of gui (i.e. plain text, check box, or drop-down menu).
EDIT I have re-publish the image for you ;)
You would use a JTable to implement it. Your data will be stored in a TableModel. The DefaultTableCellRenderer and DefaultTableCellEditor should do what you need, but you can also customize the rendering/behavior if necessary.
More info on renderers/editors: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender
Definitely JTable.
JTable allow you to define what render will have each column. That way you can add checkboxes, combos, textfields etc. to it.
Here's an screenshot:
alt text http://img43.imageshack.us/img43/9430/jtable.png
You can read more about it here: How to use Tables
JTable will do this for you, you're going to need to understand the MVC pattern pretty well to acheive this as you will need a custom model and renderer, but once you get the hang of it its not too hard.
David gearys book "Graphic Java Vol.2" has an excellent section on JTable, whilst this book is now fairly old I still personally think thats the best explanation of JTable I've seen.

GUI: Changing panels based on value of combo box

I have a question about GUI design, specifically with Java Swing and creating clean separation between presentation and model.
It's a bit difficult to describe, but essentially we have lots of reference data in our system (i.e. that would correspond to lookup tables in the DB). We want people to be able to edit them all from one screen.
So, in an ideal world what we'd like is a combo box in the top-left corner with a list of 'types' of reference data (so each corresponding to one table in the DB).
Then, when selected, a list of the data is populated below, also a filter (or search box). When one of these items is selected, the panel to the right is activated which will allow the actual data to be edited.
Now, here's the problem: each type of data we need to edit is different, so it has different fields etc. We could go with a generic solution but I'm not really a fan of them - there are lots of different validation rules for each etc, even for different clients, and it would be a nightmare to manage.
We're using the Presentation Model pattern to achieve some degree of separation between GUI code and the model but I can't think of a clean way of doing this which doesn't somehow blur the line of responsibilities a bit.
What are the ways you have solved problems like this?
[Note: apologies for the long question, hope it's understandable, I can re-phrase if necessary]
You could use the Factory Pattern to create a UI widget for the element that you are selecting. You could also use it to create a validation rule object depending on the type. This would give you some of the flexibility you desire.
So you can have something like:
IWidget widget = UIFactory.createFor(myObject.getType())
That can be invoked on the selection event to create the right widget to edit the selected element.
The IWidget could have methods such as:
validateData()
refreshData()
setDataElement(IDataElement element)
That would allow you to treat all UI widgets generically, but still have a special UI widget for each element type. I am assuming that the elements that you are selecting from the table all implement some IDataElement interface that contains the getType() method.
I used this solution tied together with the Eclipse Extension mechanism to plug-in new UI elements into my "base" solution, to have an extensible core and a high level of reuse. You could achieve something similar by injecting types and widgets into your factory, either manually or with Spring.
If you dont want to go down the generic path, you could have your model hold a mapping of combobox item -> panel name for use with a CardLayout. You could then create custom panels for the editing each of the reference data types. When the combo box selection is changed, you can save the current state in your model, request the panel name of the current selection, prepare your next panel for display and then have your CardLayout show it.

Categories