Pergunta

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.

Foi útil?

Solução

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.

e.g.

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 KeyEvents, because both those event will witch that (immediatelly) to roll_over,

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top