I'm trying to make a method that would create a new tab with a specified name and a white background. However, nothing I pass in (whether it's a JPanel, JButton, etc.) would show up in the final result. I can't tell what's wrong.

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTabbedPane;
import java.awt.Color;
import java.awt.Dimension;

public class PaintGUI extends JFrame {

  private JPanel center;
  private JTabbedPane tabBar;

  public PaintGUI() {
    setLayout( new BorderLayout(5,5) );
    center = new JPanel();
    center.setLayout( new BorderLayout() );
    add( center, BorderLayout.CENTER );

    tabBar = new JTabbedPane();
    tabBar.setPreferredSize( new Dimension(650, 25) );
    center.add( tabBar, BorderLayout.NORTH );
    createTab( "Document 1" );

  }

  private void createTab( String name ) {
    
    // Create new canvas and set its background to white
    JPanel canvas = new JPanel();
    canvas.setBackground( Color.WHITE );
    
    // Link the canvas to the tab
    tabBar.addTab( name, canvas );
    
  }
  
}

Test class:

import javax.swing.JFrame;

public class PaintGUI_TEST
{
  public static void main( String args[] )
  {
    
    // Create new app app
    PaintGUI app = new PaintGUI();
    // Set the frame to exit when it is closed
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
    app.setSize( 850, 500 ); // set the size of the frame
    app.setVisible( true ); // make the frame visible    
    
  }
}

Am I supposed to return something? I tried making the JPanel a local object, and even tried moving the method code into the constructor, but nothing worked. Sorry if the mistake is glaringly obvious, but I really can't find it.

Nevermind, I found the problem. All I need to do now is find out how to delete this thread.

This article has been dead for over six months. Start a new discussion instead.