jcombo help!
Hey guys!
Just wondering whether it is possible to implement multiple column jcombo boxes - and only display the second column. In vb this is quite easy, but trying to implement in java I'm finding it to be difficult.
I thought i would populate doing something similiar to the following where the values are read from a database table, but rather than display the second value obtained in the second column it adds another row to the combo box.
note 'name' is the name of the JComboBox:
int y=0;
int x=0;
while (rs.next())
{
x=0;
value [y][x]=rs.getString (1);
name.addItem(value[y][x]);
System.out.println(value[y][x]);
x=x+1;
value [y][x]=rs.getString (2);
name.addItem(value[y][x]);
System.out.println(value[y][x]);
y=y+1;
}
Any help will be gretaly appreciated!!!
Thanks in advance!
Ev.
[970 byte] By [
evian] at [2007-11-11 8:27:46]

# 1 Re: jcombo help!
Just wondering whether it is possible to implement multiple column jcombo boxes - and only display the second column.
Wondering why you would want to use a single combo box if your only wanting to display one column.
-PD
Wh0M3 at 2007-11-11 22:35:25 >

# 2 Re: jcombo help!
Two columns are required- the first (hidden column) is the ID of the second column which has a descriptive meaning to the user. For example staffID and name - selecting name would be more ideal for the user rather than trying to figure which staff member is associated with which ID.
However the staffID value is more useful to the system where the id needs to be stored in the database table.
I know I could possibly implement an object class for the combo but was just wondering why my code would not work or if there was a simpler alternative?
Prehaps using JTables??...
evian at 2007-11-11 22:36:19 >

# 3 Re: jcombo help!
Since java is more object oriented than vb, you should encapsulate your problem in a class. the class can contain an id and a displaystring.
the toString() method is by default used to display the text in the control, so you just return the name in the tostring method. with getSelectedItem you can get the whole object from the control, that was selected. then you can cast it to your class and access the id.
eg.:
public class Product{
private String name;
private long id;
public Product(long id, String name){
this.id = id;
this.name = name;
}
public String toString(){
return name;
}
}
in the actionlistener for changes on a jcombobox you can do:
Product x = (Product) combobox.getSelectedItem();
// do sth with selected object
# 4 Re: jcombo help!
Yes, I agree your method is the best solution and object orientated - as Java should be.
I however have decided against using this method for a quicker, simpler solution!!! :eek:
I will display only the value in the one column combo box rather than the ID and the value. And I will just retrieve the ID when and as needed - its so much simpler as the functions for this already exist in the database itself...so without any code and without implementing more than a few extra lines of code - I achieved my goal!!!
Thanks v much for your help - very much appreciated!!! :)
evian at 2007-11-11 22:38:27 >

# 5 Re: jcombo help!
Evian,
Did you use JMappedComboBox in order to achieve what you wanted to do?.
If you could post your solution how to do that, it will be much appreciated. TIA. :WAVE:
# 6 Re: jcombo help!
JMappedComboBox (http://www.jamochamud.org/docs/anecho/gui/JMappedComboBox.html) does look interesting but it is isn't available - sadly - in NetBeans. (That's what I'm using for the DonutMonster (http://www.donutmonster.com) project I'm working on.
I guess - seeing as I have thirteen years of VB and less than two of Java experience - I don't understand gravitron's statement.
Okay, so I create a class to set the property for the id and the name of the widget. (In my case I'm using a common name of a news server.)
How to I get the ID to equal the name for each item in the combo list? In HTML, for example, I can set a value for any combo list item:
<option value='1'>The First Item</option>
How does one go about this in Java? I've googled and cannot find an answer.
# 7 Re: jcombo help!
of course, the code i supplied was not complete:
public class Product{
private String name;
private long id;
public Product(long id, String name){
this.id = id;
this.name = name;
}
// to retrieve id of selected item
public int getId(){
return id;
}
// to enshure two equal ids are accepted as to equal objects,
// from java.Object
// this will help to store the objects in a collection and find them
// again using contains() and delte() methods
public boolean equals(Object o){
if (o instanceof Product){
return ((Product)o).getId()==getId();
}
else return false;
}
public String toString(){
return name;
}
}
so you can do the following in your actionlistener:
Product x = (Product) combobox.getSelectedItem();
int productId = x.getId();
// do something with your id
# 8 Re: jcombo help!
The example from html corresponds to the following java attributes of the class persented here:
<option value='1'>The First Item</option>
value=Product.id
"The First Item" aka optiontext=Product.name
# 9 Re: jcombo help!
Nope, did not use the JMappedComboBox.
Used my own method...basically it retrives the unique id -given the value selected by the user from the combo (note:value is also unique in the table) and the function name (the function used to populate the combo).
Soz for the late reply - hardly come on to the forums...but hope that made sense...in a rush to get out of the office now...
evian at 2007-11-11 22:43:28 >
