I recently upgraded from jre 1.3.x to jre 1.4.2 (prompted by an upgrade
to FC1). Found a problem with my code and the upgraded jre, and was
hoping maybe somebody else could confirm that I'm crazy and totally
screwing up.
According to the docs, setting the selected item in a Choice widget
internally (via calling the select() function) will not generate an
event from that Choice widget. But for some reason, my code is, but
maybe I'm just doing it wrong.
From the api doc:
"Note that this method should be primarily used to initially select an
item in this component. Programmatically calling this method will not
trigger an ItemEvent. The only way to trigger an ItemEvent is by user
interaction."
I change the value internally to respond to a change in the state of my
server application. As far as I know, there's no other way to do this.
But I'm pretty limited.
If somebody could look at this, and maybe even try it on their machine
if it has the 1.4.2 jre installed, I'd apreciate it. Thanks.
Here's the code:
import java.awt.*;
import java.awt.event.*;
class ChoiceTest extends Frame implements ActionListener, ItemListener
{
ChoiceTest()
{
this.setBounds(20, 20, 200, 60);
this.setLayout(new GridLayout(1, 2));
b = new Button("Test");
c = new Choice();
c.add("One");
c.add("Two");
c.add("Three");
this.add(b);
this.add(c);
b.addActionListener(this);
c.addItemListener(this);
addWindowListener(new java.awt.event.WindowAdapter() {
public void
windowClosing(java.awt.event.WindowEvent evt) {
quit();
}
});
}
public static void main(String args[])
{
ChoiceTest ct = new ChoiceTest();
ct.setTitle("Testing for a bug");
ct.show();
}
public void actionPerformed(java.awt.event.ActionEvent e)
{
java.lang.Object object = e.getSource();
if(object == b)
{
int selected = c.getSelectedIndex();
selected++;
if(selected >= c.getItemCount())
selected = 0;
c.select(selected);
}
}
public void itemStateChanged(ItemEvent event)
{
java.lang.Object object = event.getSource();
if(object == c)
{
System.out.println("the Choice widget generated an event,
value changed to " + c.getSelectedItem());
}
}
public void quit()
{
System.exit(0);
}
Button b;
Choice c;
}