Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about JOptionPane

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,
I was trying to explore JOptionPane class and got two questions:-
1. How to set the icon for this?
2. How to trap the action for "YES" "NO" or "Cancel" button for same? as I am not able to find any action listener.
regards,
Arun
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,

1. How to set the icon for this?
2. How to trap the action for "YES" "NO" or "Cancel" button for same? as I am not able to find any action listener.

1. There are various static methods in the JOptionPane class that accept an Icon as a parameter for example:

There are also a constructors which accept an Icon:

2. The static methods generally return int, String or Object. If you don't use the static methods then you'll have to make a call to getValue() which returns an Object.
A good idea would be to look at the javadocs on JOptionPane. It contains a lot of methods and properties, but isn't as complex as it first looks.
Hope this helps
Michael Morris
SCJP2
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael. It works easily.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to ask it again. But another experipent on this JOptionPane. How to set the Top left Icon of the (Message)Box. I tried with following code but in vain. But if i try putting the test part also into the constructorit works very well. But than the problem would be that it pop the message window by default you can't call it as per your requirement.
I know that setIconImage is avaiable for Frames only. But is there any workaround as this is giving trouble to my client?
regards,
Arun

 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,
There are several ways you could go about doing this but the most straight forward would be something like this:

That should set the Icon on your JOptionPane. I didn't test this out so let me know if it works.
Hope this helps
Michael Morris
SCJP2
[ March 25, 2002: Message edited by: Michael Morris ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic