• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to apply ItemListener

 
Saulo Menezes
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!
I am trying to implement an ItemListener on my Applet but I can't manage to discover what I need to add to the command itemStateChanged. All I want is that once the user chooses an item a string will be drawn giving a description. I know that the string has got to be called on paint, but then what would I add to the method itemStateChanged? Would I need to use boolean variables in this case? Following is part of the code I've used. I hope that helps. One more question: can I display the item list anywhere in the Applet by using coordinates?
Saulo
desk = new Choice ();//setting item list and its content.
desk.add("Picture 1");
desk.add("Picture 2");
desk.add("Picture 3");
desk.add("Picture 4");
desk.add("Picture 5");
desk.add("Picture 6");
add(desk);
desk.addItemListener (this);
public void ItemChanged (ItemEvent ie) {}// that's where I am not sure what to do. I would be grateful if somebody could give me a hint on that.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saulo,
Your best bet would be to use a TextArea or at least a Label instead of drawing strings.
That will make is easy for your callback method to just use setText() method.
If you do want to draw the string then you will have to use a class variable and call your paint method after setting up the variable.

Regards,
Manfred.
 
Saulo Menezes
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help, Manfred, but I am still not sure of what I have got to do. Is there a way to set the position on my choice box on the Applet? I want to make it appear only when a certain button is clicked and on a certain location.
Regards,
Saulo
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saulo,
The only way to position your choice element is to make the applet layout null (setLayout method) and specify the choice bounds with the setBounds method.
As far as showing it or not look at setVisible method.
Regards,
Manfred.
 
Saulo Menezes
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
Thanks a lot for all the help. Now it's all working fine but I can't still compile my Applet, as I haven't defined the itemStateChanged. I did try but I ended up mixing everything. I have checked on books and tutorials but nothing helped me finding a solution for this puzzle.
Below is the code I tried to insert on my Applet, which returns lots of errors as the method does not recognise the g variable - part of the paint method. Nevertheless I need those Strings to appear on the coordinates described on this faulty code, based on the choices that are there. I hope someone can help me fixing this, as it's all I need to finish this Applet (I've been working on this for three days, at least).
Regards,
Saulo
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saulo,
I don't see a paint method in your post.
Do something like the following.

Regards,
Manfred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic