Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Do MouseEvents work on JComboBox  RSS feed

 
Dhanush Dharsh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I designed a gui in which I have two panels. One Panel contains a JTextField and a JComboBox. The other Panel contains a JLabel. Now I added MouseListeners to the JComboBox and JTextField and used Adapters to write code for mouseEntered and mouseExited methods only. The aim is to display a text (some help on the component), when the user moves the mouse over the components(textfield and combo). In case of JTextField it works and is fine, but in the case of JComboBox, it doesnot listen (whereas in Java API collection, its given that JComboBox listens to MouseEvents.)
Can Anyone please help with this??
Thanks and Regards.
Dhanush
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mouse events come from java.awt.Component so any Swing class can have addMouseListener run against it. However, JComboBox doesn't directly support mouseExited and mouseEntered.
JComboBox is actually made up of several components so in order to capture mouse events we need to apply the listener to the individual components and not the JComboBox. How do we do this?
<pre>
Component[] comps = JComboBox1.getComponents();
for(int i = 0; i < comps.length; i++)
{
comps[i].addMouseListener(mouseListener);
}
</pre>
 
Dhanush Dharsh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
Mouse events come from java.awt.Component so any Swing class can have addMouseListener run against it. However, JComboBox doesn't directly support mouseExited and mouseEntered.
JComboBox is actually made up of several components so in order to capture mouse events we need to apply the listener to the individual components and not the JComboBox. How do we do this?
<pre>
Component[] comps = JComboBox1.getComponents();
for(int i = 0; i < comps.length; i++)
{
comps[i].addMouseListener(mouseListener);
}
</pre>

Thank u very much paul. U have cleared my doubt which was clogging me for the past few days. I really feel great now, having become a member of this forum. Thanks once again.
Dhanush
[ December 02, 2003: Message edited by: Dhanush ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well shucks... that's what us bartenders are here for.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!