events of combobox

  • Thread starter Michael Mueller
  • Start date
M

Michael Mueller

Greetings!

Please take a look onto following code.
Questions:
Why don't I get any mouse event?
While clicking onto the arrow to open the box, I want to load/change the
contend of the box. Doing this in popupMenuBevomeVisible - one of the
few events I get - is too late. Is there any event to receive to
populate the box after clicking the button?

Thanks

Michael


import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class TestForm extends JFrame
implements PopupMenuListener, ActionListener, MouseListener,
AncestorListener, MouseMotionListener {

public TestForm (){
super("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox combo = new JComboBox();
combo.setEditable(true);
combo.addItem("Test1");
combo.addItem("Test2");
combo.addPopupMenuListener(this);
combo.addMouseListener(this);
combo.addActionListener(this);
combo.addAncestorListener(this);
combo.addMouseMotionListener(this);
this.add(combo);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
TestForm frame = new TestForm();
frame.setLocation(700, 500);
frame.pack();
frame.setVisible(true);
new TestForm();
}
public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
System.out.println("popupMenuWillBecomeVisible");
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
System.out.println("popupMenuWillBecomeInvisible");
}
public void popupMenuCanceled(PopupMenuEvent arg0) {
System.out.println("popupMenuCanceled");
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("actionPerformed");
}
public void mouseClicked(MouseEvent arg0) {
System.out.println("mouseClicked");
}
public void mousePressed(MouseEvent arg0) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent arg0) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent arg0) {
System.out.println("mouseExited");
}
public void ancestorAdded(AncestorEvent arg0) {
System.out.println("popupMenuWillBecomeInvisible");
}
public void ancestorRemoved(AncestorEvent arg0) {
System.out.println("popupMenuWillBecomeInvisible");
}
public void ancestorMoved(AncestorEvent arg0) {
System.out.println("ancestorMoved");
}
public void mouseDragged(MouseEvent arg0) {
System.out.println("mouseDragged");
}
public void mouseMoved(MouseEvent arg0) {
System.out.println("mouseMoved");
}
}
 
F

falcone88

Maybe try adding a mouseListener

combo.addMouseListener(
new MouseAdapter() {

public void mouseReleased(MouseEvent event) {

if (!event.isPopupTrigger())
{
// do whatever it is you're trying to do
}
}
);
 
M

Michael Mueller

Maybe try adding a mouseListener

combo.addMouseListener(
new MouseAdapter() {

public void mouseReleased(MouseEvent event) {

if (!event.isPopupTrigger())
{
// do whatever it is you're trying to do
}
}
);
MouseListner is still implemented - but I don't receive MouseEvents :(
 
T

Thomas Hawtin

Michael said:
Why don't I get any mouse event?

You are adding a MouseListener to a combo box. Typically a JComboBox is
made up of multiple components. The mouse events fire on the
subcomponents, not the JComboBox itself. (Mouse events are a bit odd, in
that they do actually bubble up, but only from components without mouse
listeners attached.)
While clicking onto the arrow to open the box, I want to load/change the
contend of the box. Doing this in popupMenuBevomeVisible - one of the
few events I get - is too late. Is there any event to receive to
populate the box after clicking the button?

What if the combo is opened using F4, Alt-Down or some other way? A
better approach is to use a lazily loading model.

Tom Hawtin
 
M

Michael Mueller

Thomas said:
You are adding a MouseListener to a combo box. Typically a JComboBox is
made up of multiple components. The mouse events fire on the
subcomponents, not the JComboBox itself. (Mouse events are a bit odd, in
that they do actually bubble up, but only from components without mouse
listeners attached.)
I inserted this code.
for (int i=0; i<combo.getComponentCount(); i++) {
combo.getComponent(i).addMouseListener(this);
combo.getComponent(i).addMouseMotionListener(this);
}
Now I get my mouse events - great hint. Thanks
What if the combo is opened using F4, Alt-Down or some other way? A
better approach is to use a lazily loading model.
Of course, a mouse event is not usefull in this case. Even it appears
after the MenuBecomesVisible, which is much too late.
Anybody has some hint to put me on the way with lazily loading model?
BTW: I never used a spezial combo model. Can I use it to create boxes
with multiple columns?

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top