5 Replies - 313 Views - Last Post: 27 January 2013 - 05:55 AM Rate Topic: -----

#1 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 08-September 12

disable hover for a specific JButton.

Posted 27 January 2013 - 03:01 AM

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 buttons 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 =/

    // 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");
         rightButtonArea[b].setTransferHandler(transfer1);

        rightButtonArea[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); 

            }
            else{   
                source.setIcon(null);
            }
        }
        catch (NullPointerException e) 
        {
             
        }
        
        }       

        });
    }
            

This post has been edited by Gisengryl: 27 January 2013 - 05:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: disable hover for a specific JButton.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,655
  • Joined: 20-September 08

Re: disable hover for a specific JButton.

Posted 27 January 2013 - 03:58 AM

Quote

.setHoverEnabled(false) doesn't work =/

Does it even exist? ;) What about the following?
http://docs.oracle.c...nabled(boolean)
Was This Post Helpful? 1
  • +
  • -

#3 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 08-September 12

Re: disable hover for a specific JButton.

Posted 27 January 2013 - 05:16 AM

View Postg00se, on 27 January 2013 - 03:58 AM, said:

Quote

.setHoverEnabled(false) doesn't work =/

Does it even exist? ;)/> What about the following?
http://docs.oracle.c...nabled(boolean)


Haha I recalled googling it somewhere.. :P

Ah well, in any case, I tried out source.setRolloverEnabled(false); , but it didn't work.

I tried out source.removeMouseListener(source.getMouseListeners()[1]); and somehow it worked, but I am not sure if there's any side effects from doing so.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: disable hover for a specific JButton.

Posted 27 January 2013 - 05:37 AM

Rather than prescribing your own medicine, as in "disable hover", it might be more productive to describe the existing effect you're trying to change and how you'd like that behavior to change. For example,

Current behavior: when the button is hovered over with the mouse cursor, it changes color, looking as though it would be selected if the user clicked on it (this effect is typical, though the exact effect can be dependent on the user system's look and feel).

Desired behavior: when the button is hovered over with the mouse cursor, I want no changes in its color or border or any other indication that it could be selected if clicked.

Knowing this, or more precisely the effect you're trying to achieve, helps us identify the changes that could be made to achieve the desired effect.

This post has been edited by GregBrannon: 27 January 2013 - 05:37 AM

Was This Post Helpful? 1
  • +
  • -

#5 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 08-September 12

Re: disable hover for a specific JButton.

Posted 27 January 2013 - 05:51 AM

Current behavior: When the button (rightButtonArea) is hovered over with the mouse cursor, the mouseEvent runs and compares the setName of the image being dragged to it and the setName of the button. If it is the same, nothing happens. If it is different, ImageIcon will be set to null.

Desired behavior: When the button is hovered over with the mouse cursor, the mouseEvent still exists, but it only executes for buttons that do not have ImageIcons on them.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,655
  • Joined: 20-September 08

Re: disable hover for a specific JButton.

Posted 27 January 2013 - 05:55 AM

Quote

Desired behavior: When the button is hovered over with the mouse cursor, the mouseEvent still exists, but it only executes for buttons that do not have ImageIcons on them.
So, in the MouseListener method for that button, check if it has an ImageIcon. If it doesn't, return.

Something like

    public void mouseEntered(MouseEvent e) {
        Object o = e.getSource();

        if (((AbstractButton) o).getIcon() == null) {
            return;
        } else {
            // Process
        }
    }


This post has been edited by g00se: 27 January 2013 - 06:00 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1