participate


Swing [Archive] - mouseEvent problem
<<   Back to Forum  |   Give us Feedback Topics: « Previous | Next »
This topic has 10 replies on 1 page.
rajesh_raj
Posts:9
Registered: 9/17/04
mouseEvent problem   
Oct 5, 2004 4:22 AM

 
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
Re: mouseEvent problem   
Oct 5, 2004 5:17 AM (reply 1 of 10)  (In reply to original post )

 
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
 
rajesh_raj
Posts:9
Registered: 9/17/04
Re: mouseEvent problem   
Oct 5, 2004 5:31 AM (reply 2 of 10)  (In reply to #1 )

 

Hi Jay!


Thanks a lot for your help.


I am trying out your suggestion.

Once again thanks!
 
rajesh_raj
Posts:9
Registered: 9/17/04
Re: mouseEvent problem   
Oct 5, 2004 9:54 PM (reply 3 of 10)  (In reply to original post )

 
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.
 
Michael_Dunn
Posts:7,727
Registered: 17/08/03
Re: mouseEvent problem   
Oct 5, 2004 10:20 PM (reply 4 of 10)  (In reply to #3 )

 
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;
}
 
rajesh_raj
Posts:9
Registered: 9/17/04
Re: mouseEvent problem   
Oct 5, 2004 10:41 PM (reply 5 of 10)  (In reply to #4 )

 
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
Re: mouseEvent problem   
Oct 6, 2004 5:02 AM (reply 6 of 10)  (In reply to #5 )

 
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
 
rajesh_raj
Posts:9
Registered: 9/17/04
Re: mouseEvent problem   
Oct 7, 2004 9:38 PM (reply 7 of 10)  (In reply to #6 )

 
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.
 
inteltrust
Posts:87
Registered: 4/22/02
Re: mouseEvent problem   
Oct 7, 2004 9:46 PM (reply 8 of 10)  (In reply to #7 )

 
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"); 
  }
 }
 
rajesh_raj
Posts:9
Registered: 9/17/04
Re: mouseEvent problem   
Oct 7, 2004 9:56 PM (reply 9 of 10)  (In reply to #8 )

 


Thanks for the help.

I am applying your suggestion to my problem.
 
rajesh_raj
Posts:9
Registered: 9/17/04
Re: mouseEvent problem   
Oct 7, 2004 11:11 PM (reply 10 of 10)  (In reply to #9 )

 
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

Click to email this message Email this Topic

Edit this Topic
  
 
 
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.

Powered by Jive Forums