This topic has
10
replies
on
1
page.
hi,
i need mouseClicked event on jcomboBox.I try it but not getting answer.Please help me.
my code is as below:::::::::::::::::::::::::::
import java.util.*;
public class mybox extends javax.swing.JFrame {
public mybox() {
initComponents();
jComboBox1.addItem("first");
jComboBox1.addItem("second");
jComboBox1.addItem("third");
jComboBox1.addItem("fourth");
jComboBox1.addItem("fifth");
setSize(500,500);
}
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
getContentPane().setLayout(null);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jComboBox1MouseClicked(evt);
}
});
getContentPane().add(jComboBox1);
jComboBox1.setBounds(170, 80, 140, 21);
pack();
}
private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("my mouse clicked event");
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
new mybox().show();
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
// End of variables declaration
}
JayDS
Posts:1,242
Registered: 10/23/98
Your problem is that JComboBox is one of the trickier components to work with for this sort of thing.
Depending on what the problem is that you're actually trying to solve, it might be easier to simply use
a popup listener on the combo's popup.
That said, the problem you're encountering is that you are not actually clicking on the combo box; you
are clicking on one of its components. Take a look at BasicComboBoxUI#installComponents. To get
what you want, you can do
Component button = jComboBox1.getComponent( 0 );
if ( button != null )
button.addMouseListener( new java.awt.event.MouseAdapter() {
public void mouseClicked( java.awt.event.MouseEvent evt ) {
jComboBox1MouseClicked( evt );
}
} );
: jay
Hi Jay!
Thanks a lot for your help.
I am trying out your suggestion.
Once again thanks!
this is not enough.i need mouseclick on particular item in comboBox.You r putting it on zero index.so it occurs for all.
raj.
Will an actionListener do?
import java.util.*;
class mybox extends javax.swing.JFrame
{
public mybox()
{
initComponents();
jComboBox1.addItem("first" );
jComboBox1.addItem("second" );
jComboBox1.addItem("third" );
jComboBox1.addItem("fourth" );
jComboBox1.addItem("fifth" );
setSize(500,500);
}
private void initComponents()
{
jComboBox1 = new javax.swing.JComboBox();
getContentPane().setLayout(null );
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);} } );
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1MouseClicked(evt);} } );
getContentPane().add(jComboBox1);
jComboBox1.setBounds(170, 80, 140, 21);
pack();
}
private void jComboBox1MouseClicked(java.awt.event.ActionEvent evt)
{
System.out.println("my mouse clicked event" );
}
private void exitForm(java.awt.event.WindowEvent evt)
{
System.exit(0);
}
public static void main(String args[])
{
new mybox().setVisible(true );
}
private javax.swing.JComboBox jComboBox1;
}
hi
i need it on mouseclicked event on a particular item in comboBox. not in actionperformed .so can u do it?
raj
JayDS
Posts:1,242
Registered: 10/23/98
Can you explain why the action listener suggestion doesn't work for you? Is it simply because keyboard
selection can occur as well? Other than that, clicking an item in the list will fire the event just like you want.
: jay
hi
i explain u all.I need a comboBox in which i need a <ADD NEW> option.But when i go up and down in comboBox my actionPerformed occured on <add new> option.so I need it on mouseEvent so when i click the mouse on <add new> option i get my result of action.
if u not understand my problem yet second time i will give u my code.
thanks for help.
raj i this what they suggest is right, why not you do this
private void jComboBox1MouseClicked(java.awt.event.ActionEvent evt) {
if (jComboBox1.getSelectedItem().equals("Add New" )){
// do this...
System.out.println("my mouse clicked event" );
}
}
Thanks for the help.
I am applying your suggestion to my problem.
this u r doing on actionListener, not any mouseEvent .so give me this on only mouse event.not actionListener.
ok
bye
This topic has
10
replies
on
1
page.
Back to Forum
Read the Developer Forums Code of Conduct
Email this Topic
Edit this Topic
Site Upgrade
Forums 7.1.8 was deployed Oct 26th. The release consists of minor fixes & a few enhancements.
Forums Statistics
Users Online : 27 Guests : 128
About Sun forums
Oracle Forums is a large collection of user generated
discussions. It is here to help you ask questions, find answers, and
participate in discussions.
Check out our guide on Getting
started with Oracle Forums for a full walkthrough of how to best
leverage the benefits of this community.