verification


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: verification

  1. #1
    Pete Guest

    verification


    I need to be certain I know what I'm doing here.
    Please let me know! Thanks!

    ##########################

    This line gets the content of the content pane
    and assigns it to an object variable container.
    What does the content pane include ? Is it the
    area within the frame ?

    Container container = getContentPane();

    ##########################

    Is this:
    Container container = getContentPane();

    equivalent to this:

    Container container = new Container();
    container = getContentPane();

    ##########################

    Is this:
    container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

    equivalent to this:

    FlowLayout x = new FlowLayout(FlowLayout.LEFT, 10, 20);
    container.setLayout(x);

    ##########################

    The following program is from a book I am using.
    I think I understand what's going on but I need
    to know for sure. Please correct me if I'm wrong!
    Here's what I think is going on:

    1. Get the content pane of the frame. The content
    pane is the area within the frame.
    2. assign it to an object (called container) to
    make the content pane accessible.
    3. add buttons to the frame; ie add buttons to the
    container according to the layout manager, which in
    this case is FlowLayout.
    4. create an instance of FlowLayout; ie a frame in
    main().
    5. set frame title and size.
    6. assure frame is visible.
    7. ready to run.

    import.javax.swing.JButton;
    import.javax.swing.JFrame;
    import.java.awt.Container;
    import.java.awt.FlowLayout;

    public class ShowFlowLayout extends JFrame
    {
    //Default constructor
    public ShowFlowLayout()
    {
    //Get the content pane of the frame
    Container container = getContentPane();

    //Set FlowLayout
    container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

    //Add buttons
    for(int i=1; i<=10; i++)
    container.add(new Jbutton("Component " + i));
    }

    //Main
    public static void main(String[] args)
    {
    ShowFlowLayout frame = new ShowFlowLayout();
    frame.setTitle("Show FlowLayout");
    frame.setDefaultCloseOperation("JFrame.EXIT_ON_CLOSE");
    frame.setSize(200,200);
    frame.setVisible(true);
    }
    }

    ##########################

    I'm not sure I understand the flow of this statement.
    Please correct if I'm wrong:
    f is a frame;
    p is a panel;

    f.getContentPane().add(p);

    1. Add panel p to frame f.
    2. Evaluate getContentPane to get the content of
    the content pane and return that value to the frame.
    3. The frame now has the panel p.
    4. Ready to run with panel p included.

    ##########################

  2. #2
    Vincent Guest

    Re: verification


    >Container container = getContentPane();

    Creates a reference (called container) to the contentPane of the current
    object.

    A frame in a standard windowing system can be thought to consist of a title
    bar (accessed in java by setTitle(someString) ), a menu bar (File, Edit,
    etc accessed by getJMenuBar() ) and (as it is called in java) a "contentPane"
    which contains everything else. (There are also other panes in the JFrame
    class such as rootPane and glassPane but you are unlikely to need to know
    about them yet.)


    >Is this:
    >Container container = getContentPane();
    >equivalent to this:
    >Container container = new Container();
    >container = getContentPane();


    They are similar (the end result is the same) but the first is the preferred
    technique as it is more efficient. The former creates a reference to a pre-existing
    object, the contentPane of the current object.

    The latter creates a brand new object of type Container and creates a reference
    called "container" for it. It then proceeds to redirect said reference to
    another object, namely the contentPane of the current object. As there are
    no other references to the initial object of type Container it will, at an
    appropriate moment, be destroyed by the garbage collection thread. As I
    hope you can see this results in wasted processor cycles and (temporarily)
    wasted memory.

    nb: It is possible that compilers exist that are capable recognising such
    wastage and avoiding it in the compiled bytecode (I don't know enough about
    compiler design to be certain) but it is better to use good programming techniques
    rather than rely on compiler design.


    >Is this:
    >container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
    >equivalent to this:
    >FlowLayout x = new FlowLayout(FlowLayout.LEFT, 10, 20);
    >container.setLayout(x);


    Basically yes. As to efficiency I do not believe that there is any real
    difference in this case (I could be wrong) but the former avoids unnecessarily
    assigning variable names whilst the latter allows one to define a particular
    layout that could be used by reused by many different frames. It is a matter
    of suitability to the task at hand.


    >The following program is from a book I am using.

    [Snip]

    Running this program results in the following:
    1. Create an object of type ShowFlowLayout, referenced by "frame".
    To do this the default constructor ( public ShowFlowLayout(){...} ) is called.
    This conducts the following steps:
    A. Create a reference ("container") to the contentPane of the object under
    construction.
    B. Use this reference to specify an appropriate LayoutManager for the contentPane.
    C. Add some buttons to the contentPane.

    2. Set a title, the default close operation and size of the frame.
    3. Make it visible to the user.


    >I'm not sure I understand the flow of this statement.
    >Please correct if I'm wrong:
    >f is a frame;
    >p is a panel;
    >
    >f.getContentPane().add(p);


    f.getContentPane() represents a reference to the contentPane of the JFrame.
    This is why you can do either of the following:
    Container c = f.getContentPane(); someMethod(c);
    or someMethod(f.getContentPane());

    Hence as f.getContentPane() refers to an object of type Container you can
    then apply any appropriate methods for that container, namely add(p).

    Hence f.getContentPane().add(p); will add the Panel p to the contentPane
    of JFrame f.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center