• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MouseListener dosen't work with JComboBox ???

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i m trying to get if JCombobox has been clicked or not..
my code is..
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Test10 extends JFrame {
public Test10() {
super();
JComboBox comboBox=new JComboBox();
comboBox.addItem("aaa");
comboBox.addItem("bbb");
comboBox.addItem("ccc");

comboBox.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("clicked");
}
});

Container content=getContentPane();
content.setLayout(new FlowLayout());
content.add(comboBox);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(300,300);
setVisible(true);
}

public static void main(String args[]) {
new Test10();
}
}

it's not printing clicked even though i clicked JComboBox..
any answer??
Bhupendra Mahajan
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the ItemEvent. It gets fired when list and combo boxes are selected and deselected.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also try using an ActionListener. This gets triggered whenever a different item is selected. Not sure what the problem with your MouseListener is though...
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
JCombobox is not is a single control.
U can use this line of code to add the mouse listener.
((JTextField)combo.getEditor().getEditorComponent()).addMouseListener(this);
where combo is ur JComboBox instance
Regards
Saj
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi paul,
if i use itemevent it will notifyme only when item state changes
i.e. if item added removed or inserted...as i suppose
but actually i want that if one clicks on JComboBox its contents
are to be changed dynamically according to user....
all users are stored as rows in JTable..
so according to row no i m finding user and contents are changing..
that's why i wanted to find out click on JCombobox..

Originally posted by Paul Stevens:
Try the ItemEvent. It gets fired when list and combo boxes are selected and deselected.


well Sajee i tried your option but unforunately it's
not working...
any solution ???
Bhupendra Mahajan

 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mahajan,
ItemEvent gets fired when the state changes. Selecting something from a combo box changes the state.
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
//do something
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I made the code work...

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Test10 extends JFrame {
public Test10() {
super();
JComboBox comboBox=new JComboBox();
comboBox.addItem("aaa");
comboBox.addItem("bbb");
comboBox.addItem("ccc");
Component[] comps = comboBox.getComponents();
for(int i = 0; i < comps.length; i++)
{
comps[i].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("clicked");
}
});
}
Container content=getContentPane();
content.setLayout(new FlowLayout());
content.add(comboBox);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(300,300);
setVisible(true);
}
public static void main(String args[]) {
new Test10();
}
}
[This message has been edited by Sajee Joseph (edited December 19, 2001).]
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent Sajee,
It's working...
please Explain me exactly what is happining in code..
Bhupendra Mahajan
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
JComboBox is one of the clumsiest java controls. It is made up of sevelal components like a JTextField , JList etc.
So adding the mouselistener to jcombobox alone will not work, since its not an indvidual contol. So we need to add the mouselistener to each of the controls in the JComboBox. That is what im doing by
Component[] comps = comboBox.getComponents();
for(int i = 0; i < comps.length; i++)
{
comps[i].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("clicked");
}
});
}

Hope u got the point.
Regards
Saj
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saj i got it..
thanx ..
what's ur mail address can i contact u directly if i had some problems..???
Bhupendra Mahajan
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Nice to hear from u.
my mail id's are josephsk@slk-soft.com
and sajee_joseph@hotmail.com.
Hey, can i have ur mail id too?? In case u can help me with a couple of java problems
Regards
Saj
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you two are using bogus email accounts, you could always click the little mail icon and find out a users email address.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic