Results 1 to 5 of 5

Thread: How to write a Container Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    168

    How to write a Container Listener in Java?

    Hi friends,
    I am posting on this forum for the first time. Need your help. I am stuck while using the Container Listener. I have tried different ways for getting the result but was not succeeded. Please tell me how to write a Container Listener.?? Somebody out there would be interested in solving my problem.!!
    "All gave some, some gave all."

  2. #2
    Join Date
    Nov 2008
    Posts
    996

    Re: How to write a Container Listener in Java?

    I think that before going for the coding you should know what exactly the container listener are and when they are used.!?? I am trying to explain you in simple words. Container events are fired by a Container just after a component is added to or removed from the container. These events are for notification only when no container listener need be present for components to be successfully added or removed. Hope that you got the point that I was trying to tell you,..!!

  3. #3
    Join Date
    Jul 2006
    Posts
    289

    Re: How to write a Container Listener in Java?

    The following are the methods that are called when the ContainerListener Interface is used. The corresponding adapter for the ContainerListener Interface class is ContainerAdapter.
    • componentAdded(ContainerEvent) - This method is called just after a component is added to the listened-to container.
    • componentRemoved(ContainerEvent) - This method is called just after a component is removed from the listened-to container.
    Signatures reduce available bandwidth

  4. #4
    Join Date
    Aug 2006
    Posts
    235

    Re: How to write a Container Listener in Java?

    The following are the coding for demo's container event handling code :
    Code:
    public class ContainerEventDemo ... implements ContainerListener ... {
    	buttonPanel = new JPanel(new GridLayout(2,2));
    	buttonPanel.addContainerListener(this);
        ...
        public void componentAdded(ContainerEvent e) {
    	displayMessage(" added to ", e);
        }
    
        public void componentRemoved(ContainerEvent e) {
    	displayMessage(" removed from ", e);
        }
    
        void displayMessage(String action, ContainerEvent e) {
    	display.append(((JButton)e.getChild()).getText()
    		       + " was"
    		       + action
    		       + e.getContainer().getClass().getName()
    		       + newline);
        }
        ...
    }
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

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

    Re: How to write a Container Listener in Java?

    For creating the GUI, you can use the following code :
    Code:
    private static void createAndShowGUI() {
    
            JFrame frame = new JFrame("TrialContainerEvent");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JComponent newContentPane = new ContainerEventDemo();
            newContentPane.setOpaque(true); 
            frame.setContentPane(newContentPane);
    
            frame.pack();
            frame.setVisible(true);
        }

Similar Threads

  1. 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
  2. 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
  3. 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
  4. How to write a Component Listener in Java?
    By hariharan_00 in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:28 AM
  5. How to write a Caret Listener in Java?
    By Dilbert in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:33 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,313,776.14848 seconds with 17 queries