Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: java top level container and EventLstenerList

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default java top level container and EventLstenerList

    Hello everyone

    I have two questions about event handling (Button) and top level container

    In the following sentences it says :

    Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second

    Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    I can't understand how we can have to add a component from one container to - another container - also how we can have two containers in a
    Class (means we should have two frames possible)

    2. When we want to add a button listener it sent ActionListenrt to a list of itself why we should use a list ?
    A button only wants to add on one ActionListener or one source, but why it uses List

    thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,114
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: java top level container and EventLstenerList

    1 - Multiple containers are possible. JFrame, JPanel, and JDialog are all containers and can all be used in multiple numbers in a single application. You may have multiple containers displayed that all have a Cancel button that does the same thing, closes the dialog. To save time, you might think of using the same Cancel button in each of the containers. The component rule you've quoted says that using the same Cancel button in multiple containers AT THE SAME TIME is not possible.

    2 - Components can have multiple ActionListeners.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    mohsen.noor (August 31st, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java top level container and EventLstenerList

    Thanks
    But there is a question about "Components can have multiple ActionListeners" suppose in a button which can only do one thing
    What is the use of multiple ActionListener. Can you elaborate more.
    Thanks again

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,114
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: java top level container and EventLstenerList

    a button which can only do one thing
    That's a self-imposed, unnecessary limit, or incorrect assumption. A button, even with only one action listener assigned to it, could do multiple things.

    Perhaps one action listener is common to multiple components, providing the ability to do the same thing multiple ways, resetting the program to a previous state, for example. While multiple buttons may perform this action when pressed, they may each have their own specific function, closing a dialog, or reading an input field, etc.

    Knowing that one component can be assigned multiple action listeners is the important point. You may someday find that useful.

Similar Threads

  1. Replies: 0
    Last Post: October 14th, 2013, 09:30 PM
  2. java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPanel
    By Freshtilldeath0 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2012, 07:19 AM
  3. Top Java frameworks as rated by developers
    By pjagielski in forum The Cafe
    Replies: 0
    Last Post: August 15th, 2012, 01:05 PM
  4. [Jade-Java]Distributed Container
    By ermasto in forum Algorithms & Recursion
    Replies: 0
    Last Post: January 25th, 2011, 06:08 AM
  5. Design Java Generic Container-please help!!!
    By zhoujackji in forum Collections and Generics
    Replies: 1
    Last Post: November 13th, 2010, 05:55 AM