Results 1 to 6 of 6

Thread: How to write a Change Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    162

    How to write a Change Listener in Java?

    Hi friends,
    I am new to this forum, so please ignore my mistakes. I have started doing Java recently. I want to use the change listener, so that I can know when the source object is changed.!! But I don't know about the Change Listener which is used for doing that purpose.?! So please tell me how to write a Change Listener in Java.?? Hoping that someone would help me..!!
    Processor: AMD Athlon(tm) 64 X2 Dual Core @ ~2.2 GHz
    Memory: 1024MB RAM
    Hard Drive: 200 GB
    Video Card: RADEON X300/X550 Series

  2. #2
    Join Date
    Jul 2006
    Posts
    442

    Re: How to write a Change Listener in Java?

    A change listener is registered on an object and the listener is notified when the object has changed. A change listener must have a stateChanged (...) method, which has a ChangeEvent parameter. Focus events are fired whenever a component gains or loses the keyboard focus. You can call the ChangeEvent getSource() method to get the slider which caused this event. You should be knowing the basic thing that component generally gains the focus when the user clicks it.
    "When they give you ruled paper, write the other way..." J.R.J.

  3. #3
    Join Date
    Aug 2006
    Posts
    227

    Re: How to write a Change Listener in Java?

    The following is a method of the ChangeListener Interface, which is given below :
    • stateChanged(ChangeEvent) - This method is called when the listened-to component changes state.

    A ChangeListener has only one method. Since it has only one method, there is no corresponding adapter class.
    The following is the method of the ChangeEvent Class :
    • Object getSource() - This method returns the object that fired the event.
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to write a Change Listener in Java?

    There are many swing components which relies on change events for basic functionality. Components like sliders, color choosers and spinners depends on change events. If you want to know when the value in a slider changes, you need to register a change listener. When you register a change listener on a spinner, a component introduced will be notified when the spinner's value changes. Hope that you can understand properly when to use the change listener.!!

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to write a Change Listener in Java?

    I am providing an example of coding which is useful to change event handling for a slider. Have a look on the following example :
    Code:
       framesPerSecond.addChangeListener(new SliderListener());
        ...
        class SliderListener implements ChangeListener {
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider)e.getSource();
                if (!source.getValueIsAdjusting()) {
                    int fps = (int)source.getValue();
        	    ...
                }    
            }
        }

  6. #6
    Join Date
    Mar 2008
    Posts
    672

    Re: How to write a Change Listener in Java?

    I have provided you with an example of coding having the Change Listener. You will find the coding very lengthy but it can be useful for you to understand things in full coding :
    Code:
    import java.awt.*;
    import javax.swing.event.*;
    import javax.swing.colorchooser.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ColorChooserDemo extends JPanel
                                  implements ChangeListener {
    
        protected JColorChooser tcc;
        protected JLabel banner;
    
        public ColorChooserDemo() {
            super(new BorderLayout());
    
            banner = new JLabel("This is Study Place!",
                                JLabel.CENTER);
            banner.setForeground(Color.red);
            banner.setBackground(Color.yellow);
            banner.setOpaque(true);
            banner.setFont(new Font("Times", Font.BOLD, 22));
            banner.setPreferredSize(new Dimension(120, 77));
    
            JPanel bannerPanel = new JPanel(new BorderLayout());
            bannerPanel.add(banner, BorderLayout.CENTER);
            bannerPanel.setBorder(BorderFactory.createTitledBorder("Banner"));
    
            tcc = new JColorChooser(banner.getForeground());
            tcc.getSelectionModel().addChangeListener(this);
            tcc.setBorder(BorderFactory.createTitledBorder(
                                                 "Choose Text Color"));
    
            add(bannerPanel, BorderLayout.CENTER);
            add(tcc, BorderLayout.PAGE_END);
        }
    
        public void stateChanged(ChangeEvent e) {
            Color newColor = tcc.getColor();
            banner.setForeground(newColor);
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("ColorChooserDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JComponent newContentPane = new ColorChooserDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

Similar Threads

  1. How to write a Property Change Listener in Java?
    By SKREECH in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 04:21 AM
  2. How to write a Mouse Listener in Java?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 03:47 AM
  3. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  4. How to write an Item Listener in Java?
    By shivendra in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 01:13 AM
  5. How to write an Action Listener in Java?
    By Beter 2 Burn Out in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,493,644.40585 seconds with 17 queries