-1

I am trying to add a JPanel onto the JFrame but I cannot see the panel. I also am trying to add a JTable to the panel but after I can get this working. Here is my code:

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

public class table
{

    public table(){

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setSize(500,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel, BorderLayout.CENTER);
        frame.setVisible(true);

    }
}

In another class, I have the code:

public class Main {

    public static void main(String[] args){
        new table();
    }

}

Here is a screenshot of the JFrame with no JPanel (I think). Here is a screen shot of the JFrame.

John Collins
  • 121
  • 2
  • 14
  • 1
    Move `frame.setVisible(true);` to the end. – Pshemo Sep 06 '17 at 18:20
  • Either that, or revalidate it after all is said and done. – Mad Physicist Sep 06 '17 at 18:22
  • Adding that line to the end didn't work for me. – John Collins Sep 06 '17 at 18:34
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). The advice of @MadPhysicist would have been what I and many others offer, based on the uncompilable code snippet seen above. In the event it does not work, we'll need to see what else is happening to be able to understand what is wrong. – Andrew Thompson Sep 06 '17 at 18:49
  • Please indent your code properly. – Mad Physicist Sep 06 '17 at 18:57
  • Thanks. Can you show how you run this code? Also, a screenshot of the window would be nice. – Mad Physicist Sep 06 '17 at 18:59
  • The panel is there. Do `panel.setBackground(Color.RED);` before you add it. Why did you think it wasn't there before? – Mad Physicist Sep 06 '17 at 19:07
  • I have updated my answer, which I still believe to be the correct one, with the additional clarification. – Mad Physicist Sep 06 '17 at 19:12
  • Thanks, seems like there was no problem after all - even when the setVisible was not at the end. When I comment out the add JPanel line the JFrame looks the same as when it is not commented out. I tested it with changing the background and the JPanel is there, just looks the same as no JPanel. Useful information anyway. – John Collins Sep 06 '17 at 19:17
  • The set visible thing most likely works because you are adding to the root component. If you try adding sub-components to the frame, there is a good chance they will not show up until you revalidate. – Mad Physicist Sep 06 '17 at 19:20

2 Answers2

1

If you take a look at the docs for Window.setVisible(), you might notice a note about the input parameter that says

The Window will be validated prior to being made visible.

Validation is the key to making new components show up when you add them to a window. You have two options:

  1. Move the call to setVisible to the end of your code, once everything is added to the window:

    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setSize(500,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.CENTER);
    frame.setVisible(true);
    
  2. Re-validate the frame yourself after adding the panel:

    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setSize(500,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    JPanel panel = new JPanel();
    frame.add(panel, BorderLayout.CENTER);
    frame.validate();
    

A note from the docs for Container.validate():

Validating the container may be a quite time-consuming operation. For performance reasons a developer may postpone the validation of the hierarchy till a set of layout-related operations completes, e.g. after adding all the children to the container.

Using the first option is probably better.

Clarification

Keep in mind that the background color of a JPanel is the same as the background color of the JFrame's content pane by default. You will not be able to determine that it was added correctly visually. At @W.K.S.'s suggestion, you can change the panel's color so you can see it clearly in the frame:

JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);

Don't forget to import java.awt.Color; in this case.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
1

Try Giving your panel a background color:

panel.setBackground(Color.BLUE);
W.K.S
  • 9,787
  • 15
  • 75
  • 122