Java isRollover() method does not produce an event in my swing application

Go To StackoverFlow.com

5

I am reading a great book called Swing: A Beginner's guide. There is this code in the book that creates a button and a label that alerts on button's state change events :

//Demonstrate a change listener and the button model

package swingexample2_6;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class ChangeDemo {

    JButton jbtn;
    JLabel jlab;

    public ChangeDemo() {
        //Create a new JFrame container
        JFrame jfrm = new JFrame("Button Change Events");

        //Specify FlowLayout for the layout manager
        jfrm.getContentPane().setLayout(new FlowLayout());

        //Give the frame an initial size
        jfrm.setSize(250, 160);

        //Terminate the program when the user closes the application
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create an empty label
        jlab = new JLabel();

        //Make a button
        jbtn = new JButton("Press for Change Event Test");

        //--Add change listener
        jbtn.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent ce) {
                ButtonModel mod = jbtn.getModel();
                String what = "";

                if (mod.isEnabled()) {
                    what += "Enabled<br>";
                }
                if (mod.isRollover()) {
                    what += "Rollover<br>";
                }
                if (mod.isArmed()) {
                    what += "Armed<br>";
                }
                if (mod.isPressed()) {
                    what += "Pressed<br>";
                }

                //Notice that this label's text is HTML
                jlab.setText("<html>Current stats:<br>" + what);
            }
        });


        //Add the components to the content pane
        jfrm.getContentPane().add(jbtn);
        jfrm.getContentPane().add(jlab);

        //Display the frame
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ChangeDemo();
            }
        });
    }
}

Everything is working fine except for the rollover event. The underlying operating system is Mac OS Lion. Should I blame Lion for this swing problem or am I doing something wrong? Thank you.

Update 1 : My neatbeans settings picture (I hope it helps) settings http://i44.tinypic.com/vdzg92.png

2012-04-05 22:18
by skiabox
works fine for me...Windows 7 O - mre 2012-04-05 22:24
@mre Same here (also using Win. 7 with a late 1.6 JRE). What is the version of the JRE used? (Question to both mre & the original poster. - Andrew Thompson 2012-04-05 22:30
jdk used is 1.6. - skiabox 2012-04-05 22:35
@AndrewThompson, jdk 1.6.0_2 - mre 2012-04-05 22:40
So probably it is a mac OS incompatibility with swing.I wonder if I should email apple technical support - skiabox 2012-04-05 22:43
@skiabox I am hoping to see a reply from at least one more OS X user before tending toward to that conclusion/strategy - Andrew Thompson 2012-04-05 22:49
+1 for sscce - trashgod 2012-04-06 00:24
@skiabox, Instead of a screenshot, can you just post the text - mre 2012-04-06 17:03
@mre, what text - skiabox 2012-04-06 17:41


5

Code as tested on Leopard with Java version 1.6.0_26 shown below. The trailing </html> tag fixed a highlighting glitch on rollover.

Addendum: Using the updated example below, adding setRolloverEnabled(true) allows the model to work as expected. Interestingly, the Mac UI delegate, com.apple.laf.AquaButtonUI, does nothing when isRollover() is true. If it's important to your application, you can take the desired action when the following predicate is true:

System.getProperty("os.name").startsWith("Mac OS X")

For reference, this example demonstrates setRolloverIcon().

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class ChangeDemo {

    private JButton jbtn;
    private JLabel jlab;

    public ChangeDemo() {
        JFrame jfrm = new JFrame("Button Change Events");
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.setLayout(new GridLayout(0, 1));
        jlab = new JLabel("", JLabel.CENTER);
        jbtn = new JButton("Press for Change Event Test");
        jbtn.setRolloverEnabled(true);

        jbtn.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent ce) {
                ButtonModel mod = jbtn.getModel();
                String what = "";

                if (mod.isEnabled()) {
                    what += "Enabled<br>";
                }
                if (mod.isRollover()) {
                    what += "Rollover<br>";
                }
                if (mod.isArmed()) {
                    what += "Armed<br>";
                }
                if (mod.isPressed()) {
                    what += "Pressed<br>";
                }

                //Notice that this label's text is HTML
                jlab.setText("<html>Current stats:<br>" + what + "</html>");
            }
        });

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(50, 10, 0, 10));
        panel.add(jbtn);
        jfrm.add(panel);
        jfrm.add(jlab);

        jfrm.pack();
        jfrm.setLocationRelativeTo(null);
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ChangeDemo changeDemo = new ChangeDemo();
            }
        });
    }
}
2012-04-06 00:19
by trashgod
That is getting more interesting over time - skiabox 2012-04-06 07:27
I still get no rollover effect event with the closing html tag - skiabox 2012-04-06 07:28
+1, works fine on Windows 7 with 1.7.0_03 :- - nIcE cOw 2012-04-06 07:37
original post updated with new information..(netbeans settings - skiabox 2012-04-06 07:49
I made an error, too; more when I get it sorted out - trashgod 2012-04-06 15:10
@skiabox: Sorry, I was inadvertently testing with javax.swing.plaf.metal.MetalLookAndFeel; more above - trashgod 2012-04-06 17:02
awesome answer trashgod!maybe stackoverflow should add star grades to the answers so that people can grade such amazing answers with 5 stars!thnx again - skiabox 2012-04-06 17:36
btw I think that closing html tag is not needed.. - skiabox 2012-04-06 17:42
@skiabox: Kudos for your interesting sscce from which I learned a lot. I think the small shift I was seeing with MetalLookAndFeel was layout related - trashgod 2012-04-06 18:29


0

Okay okay. I know this has been answered really well already, and the OP's question was on a Mac, but this answer needs sharin'.

If you are on Windows 7, rollover effects won't work if you have the theme set to "Classic". Set it to "Basic" and rollovers will work.

2015-04-15 23:40
by Russbear
Ads