Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:36:30+00:00 2026-06-01T17:36:30+00:00

I am reading a great book called Swing: A Beginner’s guide. There is this

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:36:32+00:00Added an answer on June 1, 2026 at 5:36 pm

    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();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which
I had learnt by reading your great answers here, that it is not good
I was reading up on singleton class design in C# on this great resource
Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
I'm currently reading Head First's Object Oriented Analysis and Design. The book states that
Javascript: The Good Parts is a great book. Often I find myself reading passages
I am reading Write Great Code Volume 2 and it shows the following strlen
I'm reading Jon Skeet's book and he offers a great example: List<Product> products =
I have been reading great posts in this forum and got close to what
I am reading a great Rails tutorial and came across a passage that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.