Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2324 > unrolled thread
| Started by | kedward777@gmail.com |
|---|---|
| First post | 2012-12-05 09:27 -0800 |
| Last post | 2012-12-12 18:15 -0800 |
| Articles | 13 — 5 participants |
Back to article view | Back to comp.lang.java.help
Long delay when using JComboBox.setSelectedIndex() ?? kedward777@gmail.com - 2012-12-05 09:27 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? kedward777@gmail.com - 2012-12-05 10:08 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Lew <lewbloch@gmail.com> - 2012-12-05 10:35 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? kedward777@gmail.com - 2012-12-05 10:46 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Lew <lewbloch@gmail.com> - 2012-12-05 11:31 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Roedy Green <see_website@mindprod.com.invalid> - 2012-12-05 16:16 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Lew <lewbloch@gmail.com> - 2012-12-05 16:34 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Knute Johnson <nospam@knutejohnson.com> - 2012-12-05 16:40 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Lew <lewbloch@gmail.com> - 2012-12-05 16:58 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Roedy Green <see_website@mindprod.com.invalid> - 2012-12-06 15:32 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Lew <lewbloch@gmail.com> - 2012-12-06 18:04 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Knute Johnson <nospam@knutejohnson.com> - 2012-12-05 16:15 -0800
Re: Long delay when using JComboBox.setSelectedIndex() ?? Gene Wirchenko <genew@telus.net> - 2012-12-12 18:15 -0800
| From | kedward777@gmail.com |
|---|---|
| Date | 2012-12-05 09:27 -0800 |
| Subject | Long delay when using JComboBox.setSelectedIndex() ?? |
| Message-ID | <b396252c-b370-413b-963b-97313f83a30f@googlegroups.com> |
Hello,
I am attempting to load 5000 employee items into a jcombobox, and then setSelectedIndex, but I am getting a odd delay in the gui after setting the selectedIndex
1) If I load the 5000 employee names using a simple vector, it works fine. When I call setSelectedIndex(5), it sets the combobox to the 5th element, very quickly. This works fine:
while ((line = input.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, "|");
lastName = st.nextToken().trim();
firstName = st.nextToken().trim();
id = Integer.parseInt(st.nextToken().trim());
comboBoxItems.add(lastName);}
mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
mybox1.setSelectedIndex(5);
2)HOWEVER, if I load an employee object into a mapped vector, and then setSelectedIndex(5), I DO see it instantly set the GUI combobox to the 5th item, BUT then there is a LOOOONG delay in the gui (1 minute or more) before it allows me to do anything further in the gui
while ((line = input.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, "|");
lastName = st.nextToken().trim();
firstName = st.nextToken().trim();
id = Integer.parseInt(st.nextToken().trim());
empBuffer= new Employee(id, lastName );
map.put(empBuffer.getId()+"",empBuffer );
count++; }
model.addAll(map.values());
Collections.sort(model);
mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
mybox1.setSelectedIndex(5);
public class Employee implements Comparable {
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int compareTo(Object emp) {
return getName().compareTo(((Employee)emp).getName());
}
public String toString() {
return getName()+","+getId();
}
}
[toc] | [next] | [standalone]
| From | kedward777@gmail.com |
|---|---|
| Date | 2012-12-05 10:08 -0800 |
| Message-ID | <e11b4b5d-76ae-4eb0-b29a-b5c40ad59937@googlegroups.com> |
| In reply to | #2324 |
CORRECTION, both examples have the same problem. I thought the first example was working, but in each case AFTER I call setSelectedIndex, the GUI DOES quickly go to the item in the combobox , but then the whole gui hangs for about a minute ???? Why? If I replace the combobox with a jtextfield, it works without delay...
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-12-05 10:35 -0800 |
| Message-ID | <1f53fcab-243d-47aa-9b17-a1216b1e8138@googlegroups.com> |
| In reply to | #2325 |
kedwa...@gmail.com wrote:
> I am attempting to load 5000 employee items into a jcombobox, and then setSelectedIndex, but I am getting a odd delay in the gui after setting the selectedIndex
>
> 1) If I load the 5000 employee names using a simple vector, it works fine. When I call
'vector' or "vector" or 'Vector'?
> setSelectedIndex(5), it sets the combobox to the 5th element, very quickly. This works fine:
>
> while ((line = input.readLine()) != null) {
This is a slow action on the EDT. Bad.
> StringTokenizer st = new StringTokenizer(line, "|");
> lastName = st.nextToken().trim();
> firstName = st.nextToken().trim();
> id = Integer.parseInt(st.nextToken().trim());
'lastName, 'firstName', and 'id' should be declared locally to the loop.
> comboBoxItems.add(lastName);}
> mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
> mybox1.setSelectedIndex(5);
>
> 2)HOWEVER, if I load an employee object into a mapped vector, and then setSelectedIndex(5), I DO
What is a "mapped vector"?
> see it instantly set the GUI combobox to the 5th item, BUT then there is a LOOOONG delay in the gui (1 minute or more) before it allows me to do anything further in the gui
>
> while ((line = input.readLine()) != null) {
This is a slow action on the EDT. Bad.
> StringTokenizer st = new StringTokenizer(line, "|");
> lastName = st.nextToken().trim();
> firstName = st.nextToken().trim();
> id = Integer.parseInt(st.nextToken().trim());
> empBuffer= new Employee(id, lastName );
'lastName, 'firstName', 'id' and 'empBuffer' should be declared locally to the loop.
> map.put(empBuffer.getId()+"",empBuffer );
> count++; }
>
> model.addAll(map.values());
> Collections.sort(model);
> mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
> mybox1.setSelectedIndex(5);
>
> public class Employee implements Comparable {
> private int id;
> private String name;
>
> public Employee(int id, String name) {
> this.id = id;
> this.name = name;
> }
>
> public String getName() {
> return name;
> }
>
> public int getId() {
> return id;
> }
>
> public int compareTo(Object emp) {
> return getName().compareTo(((Employee)emp).getName());
> }
>
> public String toString() {
> return getName()+","+getId();
> }
> }
You should override 'equals()' to match 'compareTo()' so the model will work right.
kedwa...@gmail.com wrote:
> CORRECTION, both examples have the same problem. I thought the first example was working, but in each case AFTER I call setSelectedIndex, the GUI DOES quickly go to the item in the combobox , but then the whole gui hangs for about a minute ???? Why? If I replace the combobox with a jtextfield, it works without delay...
How long does 'input.readLine()' take, and why are you doing it on the EDT?
I/O is much slower than CPU activity. You are causing the GUI to wait for 5000 lines of read. Bad.
Don't do it on the EDT.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | kedward777@gmail.com |
|---|---|
| Date | 2012-12-05 10:46 -0800 |
| Message-ID | <bc9f2f27-8ee5-41ef-b03c-575939131caa@googlegroups.com> |
| In reply to | #2326 |
Thank you for your input!
I know doing io on the EDT is not optimal, but I was just trying to provide a short code snippet to illustrate the problem. The issue is happening well AFTER I load the employee names from disk. The delay is happening right after I setSelectedIndex(3) on the jcombobox..
Can you show me how to override equals ??
Thank you again!
Ken
On Wednesday, December 5, 2012 1:35:50 PM UTC-5, Lew wrote:
> kedwa...@gmail.com wrote:
>
> > I am attempting to load 5000 employee items into a jcombobox, and then setSelectedIndex, but I am getting a odd delay in the gui after setting the selectedIndex
>
> >
>
> > 1) If I load the 5000 employee names using a simple vector, it works fine. When I call
>
>
>
> 'vector' or "vector" or 'Vector'?
>
>
>
> > setSelectedIndex(5), it sets the combobox to the 5th element, very quickly. This works fine:
>
> >
>
> > while ((line = input.readLine()) != null) {
>
>
>
> This is a slow action on the EDT. Bad.
>
>
>
> > StringTokenizer st = new StringTokenizer(line, "|");
>
> > lastName = st.nextToken().trim();
>
> > firstName = st.nextToken().trim();
>
> > id = Integer.parseInt(st.nextToken().trim());
>
>
>
> 'lastName, 'firstName', and 'id' should be declared locally to the loop.
>
>
>
> > comboBoxItems.add(lastName);}
>
> > mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
>
> > mybox1.setSelectedIndex(5);
>
> >
>
> > 2)HOWEVER, if I load an employee object into a mapped vector, and then setSelectedIndex(5), I DO
>
>
>
> What is a "mapped vector"?
>
>
>
> > see it instantly set the GUI combobox to the 5th item, BUT then there is a LOOOONG delay in the gui (1 minute or more) before it allows me to do anything further in the gui
>
> >
>
> > while ((line = input.readLine()) != null) {
>
>
>
> This is a slow action on the EDT. Bad.
>
>
>
> > StringTokenizer st = new StringTokenizer(line, "|");
>
> > lastName = st.nextToken().trim();
>
> > firstName = st.nextToken().trim();
>
> > id = Integer.parseInt(st.nextToken().trim());
>
> > empBuffer= new Employee(id, lastName );
>
>
>
> 'lastName, 'firstName', 'id' and 'empBuffer' should be declared locally to the loop.
>
>
>
> > map.put(empBuffer.getId()+"",empBuffer );
>
> > count++; }
>
> >
>
> > model.addAll(map.values());
>
> > Collections.sort(model);
>
> > mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
>
> > mybox1.setSelectedIndex(5);
>
> >
>
> > public class Employee implements Comparable {
>
> > private int id;
>
> > private String name;
>
> >
>
> > public Employee(int id, String name) {
>
> > this.id = id;
>
> > this.name = name;
>
> > }
>
> >
>
> > public String getName() {
>
> > return name;
>
> > }
>
> >
>
> > public int getId() {
>
> > return id;
>
> > }
>
> >
>
> > public int compareTo(Object emp) {
>
> > return getName().compareTo(((Employee)emp).getName());
>
> > }
>
> >
>
> > public String toString() {
>
> > return getName()+","+getId();
>
> > }
>
> > }
>
>
>
> You should override 'equals()' to match 'compareTo()' so the model will work right.
>
>
>
> kedwa...@gmail.com wrote:
>
> > CORRECTION, both examples have the same problem. I thought the first example was working, but in each case AFTER I call setSelectedIndex, the GUI DOES quickly go to the item in the combobox , but then the whole gui hangs for about a minute ???? Why? If I replace the combobox with a jtextfield, it works without delay...
>
>
>
> How long does 'input.readLine()' take, and why are you doing it on the EDT?
>
>
>
> I/O is much slower than CPU activity. You are causing the GUI to wait for 5000 lines of read. Bad.
>
>
>
> Don't do it on the EDT.
>
>
>
> --
>
> Lew
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-12-05 11:31 -0800 |
| Message-ID | <c6527b13-fc31-4f02-a378-e7995ef895fe@googlegroups.com> |
| In reply to | #2327 |
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 provide a short code snippet to illustrate the problem. The issue is happening well 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 represent 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.name);
}
@Override
public int hashCode()
{
return name == null? 0 : name.hashCode();
}
/**
* Javadoc comments go here!
*/
public String toString() {
return getName()+","+getId();
}
}
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-12-05 16:16 -0800 |
| Message-ID | <7uovb8t3p068l0fc39qj7n6ttr237g2k9o@4ax.com> |
| In reply to | #2327 |
On Wed, 5 Dec 2012 10:46:14 -0800 (PST), kedward777@gmail.com wrote, quoted or indirectly quoted someone who said : >Can you show me how to override equals ?? > >Thank you again! just write a method that has the same parms as Object.equals -- Roedy Green Canadian Mind Products http://mindprod.com Students who hire or con others to do their homework are as foolish as couch potatoes who hire others to go to the gym for them.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-12-05 16:34 -0800 |
| Message-ID | <c3fc1e50-54d6-4c45-b5d4-174aa809f1cb@googlegroups.com> |
| In reply to | #2330 |
Roedy Green wrote: > kedward777... wrote, quoted or indirectly quoted someone who said : >>Can you show me how to override equals ?? > > just write a method that has the same parms [sic] as Object.equals That is necessary but very far from sufficient. It has to have the same or compatible signature, not just the same parms. It should be annotated with '@Override'. It has to have logic appropriate to the type, so that the semantics of 'equals()' makes sense. You also left out the part about having 'hashCode()' match. Your answer is like telling a newbie, "To write a Java program to do what you're asking, just write a class that has the same form as 'Hello, World'." Except for being more incorrect. It also came four hours after an extremely detailed answer with code specific to the OP's situation that didn't have these lacunae. But thank you for participating anyway. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2012-12-05 16:40 -0800 |
| Message-ID | <k9opia$aat$1@dont-email.me> |
| In reply to | #2331 |
On 12/5/2012 4:34 PM, Lew wrote: > It also came four hours after an extremely detailed answer with code specific to > the OP's situation that didn't have these lacunae. Lacunae, you must have gone to a good college :-). -- Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-12-05 16:58 -0800 |
| Message-ID | <494effc5-f9bc-485f-a818-eb09c0fbb120@googlegroups.com> |
| In reply to | #2332 |
Knute Johnson wrote: > Lew wrote: >> It also came four hours after an extremely detailed answer with code specific to > > the OP's situation that didn't have these lacunae. > > Lacunae, you must have gone to a good college :-). SUNY at Stony Brook, an excellent institution of higher learning. But the real culprit is my penchant for sesquipedalianism. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-12-06 15:32 -0800 |
| Message-ID | <5na2c8lf28to6nokib9csutb1vn1vflu10@4ax.com> |
| In reply to | #2331 |
On Wed, 5 Dec 2012 16:34:28 -0800 (PST), Lew <lewbloch@gmail.com> wrote, quoted or indirectly quoted someone who said : > >> just write a method that has the same parms [sic] as Object.equals > >That is necessary but very far from sufficient. That would take a perverse nature to change the types and still expect it to work. -- Roedy Green Canadian Mind Products http://mindprod.com Students who hire or con others to do their homework are as foolish as couch potatoes who hire others to go to the gym for them.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-12-06 18:04 -0800 |
| Message-ID | <30472054-57a1-4025-a357-c7f7417d33fc@googlegroups.com> |
| In reply to | #2345 |
Roedy Green wrote:
> Lew wrote, quoted or indirectly quoted someone who said :
>> Roedy Green wrote:
>>> just write a method that has the same parms [sic] as Object.equals
>>That is necessary but very far from sufficient.
>
> That would take a perverse nature to change the types and still expect
> it to work.
If you mean the types of the method parameters, you're right but irrelevant.
No one disputed that the parameter types had to match - hence my comment
that it is necessary to do that. The problem with your advice is that it left out
every other significant factor that makes a method an override of 'equals()'.
Case in point - 'Object#equals()' requires one parameter of type 'Object'. A common
tyro mistake is to use the type of the defining child class, much as one is supposed
to do with 'compareTo()'. Thus:
public class Foo
{
...
public boolean equals(Foo other) { ... }
}
If you remember to use the '@Override' annotation this mistake will be caught at
compile time, rather than at run time as happens when you don't use that annotation.
That's one of the items you left out, and ignored from my post.
It has to have a compatible return type.
public void equals(Object other) { ... }
fits your advice but is wrong.
It has to make sense.
@Override public boolean equals(Object other) { return true; }
is a valid override but almost always very stupid.
Your advice didn't cover that, and you ignored it from my post.
'hashCode()' has to match. It is possible to have code work correctly without
following this advice, but it's not smart. It'll break with one unanticipated
refactor or extension. It won't work right in hash structures.
'compareTo()' has to be consistent nearly always. Again, you can violate this
in particular circumstances, but why violate the rule? Just always keep the four
identifying methods in synch with each other (three if you don't implement 'Comparable<T>').
equals()
hashCode()
toString()
compareTo()
They should all agree with respect to equality.
That way you don't have to puzzle out every time if your code will always be safe
when you violate the rule of thumb.
Your comment about "changing the types" makes no sense to me, actually. I don't
know what triggered it or how it furthers the pedagogy.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2012-12-05 16:15 -0800 |
| Message-ID | <k9oo3i$4gf$1@dont-email.me> |
| In reply to | #2324 |
On 12/5/2012 9:27 AM, kedward777@gmail.com wrote:
> Hello,
>
> I am attempting to load 5000 employee items into a jcombobox, and then setSelectedIndex, but I am getting a odd delay in the gui after setting the selectedIndex
>
> 1) If I load the 5000 employee names using a simple vector, it works fine. When I call setSelectedIndex(5), it sets the combobox to the 5th element, very quickly. This works fine:
>
> while ((line = input.readLine()) != null) {
> StringTokenizer st = new StringTokenizer(line, "|");
> lastName = st.nextToken().trim();
> firstName = st.nextToken().trim();
> id = Integer.parseInt(st.nextToken().trim());
> comboBoxItems.add(lastName);}
>
> mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
> mybox1.setSelectedIndex(5);
>
>
> 2)HOWEVER, if I load an employee object into a mapped vector, and then setSelectedIndex(5), I DO see it instantly set the GUI combobox to the 5th item, BUT then there is a LOOOONG delay in the gui (1 minute or more) before it allows me to do anything further in the gui
>
> while ((line = input.readLine()) != null) {
> StringTokenizer st = new StringTokenizer(line, "|");
> lastName = st.nextToken().trim();
> firstName = st.nextToken().trim();
> id = Integer.parseInt(st.nextToken().trim());
> empBuffer= new Employee(id, lastName );
> map.put(empBuffer.getId()+"",empBuffer );
> count++; }
>
> model.addAll(map.values());
> Collections.sort(model);
> mybox1.setModel(new javax.swing.DefaultComboBoxModel(model));
> mybox1.setSelectedIndex(5);
>
>
> public class Employee implements Comparable {
> private int id;
> private String name;
>
> public Employee(int id, String name) {
> this.id = id;
> this.name = name;
> }
>
> public String getName() {
> return name;
> }
>
> public int getId() {
> return id;
> }
>
> public int compareTo(Object emp) {
> return getName().compareTo(((Employee)emp).getName());
> }
>
> public String toString() {
> return getName()+","+getId();
> }
> }
>
Having created some rather large GUIs that perform poorly, I wouldn't
create a JComboBox with 5000 elements. It is just too many especially
if your computer is not really fast.
Suggestion: Keep your Employee objects in a List of some sort and use
an editable JComboBox that will load names or IDs that match what you
enter. I might wait for two or three letters before stuffing the
JComboBox to keep the list a more reasonable size. If a person entered
a number you could just bring up that Employee.
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@telus.net> |
|---|---|
| Date | 2012-12-12 18:15 -0800 |
| Message-ID | <0geic85njnr5s5drn8e751jifa8pkaamqr@4ax.com> |
| In reply to | #2324 |
On Wed, 5 Dec 2012 09:27:59 -0800 (PST), kedward777@gmail.com wrote:
>I am attempting to load 5000 employee items into a jcombobox, and
then setSelectedIndex, but I am getting a odd delay in the gui after
setting the selectedIndex
5000? Are you really expecting someone to scroll through
something that big?
[snip]
Sincerely,
Gene Wirchenko
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.help
csiph-web