Results 1 to 5 of 5

Thread: How to write a Component Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    142

    How to write a Component Listener in Java?

    Hi friends,
    Last time you all guys helped me a lot. So i thought that this is perfect place to post my query. I am getting confused while writing the Component Listener..!! I have tried many different ways but not succeeding in any of them. Please help by telling me how to write a Component Listener in Java.?? Hope that I will get the response sooner.!!
    ~HARE KRISHNA HARE RAM~

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to write a Component Listener in Java?

    Since, you don't know much about the Component listener, I thought that explaining you about that would be better than telling you the coding. The Component listener is a listener interface for receiving component events. You can define component as an object having a graphical representation that can be displayed on the screen. The user can also interact with the component. Buttons, checkboxes, and scrollbars of a typical graphical user interface are some of the examples of components.

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    Re: How to write a Component Listener in Java?

    The following are the methods of the ComponentListener Interface :
    • componentHidden(ComponentEvent) - This method is called after the listened-to component is hidden.
    • componentMoved(ComponentEvent) - This method is called after the listened-to component moves.
    • componentResized(ComponentEvent) - When the listened-to component's size changes, this method is called.

    The following is the method of the ComponentEvent Class :
    • Component getComponent() - This method returns the component that fired the event.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to write a Component Listener in Java?

    The following is the code related to handling component events :
    Code:
    public class ComponentEventDemo ... implements ComponentListener {
        static JFrame frame;
        JLabel label;
        ...
        public ComponentEventDemo() {
            ...
            JPanel panel = new JPanel(new BorderLayout());
            label = new JLabel("Example of Label", JLabel.CENTER);
            label.addComponentListener(this);
            panel.add(label, BorderLayout.CENTER);
    
            JCheckBox checkbox = new JCheckBox("Label visible", true);
            checkbox.addComponentListener(this);
            panel.add(checkbox, BorderLayout.PAGE_END);
            panel.addComponentListener(this);
            ...
            frame.addComponentListener(this);
        }
        ...
         public void componentHidden(ComponentEvent e) {
            displayMessage(e.getComponent().getClass().getName() + " --- Hidden");
        }
    
        public void componentMoved(ComponentEvent e) {
            displayMessage(e.getComponent().getClass().getName() + " --- Moved");
        }
    
        public void componentResized(ComponentEvent e) {
            displayMessage(e.getComponent().getClass().getName() + " --- Resized ");            
        }
    
        public void componentShown(ComponentEvent e) {
            displayMessage(e.getComponent().getClass().getName() + " --- Shown");
    
        }
    
        public static void main(String[] args) {
            ...
            frame = new JFrame("ComponentEventDemo");
            ...
            JComponent newContentPane = new ComponentEventDemo();
            frame.setContentPane(newContentPane);
            ...
        }
    }

  5. #5
    Join Date
    Jul 2006
    Posts
    442

    Re: How to write a Component Listener in Java?

    The following sample creates the GUI and show it. Just have a look at the example :
    Code:
    private static void createAndShowGUI() {
            frame = new JFrame("TrialComponentEvent");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JComponent newContentPane = new ComponentEventDemo();
            newContentPane.setOpaque(true);
            frame.setContentPane(newContentPane);
    
            frame.pack();
            frame.setVisible(true);
        }
    "When they give you ruled paper, write the other way..." J.R.J.

Similar Threads

  1. How to write a Document Listener in Java?
    By pushpendra in forum Software Development
    Replies: 8
    Last Post: 18-03-2010, 01:22 PM
  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 a Focus Listener in Java?
    By The Recruiter in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 07:05 AM
  5. How to write a Container Listener in Java?
    By Pratim in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:43 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,714,269,196.92126 seconds with 17 queries