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 3 of 3

Thread: Typecasting?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    27
    My Mood
    Devilish
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Typecasting?

    On the Oracle website, at
    Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    I have this paragraph:
    It's easy to customize the content pane — setting the layout manager or adding
    a border, for example. However, there is one tiny gotcha. The getContentPane
    method returns a Container object, not a JComponent object. This means that
    if you want to take advantage of the content pane's JComponent features, you
    need to either typecast the return value or create your own component
    to be the content pane. Our examples generally take the second approach,
    since it's a little cleaner.
    However, it does not really give a definition of typecasting, or how it applies
    to UI programming, so I am wondering what the heck they are talking about.
    The Java API says:

    public Container getContentPane()
    Returns the contentPane object for this dialog.

    So I'm thinking they may be talking about casting
    jComponent = (JComponent) getContentPane();
    But that is just a shot in the dark.

    They say it is easier to
    create your own component to be the content
    pane
    and say that it is easier, but I have no idea what the hell they
    are talking about . . .


    Here is the whole code example they are providing:

    import java.awt.*;
    import javax.swing.*;
     
    /* TopLevelDemo.java requires no other files. */
    public class TopLevelDemo {
        /**       * Create the GUI and show it.  For thread safety, this method should be invoked from the event-dispatching thread. */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("TopLevelDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create the menu bar.  Make it have a green background.
            JMenuBar greenMenuBar = new JMenuBar();
            greenMenuBar.setOpaque(true);
            greenMenuBar.setBackground(new Color(154, 205, 127));
            greenMenuBar.setPreferredSize(new Dimension(200, 20));
     
            //Create a yellow label to put in the content pane.
            JLabel yellowLabel = new JLabel();
            yellowLabel.setOpaque(true);
            yellowLabel.setBackground(new Color(248, 213, 131));
            yellowLabel.setPreferredSize(new Dimension(200, 180));
     
            //Set the menu bar and add the label to the content pane.
            frame.setJMenuBar(greenMenuBar);
            frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);
            frame.setLocation(700, 400);  
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,065
    My Mood
    Mellow
    Thanks
    243
    Thanked 750 Times in 736 Posts

    Default Re: Typecasting?

    Your example of typecasting should work, but I'd suggest a better name for the jComponent.

    This line is the completion of the other approach:

    frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);

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

    Skywola (November 30th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    27
    My Mood
    Devilish
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: Typecasting?

    So it appears that they are saying that to use the JComponent features, you need to
    do one of three possible things:

    1. Typecast the return value.
    contentPane1  = (JComponent) getContentPane();

    2. Create your own component to be the content pane.
     //Create a panel and add components to it.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setBorder(someBorder);
    contentPane.add(someComponent, BorderLayout.CENTER);
    contentPane.add(anotherComponent, BorderLayout.PAGE_END);
     
     
    topLevelContainer.setContentPane(contentPane);

    3. Add a customized component to the content pane, covering the content pane completely.
     frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);
    Here they are adding a label.

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM