Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

events of combobox

Thread view: 
Michael Mueller - 07 Mar 2006 20:18 GMT
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");
    }
}
falcone88@gmail.com - 07 Mar 2006 20:35 GMT
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
      }
}
);
Michael Mueller - 08 Mar 2006 05:46 GMT
falcone88@gmail.com schrieb:
> Maybe try adding a mouseListener
>
[quoted text clipped - 9 lines]
> }
> );

MouseListner is still implemented - but I don't receive MouseEvents :(
Thomas Hawtin - 08 Mar 2006 18:14 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Michael Mueller - 09 Mar 2006 06:43 GMT
Thomas Hawtin schrieb:
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.