Article View: borland.public.jbuilder.java.swing
Article #10635Re: JComboBox - setSelectedItem doesn't work
From: Timothy Reaves
Date: Mon, 23 Aug 1999 00:00
Date: Mon, 23 Aug 1999 00:00
54 lines
2011 bytes
2011 bytes
Your problem is that jCombo uses equals() to determine if two
objects are the same. Since the default for a String is the one defined
in Object, even if two Strings represent the same text, the are NOT the
same object (which is how equals(0 is defined in Object).
What you would need to do is create a class as follows and it will
work for you.
public class DotQuad{
private String string;
public String string(){
return string;
}
public void string(String newString){
this.string = newString.copy();
}
public DotQuad(String string){
this.string = string.copy();
}
public String toString(){
return string;
}
public boolean equals(Object object){
if (object instanceof DotQuad){
if (this.string.equals(((DotQuad) object).string())){
return true;
}
}
return false;
}
}
Robert Laird wrote:
> I have a simple JComboBox, that I add items to:
>
> jComboBox1.addItem("100.123.456.789");
> jComboBox1.addItem("100.123.456.790");
> jComboBox1.addItem("100.123.456.791");
>
> If, when I present this ComboBox to the user, I
> want to have item # 2 already selected, then I try:
>
> jComboBox1.setSelectedItem("100.123.456.790");
>
> But when it comes up, it always has item #1
> selected.
>
> Any hints, suggestions? Thanks!
>
> -- Robert
Technical Details
Message-ID:
<37C1B817.417580B@silverfields.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01beeb0e$8bc0f220$238eabc6@rtlaird>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,699b2bb1d7db8f99 X-Google-Attributes: gid111d45,public From: Timothy Reaves <treaves@silverfields.com> Subject: Re: JComboBox - setSelectedItem doesn't work Date: 1999/08/23 Message-ID: <37C1B817.417580B@silverfields.com> X-Deja-AN: 516319470 Content-Transfer-Encoding: 7bit References: <01beeb0e$8bc0f220$238eabc6@rtlaird> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Silverfields Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing