In my journey "back to the basics" to get a more solid foundation for my Java skills, I am curious about the difference between using the existing content pane of a JFrame vs. creating a new content pane in a JFrame.

I have two chunks of code... The first uses the existing content pane in a JFrame and the second creates a NEW content pane within a JFrame. The code is almost identical, but the output to the screen is very different.

When you add an object to an existing content pane, it fills the entire pane. This seems wrong to me for some reason, although I probably still don't understand the structure of the content pane within its JFrame. When adding an object to a newly created content pane, the behavior is more in line with what I expect to see happening.

The remaining differences in behavior appear to have to do with "layout". My assumption, which is almost always wrong with respect to Java, is that the layout for an existing JFrame content pane would be the same as a newly created content pane. This appears to NOT be the case since the text appears in different places in the window for each code sample.

Here is code sample 1, using the existing content pane...

import java.awt.*; 
import javax.swing.*;

class UseTheExistingContentPane extends JFrame {
  
  String txtGreeting = "Hello World";
  
  UseTheExistingContentPane() {            // constructor
    Container content = getContentPane();  // Use the default content pane.
    
    JTextField jtfGreeting = new JTextField(txtGreeting);
    Font font = new Font("Verdana", Font.BOLD, 18);
    jtfGreeting.setFont(font);
    jtfGreeting.setForeground(Color.BLUE);
    jtfGreeting.setBackground(Color.RED);
    content.add(jtfGreeting);

    this.setSize(400, 300);
    this.setVisible(true);
  }
  
  public static void main(String[] args){
    new UseTheExistingContentPane();
  }
  
}

Here is code sample 2, creating a new content pane...

import java.awt.*; 
import javax.swing.*;

class CreateYourOwnContentPane extends JFrame {
  
  String txtGreeting = "Hello World";
  
  CreateYourOwnContentPane() {             // constructor
    JPanel content = new JPanel();         // Create a new content pane.
    
    JTextField jtfGreeting = new JTextField(txtGreeting);
    Font font = new Font("Verdana", Font.BOLD, 18);
    jtfGreeting.setFont(font);
    jtfGreeting.setForeground(Color.BLUE);
    jtfGreeting.setBackground(Color.RED);
    content.add(jtfGreeting);
    setContentPane(content);

    this.setSize(400, 300);
    this.setVisible(true);
  }
  
  public static void main(String[] args){
    new CreateYourOwnContentPane();
  }
  
}

Perhaps someone could explain to me why the result for each code sample is so different.

Thanks,
Tahoe

Recommended Answers

All 2 Replies

java doc. Interface java.awt.LayoutManager
defaults:
1. javax.swing.JRootPane$1[hgap=0,vgap=0]
2. java.awt.FlowLayout[hgap=5,vgap=5,align=center]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.