简体   繁体   中英

How to create a rollover button in swing

So I have two buttons,

JButton playB = new JButton(new ImageIcon("res/playA.png"));
playB.setBorder(BorderFactory.createEmptyBorder());
playB.setContentAreaFilled(false);
JButton playA = new JButton(new ImageIcon("res/playA.png"));
playA.setBorder(BorderFactory.createEmptyBorder());
playA.setContentAreaFilled(false);

How would I create it so that when I move my mouse over button A it switches to button B? All I can think of doing is using a mouse listener but I'm wondering if there would be any better way.

How would I create it so that when I move my mouse over button A it switches to button B? All I can think of doing is using a mouse listener but I'm wondering if there would be any better way.

Note: The setRolloverIcon method can't be used for this.

eg

JButton.getModel().addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        ButtonModel model = (ButtonModel) e.getSource();
        if (model.isRollover()) {
            //doSomething
        } else if (model.isPressed()) {
           //doSomething
        }  // etc
    }
});

EDIT

again back

How would I create it so that when I move my mouse over button A it switches to button B? All I can think of doing is using a mouse listener but I'm wondering if there would be any better way.

  • conclusion then first JButton (button A) isn't, never will be accesible on users side from Mouse or KeyEvent s, because both those event will witch that (immediatelly) to roll_over ,

  • then there is only one way, call programatically JButton.doClick() from KeyBinding s, but still is there question when, how and for why reason ... complicating simple things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM