DISABLE MOUSE EVENT on jlabel

Dear all,
I have a jlabel on which i perform a click and the embbeded image switch between start and stop image.
My problem consists in preventing from performing multiples click/or disabling mouse event while the application starts loading some parameters.
switchViewLabel.addMouseListener(new MouseAdapter() {
               public void mouseClicked(MouseEvent e) {
                    //it takes a long time
                               onSwitchViewButtonClick();
protected void onSwitchViewButtonClick() {
          System.out.println("onSwitchViewButtonClick");
          if (canGo) {
               System.out.println("canGo");
               canGo = false;
               MainWindowPeer.getInstance().setCursorToWait();
                     //this action load parameter
                        CommandWindow.getInstance().switchView();
                     refreshSwitchViewButton();
               MainWindowPeer.getInstance().setCursorToDefault();
               canGo = true;
     } But on my console i have noticed :
canGo
onSwitchViewButtonClick
canGo
onSwitchViewButtonClick
canGo.....
It looks like we store mouse event anywhere and for each event we execute onSwitchViewButtonClick code and consume event.(it seems to not be asynchronous)....
How to do ???
Thanks for any helps..

Dear all,
yes i want to ignore mouse events until the processing is done.But how to perform process in separate thread and what is a semaphore.Is it possible to have much more explanation...
Shall i do following code..
mouseAdapter = new MouseAdapter() {
               public void mouseClicked(MouseEvent e) {
                    SwingUtilities.invokeLater(new Runnable(){
                         public void run() {
                              //switchViewLabel.removeMouseListener(mouseAdapter);
                              onSwitchViewButtonClick();
                              //switchViewLabel.addMouseListener(mouseAdapter);
          };Thanks you in advance...

Similar Messages

  • Add mouse event on JLabel?

    Hi there!
    Can i add a mouse event on JLabel? 'Coz i thought of when I'm double-clicking the label, i could capture the text on the label and open a new sub-frame that displays information of the captured text. Your replies would be appreciated! Thanks ;)

    Why don't you just add a MouseListener to you label and wait for the user to click it. Shouldn't be that hard to accomplish.
    JLabel myLabel = new JLabel("some text");
    myLabel.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() > 1) {
          // user double-clicked the label
        } else {
          // user single-clicked it
    });cheers,
    kelysar

  • Disabling mouse events

    hi,
    I am having objects on a JFrame that respond to mouse events. I would like to know how I can disable these events. I use the addMouseListener to register a mouse event on something. Is there a way to remove or to deregister mouse events.
    I basically have a game of tiles on a JFrame and I am going to have the human player play against the computer. I want to be able to have turns; computer and human player turn.

    removeMouseListener()

  • Add mouse event in JLabel

    Hello!
    I have a Jtable in wich I inserted some JLabels. This labels are inside a Vector, and I use this TableCellRender to show them.
    Here is the code of my TableCellRenderer.
    public class JLabelCellRender extends JLabelsPanel implements  TableCellRenderer {
        Border b;
        /** Creates a new instance of PLIconLabel */
        public JLabelCellRender() {
            super(); /* null indicates no layour manager */
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            b=BorderFactory.createLineBorder(Color.black);
        public java.awt.Component getTableCellRendererComponent(JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row, int column) {
            removeAll();
            setBackground(getCor());
            Vector modulos=(Vector)value;
            JLabelModuloTurmaMouseEvents me=new JLabelModuloTurmaMouseEvents();
            if(modulos.size()==0)
                return this;
            for(int i=0;i<modulos.size();i++){
                JLabelModuloTurma x=(JLabelModuloTurma)modulos.elementAt(i);
    //add the event to label       
        x.addMouseListener(me);
                x.setAlignmentX(CENTER_ALIGNMENT);
                x.setAlignmentY(BOTTOM_ALIGNMENT);
                if(!x.getText().equals(""))
                    x.setBorder(b);
                add(x);
            return this;
    }Now I want to add a mouse event to labels but it doesn't work.
    Is possible to do that? Please help me.
    Thanks in advance.

    Well thanks for your suggestions. By now I'm trying to write a TableCellEditor. I think if I can return a component like the rendered compononent of each cell with the method getTableCellEditorComponent I have access to do whatever I want. Here is my TableCellEditor class:
    import java.awt.Color;
    import java.awt.Component;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.table.TableCellEditor;
    import javax.swing.BoxLayout;
    public class GrelhaHorariosCellEditor extends AbstractCellEditor implements TableCellEditor {
        Vector labels;
        JPanel painel;
        Border b;
        public GrelhaHorariosCellEditor(){
            painel=new JPanel();
            painel.setLayout(new BoxLayout(painel, BoxLayout.Y_AXIS));
            b=BorderFactory.createLineBorder(Color.black);
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int r, int c) {
            labels=(Vector)value;
            if(labels.size()==0){
                stopCellEditing();
                return null;
                else{
            for(int i=0;i<labels.size();i++){
            JLabelModuloTurma li=(JLabelModuloTurma)labels.elementAt(i);
            painel.add(li);
            return painel;
        public Object getCellEditorValue() {
            return labels;
    }What you think of my ideia? Thanks for reply.

  • Disable mouse events in ActiveX control

    Hi,
    I am using the Quicktime COM/ActiveX control for Windows from C# but want to change its default behaviour slightly.
    Currently when the mouse is pressed on the player control it pauses, resumes when double clicked or if already playing reverts to fullscreen mode.
    How can I disable this behaviour so that mouse clicks on the control do nothing?
    I can respond to mouse down events and intercept the message in WndProc but the control still seems to recieve the message before my application code.
    I get the feeling that I just need to change a property of the player, but which one?
    Thanks in advance for your help.
    James

    Mate, you are a total life saver!!!!!!!!!
    Thanks so much again.

  • Disable Mouse Events

    There is an area of the application I am working on in which one frame is calling another frame to open. The frame that is being called takes about 10 seconds to load. My problem is that while the frame is loading, I want to disable the user from clicking their mouse while this frame loads. I have read about glass panes, but was wondering if there was a way to just disable the mouse click.

    you could unistall the mouse handler for the object, or use a flag in the mouse handler that would be set for a wait condition, and thus, instructing the handler to process or pass through.

  • Disable JTree and stop it from receiving mouse events

    Hi,
    I have a JTree which has a mouse listener associated with (which is used for right-click operations)..,..anyway I am trying to disable the tree, so I call setEnabled(false)...which, as documented, disables the tree but it will still receive mouse events. I also tried, setEditable(false)..... the only way I can think of is to remove the mouse listener, and then when I want to re-enable the tree, I have to add it back.
    is there some other way?
    thanks

    Hi,
    I have a JTree which has a mouse listener
    ner associated with (which is used for right-click
    operations)..,..anyway I am trying to disable the
    tree, so I call setEnabled(false)...which, as
    documented, disables the tree but it will still
    receive mouse events. I also tried,
    setEditable(false)..... the only way I can think of
    is to remove the mouse listener, and then when I want
    to re-enable the tree, I have to add it back.
    is there some other way?Why do you want another way? This seems like a perfectly viable way to accomplish what you want.

  • Mouse Events on Disabled Buttons

    Hi,
    In my application I should make a disabled button to show a tool tip when mouse is entered onto it.
    I'm using java.awt.container not Jcontainer.
    I have searched in SDN forums and after reading some of the comments what I understood is �disabled Swing button can react to Mouse events but a disabled awt button can not react to mouse events�.
    Is that true or did I not understand correctly?
    And how would I be able to implement the required functionality in my
    application?
    Thanks.

    import java.awt.*;
    import java.awt.event.*;
    public class AwtTooltip {
        private Panel getContent(Frame f) {
            Button left = new Button("left");
            left.setEnabled(false);
            Button right = new Button("right");
            Panel panel = new Panel(new GridBagLayout());
            new TipManager(panel, f);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            panel.add(left, gbc);
            panel.add(right, gbc);
            return panel;
        public static void main(String[] args) {
            AwtTooltip test = new AwtTooltip();
            Frame f = new Frame();
            f.addWindowListener(closer);
            f.add(test.getContent(f));
            f.setSize(300,100);
            f.setLocation(200,200);
            f.setVisible(true);
        private static WindowListener closer = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
    class TipManager extends MouseMotionAdapter {
        Panel component;
        Window tooltip;
        Label label;
        public TipManager(Panel panel, Frame frame) {
            component = panel;
            panel.addMouseMotionListener(this);
            initTooltip(frame);
         * Since enabled Buttons consume MouseEvents we will
         * receive events sent only from disabled Buttons.
        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();
            boolean hovering = false;
            Component[] c = component.getComponents();
            for(int j = 0; j < c.length; j++) {
                Rectangle r = c[j].getBounds();
                if(r.contains(p)) {
                    hovering = true;
                    if(!tooltip.isShowing())
                        showTooltip(c[j], p);
                    break;
            if(!hovering && tooltip.isShowing()) {
                tooltip.setVisible(false);
        private void showTooltip(Component c, Point p) {
            String text = ((Button)c).getLabel();
            label.setText(text);
            tooltip.pack();
            convertPointToScreen(p, component);
            tooltip.setLocation(p.x+10, p.y-15);
            tooltip.setVisible(true);
        /** Copied from SwingUtilities source code. */
        public void convertPointToScreen(Point p, Component c) {
            Rectangle b;
            int x,y;
            do {
                if(c instanceof Window) {
                    try {
                        Point pp = c.getLocationOnScreen();
                        x = pp.x;
                        y = pp.y;
                    } catch (IllegalComponentStateException icse) {
                        x = c.getX();
                        y = c.getY();
                } else {
                    x = c.getX();
                    y = c.getY();
                p.x += x;
                p.y += y;
                if(c instanceof Window)
                    break;
                c = c.getParent();
            } while(c != null);
        private void initTooltip(Frame owner) {
            label = new Label();
            label.setBackground(new Color(184,207,229));
            tooltip = new Window(owner);
            tooltip.add(label);
    }

  • How can i disable mouse right clicking event

    I need in my project to disallow any body to right click the mouse. Can i disable this event to prevent user of the application from right clicking if yes how can i disable it.

    I have code written in Visual Basic doing the same task but i don't know how to transfer those code in java
    The following is a code for that:
    Option Explicit
    'declares
    Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    'constant
    Private Const WH_MOUSE_LL = 14&
    Public Const HC_ACTION = 0&
    Public Const WM_RBUTTONDOWN = &H204
    Public Const WM_RBUTTONUP = &H205
    Public Const VK_RBUTTON = &H2
    Private lMShook As Long
    Private bHookEnabled As Boolean
    'functions which process mouse events
    Public Function MouseProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If nCode = HC_ACTION Then
    If (wParam = WM_RBUTTONUP Or wParam = WM_RBUTTONDOWN) Then
    MouseProc = 1
    Exit Function
    End If
    End If
    MouseProc = CallNextHookEx(lMShook, nCode, wParam, lParam)
    End Function
    Public Function SetHook()
    If lMShook = 0 Then
    lMShook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf MouseProc, App.hInstance, 0&)
    End If
    If lMShook = 0 Then
    MsgBox "failed to install hook :" & lMShook & " : " & WH_MOUSE_LL
    bHookEnabled = False
    Unload Form1
    Exit Function
    Else
    bHookEnabled = True
    End If
    End Function
    Public Function UnSetHook()
    If bHookEnabled Then
    Call UnhookWindowsHookEx(lMShook)
    lMShook = 0
    End If
    bHookEnabled = False
    End Function
    Public Function InstalledHook()
    MsgBox " installed hook is :" & lMShook
    End Function
    code for form is below:
    Option Explicit
    Private Sub Command1_Click()
    InstalledHook
    End Sub
    Private Sub Command2_Click()
    Call SetHook
    End Sub
    Private Sub Command3_Click()
    Call UnSetHook
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    Call UnSetHook
    End Sub

  • How can I disable any mouse event while is another is processing?

    Hi!
    I've a problem and I need some help.
    I'm downloading an image in the MouseAdapter's mouseClicked method. So I need to disable any other mouse events produced durring this download. These events can be removed from the queue or is there is any way to disable entering mouse events to the queue while another one is processing?
    If anyone can halp me, thanks a lot.

    Interesting situation. I would suggest that the downloading be done on another thread; long processes shouldn't be handled on the swing thread in most situations. Then to stop the mouse events you'd simply disable the component that has the mouse listener until the image has finished loading. That way these events will be processed and discarded while the image is loading.
    ie,
    public void mouseClicked(MouseEvent e)
      final Component source =  (Component)e.getSource;
      source.setEnabled(false); //stop further mouse events.
      new Thread()
          public void run()
             loadImage(); //should not return until image is loaded
             source.setEnabled(true);
    }Hope that helps. :)

  • Firefox mouse event not behaving the same on PC and Mac computers

    I use the FCKEditor and having a very strange problem. When I click on the FCKEditor's editing area, the keyboard gets disabled untill I click the mouse somewhere outside of the editing window. The problem only happen on Firefox (3.6.10) on Mac computer (Snowleopard 10.6.4). Doing exactly the same steps on PC works without any problem.
    What I want to know:
    - Is it possible that the Firefox on Mac is different than the PC in handling mouse events? Or some other areas?

    Sounds more that Firefox is treating that editor area as read-only.
    Start Firefox in [[Safe Mode]] to check if one of your add-ons is causing your problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).
    See [[Troubleshooting extensions and themes]] and [[Troubleshooting plugins]]

  • Touch to Mouse events conversion

    Hi Everybody!
    Does anybody know how correctly fire mouse events to Oracle Maps in javascript?
    Default translation touch events in iPad Safari does not quite fit. It try zoom or pan browser window, not map. :(
    If catch touch events and disable default translation I can zoom and pan MVMapView object.
    But how fire "click","mousemove","mouseover" events on Map? I need dispatch event to ThemeBasedFOIs (InfoWindow Popup and other listeners)?

    Yes. The Oracle Maps tutorial contains examples (the tutorial can be found in your mapviewer deployment
    e.g. http://localhost:7001/mapviewer/fsmc/tutorial/index.html) and documentation in the API reference
    (e.g. http://localhost:7001/mapviewer/fsmc/apidoc/index.html). Look at MVEvent and the attachEventListener function of MVMapView, MVThemeBasedFOI, and MVFOI classes for example.

  • Inappropriate mouse event coordinates in the WebKit of HTMLLoader/StageWebView

    Hi,
    We have a desktp application which incorporates an HTMLRichTextEditor (implemented in HTML/JS and loaded into AIR).
    We have run into a big problem with text selection though. It seems that the both the HTMLLoader and the StageWebView are passing wrong mouse coordinates to the WebKit when the cursor is outside of the component.
    I've created a sample application which uses mx:HTML (which is a wrapper of the HTMLLoader) and a WebView class (a wrapper aroun StageWebView reference to which is given here).
    The application is located here in Dropbox. The source code of the app is here.
    The app was built with Flex 4.6 and ran in AIR 3.7
    The sample app has a WebView which loads loremipsum and HTML loading some dummy local HTML page. The HTML page uses the JS->AIR bridge to report the mouseevents to a function in AIR. In the snapshot below the selection was started from the first row and then slowly continued to the left. As soon as it reaches the green area the selection changes unexpectably. As it can be seen in the bottom right -> the WebKit has received a mouse event with incorrect coordinates. The reported mouse coordinates seem to be the mouse coordinates within the green component and the selection behaves like that. If the mouse is moved to the blue - the selection changes again (according to the coordinates in the blue area).
    The component which uses the StageWebView behaves in absolutely the same way.
    I'd assume that I have to post a bug but I decided to post here at first.
    I've tried a few workarounds in order to stop and ongoing selection or to change it's behaviour but none of them with any relevant success. Even disabling mouse children for the entire application has no effect on the selection once it's started.
    Any help and/or comments would be much appreciated.
    Thanks !

    Thank you for the heads up.  Do you know if this is a recent change in behavior with AIR (ie. does this occur in previous versions)?  Regardless, could you please open a new bug report on this over at bugbase.adobe.com? 
    Once added, please post back with the URL so that others affected can add their comments and votes and I can follow up internally.
     

  • Swing Mouse Event Propagation

    Hello,
    I'm developing my first Swing application.
    One of the components is a thumbnail image panel (ext. JPanel) containing several other components (mostly ext. JLabel). I have the image panel receiving mouse events so as to allow the user to see when the image panel is 'in focus' (rollover effect basically) and to allow the image panel to be selected by clicking.
    I added a tooltip to one of the contained labels, and it now 'consumes' my mouse enter/exit event so that now it is as if the label is not part of it's container. This is not what I want.
    Is there not some simple way to allow mouse events to be listened for by a child component but yet to 'fall through' to a listener in the parent component?
    Any insight into this little problem would be most appreciated.
    Thanks.

    You can listen to all MouseEvents generated:
    Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
         public void eventDispatched(AWTEvent e)
              System.out.println(e);
    }, AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK);You would then need to determine if the source component is a descendent of your main panel.

  • Having picked up a mouse event, pass it to parent component.

    This is further to my table cell hover, which is otherwise working.
    I track the mouse accross the table using an invisible child component, and when the mouse leaves it, move the child onto the new cell position.
    The problem is that, while the mouse is over the invisible component, that component is grabbing all the mouse events. My table also wants to detect mouse events (though it's interested in clicks).
    The obvious thing is, after they've been dealt with and the child component level, to transfer the events to the table (having mapped the mouse coordinates). However all the processEvents methods in components like JTable are protected.
    I suppose I could extend JTable and add a method to get arround protected, but that's nasty, especially since a lot of my JTables are already subclassed.

    I had to do just this. I have JLabel's in a JPanel. The JPanel is in a LayeredPane. When I mouse over the JLabels, they grab the mouse events... which I don't want. I want the LayeredPane to get it.
    So in the JLabel, I do:
          * Don't send to parent what you don't have to. Things like tooltips will
          * be broken if this is not handled carefully...
         public void mouseClicked(MouseEvent e) { }
         public void mouseMoved(MouseEvent e) { }
         public void mouseEntered(MouseEvent e) { }
         public void mouseExited(MouseEvent e) { }
         public void mouseReleased(MouseEvent e) { machine.dispatchToParent(e);}
         public void mouseDragged(MouseEvent e) { machine.dispatchToParent(e);}
         public void mousePressed(MouseEvent e) { machine.dispatchToParent(e);}(the "machine" above is a JPanel). Now in the JPanel, aka "machine", I do:
    Point point; int comp_x, comp_y; // for component with respect to this machine
    public void dispatchToParent(MouseEvent e) {
         point=((JComponent)e.getSource()).getLocation();
         //System.out.println("Dispatching: " + e.getX() + " " + e.getY());
         //System.out.println("Component position: " + ((JComponent)(e.getSource())).getLocation());
         if (e.getButton() > 0) MsgIFrame.println("ME at: " + comp_x + "," + comp_y + " button: " + e.getButton() );
         if (e.getButton() > 1) {
              popup(e);
              return;
         comp_x=(int)point.getX(); comp_y=(int)point.getY();
         comp_x+=this.getX(); comp_y+=this.getY();
         e.translatePoint(comp_x, comp_y);
         room.getLayeredPane().dispatchEvent(e);
    }Above, the "room" is a JInternalFrame with a LayeredPane inside it, as you can see. I do a little translating so the LayeredPane gets the event in its coordinate space, not those of its Components.

Maybe you are looking for