Re: Long delay when using JComboBox.setSelectedIndex() ??
kedwa...@gmail.com wrote:
Thank you for your input!
Thank me by not top-posting.
I know doing io on the EDT is not optimal, but I was just trying to provi=
de a short code snippet to illustrate the problem. The issue is happening w=
ell AFTER I load the employee names from disk. The delay is happening right=
after I setSelectedIndex(3) on the jcombobox..
You don't show that code. Why not?
Can you show me how to override equals ??
Really?
Really?
Okay.
Same as any other method.
You should learn the very basic basics of Java before programming with it.
Overriding methods is far more basic than Swing programming.
/**
* Javadoc comments go here!
*/
public class Employee implements Comparable
// Dude! 'Comparable' is a generic type! DO NOT USE RAW TYPES!
<Employee>
{
private int id; // should be final
// Far too much indentation for Usenet. 2 to 4 spaces per level.
private String name; // should be final
/**
* Javadoc comments go here!
*/
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
/**
* Javadoc comments go here!
*/
public String getName() {
return name;
}
/**
* Javadoc comments go here!
*/
public int getId() {
return id;
}
/**
* Javadoc comments go here!
*/
public int compareTo(Employee emp) {
return getName().compareTo(emp.getName());
// this will throw NullPointerException when getName() returns null
// or when emp == null
// Careless.
}
/**
* Value equality - do equality comparison on the field(s) that represe=
nt the
* distinguishing value, usually the same fields used to sort.
* You also need to override {@code hashCode} for consistency.
*
* @param obj Object to compare.
* @return boolean {@code true} iff values are "equal".
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (! (obj instanceof getClass()))
{
return false;
}
Employee other = (Employee) obj;
return name == null? other.name == null : name.equals(other.n=
ame);
}
@Override
public int hashCode()
{
return name == null? 0 : name.hashCode();
}
/**
* Javadoc comments go here!
*/
public String toString() {
return getName()+","+getId();
}
}