0

The flow of the program is like this : Image from 1st set of buttons (leftButtonArea) is dragged to the 2nd set of buttons (rightButtonArea).

Each image has a unique name via setName(). The name of the image that was dragged over will be compared with the name of the button which it is dragged to.

If the name matches, I wish to disable that particular button from reacting to any hover event.

.setHoverEnabled(false) doesn't work =/

A similar SO thread regarding the disabling of event listeners, but it seems like the solutions such a using a glass pane are for whole components ? link

Edit: Somehow this works, but I am not sure of any side effects from this method source.removeMouseListener(source.getMouseListeners()[1]);

Edit2: Found something interesting.. This could be the reason why disabled buttons still reacts to mouseEvents.

"low-level: Component, Container, Focus, Key, Mouse, Paint, Window

semantic: Action, Adjustment, Item, Text

Only semantic events are affected by disabling any component. That is because they are directly handled by the component itself which is aware that it is enabled or not. Low level events can't be affected by disabling. If you stop to think about this when you disabled your label was it still visible. If it was then the paint event must have happened. Low level events will always happen and it is up to your handlers to query the component if it is enabled or not."

// 1st set of 4 buttons
for(int a=0; a<4; a++){

 leftButtonArea[a] = new JleftButtonArea(new ImageIcon(image)); 

 TransferHandler transfer = new TransferHandler("icon");
 leftButtonArea[a].setTransferHandler(transfer);

 leftButtonArea[a].addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent e){
    JleftButtonArea leftButtonArea = (JleftButtonArea)e.getSource();
    TransferHandler handle = leftButtonArea.getTransferHandler();
    handle.exportAsDrag(leftButtonArea, e, TransferHandler.COPY);
    // get unique name for the image that is dragged
    // to rightButtonArea
    name1 = e.getComponent().getName();
    }

});

}


// creates 2nd set of 4 buttons
 for(int b=0; b<4; b++){     
     rightleftButtonAreaArea[b] = new JleftButtonArea();

     // <---- creates unique name for each leftButtonArea ----->
     cc2 += 1;
     id2+="a"+cc2;
     rightleftButtonAreaArea[b].setName(id2); 
     // <---- creates unique name for each leftButtonArea ----->

     TransferHandler transfer1 = new TransferHandler("icon");
     rightleftButtonAreaArea[b].setTransferHandler(transfer1);

    rightleftButtonAreaArea[b].addMouseListener(new MouseAdapter(){
    @Override
    public void mouseExited(MouseEvent me){
    JleftButtonArea source = (JleftButtonArea)me.getSource();

    try{
    // compare unique name of image and the button in rightButtonArea
    // if they are the same, disable hover for the button
        if( name1.equals(source.getName())){
            // this doesn't work
            source.getName().setHoverEnabled(false); 
            // Somehow this works, but I am not sure of any side effects from this 
            source.removeMouseListener(source.getMouseListeners()[1]);
        }
        else{   
            source.setIcon(null);
        }
    }
    catch (NullPointerException e) 
    {

    }

    }       

    });
}
Community
  • 1
  • 1
iridescent
  • 305
  • 4
  • 14
  • Have you tried `setEnabled(false)`? – shuangwhywhy Jan 27 '13 at 10:21
  • `source.setEnabled(false);` works, but the imageicon will still get removed via the mouseEvent. – iridescent Jan 27 '13 at 10:35
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 27 '13 at 11:17
  • What specific thing do u want to do in removing hovering effect. ? For example of mouse is moved over that button with hovering effect false then it should stop changing its border line(as it happens in normal case when mouse is moved over button)..or anything like this? – Vishal K Jan 27 '13 at 11:47
  • 1
    I do not want the `ImageIcon` to be removed from the button. – iridescent Jan 27 '13 at 12:01
  • You can check if the button is disabled before removing the icon. – shuangwhywhy Jan 27 '13 at 12:16
  • A simple solution would be store all components registered with the `MouseEvent` in a `HashMap` along with `boolean` values which will dictate whether a mouseEvent should be performed or not - `true` or `false`. Thus in the method fired by the mouseEvent iterate the map and check if we should re-act to it or not by checking if the mouseEvents component matches any compnents in the map and than if the `boolean` value is `true` or `false`. But @mKorbels answer is more re-usable. +1 to him – David Kroukamp Jan 27 '13 at 13:07

2 Answers2

4
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

try

button.setRolloverEnabled(false);

PZR
  • 1