Giovanni Far
Giovanni Far

Reputation: 1653

java swing - remove a jpanel from the frame

It's strange i don't know why but i cannot remove my jpanel from my frame. I tried everything but nothing after this instrunctions i still continue to see the jpanel:

frame.getContentPane().remove(myPanel);

i have also tried to do:

frame.remove(...);
frame.add(...);
frame.revalidate();
frame.repaint();

but still i continue to see the panel in the frame. This is my code (im developing a little app about the student note) and now i would like to remove my first panel just to do an experiment:

package StudApp;

import java.awt.BorderLayout;

public class StudApp {
    private JPanel homeFirstRun;
    private ArrayList<Corso> corsi = new ArrayList<Corso>();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StudApp();
            }
        });
    }

    /**
     * Create the frame.
     */
    public StudApp() {
        JFrame frame = new JFrame("Student Note");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 450, 300);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu menuHelp = new JMenu("Help");
        menuBar.add(menuHelp);

        JMenuItem menuIstrStud = new JMenuItem("Istruzioni Student Note");
        menuHelp.add(menuIstrStud);
        homeFirstRun = new JPanel();
        homeFirstRun.setBorder(new EmptyBorder(5, 5, 5, 5));
        frame.setContentPane(homeFirstRun);
        homeFirstRun.setLayout(null);

        JLabel welcomeMessage = new JLabel("Welcome to Student Note");
        welcomeMessage.setBounds(5, 5, 424, 18);
        welcomeMessage.setForeground(Color.DARK_GRAY);
        welcomeMessage.setHorizontalAlignment(SwingConstants.CENTER);
        welcomeMessage.setFont(new Font("Verdana", Font.BOLD, 14));
        homeFirstRun.add(welcomeMessage);

        JLabel welcomeCit = new JLabel("\"Software is like sex, it's better when it's free.\"");
        welcomeCit.setFont(new Font("Verdana", Font.ITALIC, 11));
        welcomeCit.setHorizontalAlignment(SwingConstants.CENTER);
        welcomeCit.setBounds(35, 199, 361, 14);
        homeFirstRun.add(welcomeCit);

        JTextArea welcomeTextArea = new JTextArea();
        welcomeTextArea.setFont(new Font("Verdana", Font.PLAIN, 13));
        welcomeTextArea.setText(" I think it's your first time here.\n\n"
                            + " So the first step is to create a new course to\n insert your grades.\n\n");
        welcomeTextArea.setEditable(false);
        welcomeTextArea.setBounds(27, 34, 381, 184);
        homeFirstRun.add(welcomeTextArea);

        frame.setVisible(true);
        frame.remove(homeFirstRun); //here im doing a test because i wanna see if the homeFirstRun panel go out from the frame
                                    // but not it remains.
    }
}

Upvotes: 0

Views: 1171

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Basically, because you used frame.setContentPane(homeFirstRun);, frame.remove(homeFirstRun); is being delegated to the content pane, so it's like saying...

homeFirstRun.remove(homeFirstRun);

Which obviously makes no sense...

Instead trying using something like...

frame.add(homeFirstRun);
//...
frame.remove(homeFirstRun);

Or a CardLayout, or in fact, any layout manager at all...

Upvotes: 1

Related Questions