First panel added to a frame disappears

0 votes

Below is an example of adding two panels to a frame. Only one panel (the 2nd, red panel) appears.

Disappearing Panel In Frame

Why does the first panel disappear?

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

public class DisappearingPanelInFrame {

    DisappearingPanelInFrame() {
        JFrame f = new JFrame(this.getClass().getSimpleName());
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        f.add(new ColoredPanel(Color.GREEN));
        f.add(new ColoredPanel(Color.RED));

        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new DisappearingPanelInFrame();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class ColoredPanel extends JPanel {

    ColoredPanel(Color color) {
        setBackground(color);
        setBorder(new EmptyBorder(20, 150, 20, 150));
    }
}
Mar 1, 2019 in Java by Sushmita
• 6,920 points
1,455 views

1 answer to this question.

0 votes
  • The default layout of a JFrame (or more specifically in this case, the content pane of the frame) is a BorderLayout.
  • When adding a component to a BordeLayout with no constraint, the Swing API will put the component in the CENTER.
  • A BorderLayout can contain exactly one component in each of the 5 layout constraints.
  • When a second component is added to the same (in this case CENTER) constraint of a BorderLayout, this implementation of Java will display the last component added.

As to what would be a better approach depends on the specific needs of the user interface.

answered Mar 1, 2019 by developer_1
• 3,350 points

Related Questions In Java

0 votes
1 answer

How can one day be added to a date?

One possible solution could be using calendar ...READ MORE

answered Jun 8, 2018 in Java by Daisy
• 8,140 points
627 views
0 votes
1 answer

How to call setUndecorated() after a frame is made visible?

Hello kartik, You can Try: dispose(); setUndecorated(true); setVisible(true); Check it Out. Hope ...READ MORE

answered Apr 21, 2020 in Java by Niroj
• 82,840 points
1,602 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,223 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,311 views
0 votes
1 answer

revalidate() vs repaint() in Java Swing

You need to call repaint() and revalidate() both in order ...READ MORE

answered Sep 20, 2018 in Java by code.reaper12
• 3,500 points
11,124 views
0 votes
1 answer

Fetching screen resolution using Java

You can fetch the screen resolution using the Toolkit.getScreenSize() method ...READ MORE

answered Sep 20, 2018 in Java by anto.trigg4
• 3,440 points
4,803 views
0 votes
1 answer

How to change selection listener in JComboBox?

It should respond to ActionListeners, like this: combo.addActionListener (new ...READ MORE

answered Nov 15, 2018 in Java by Frankie
• 9,830 points
1,167 views
0 votes
1 answer

Describe Heavy Weight Components Mean In Java Programming?

Heavy weight components like Abstract Window Toolkit ...READ MORE

answered Nov 28, 2018 in Java by Frankie
• 9,830 points
1,900 views
0 votes
3 answers

Convert date object to a String

We parse the full date to time ...READ MORE

answered Jul 31, 2018 in Java by samarth295
• 2,220 points
1,547 views
0 votes
2 answers

what is the best way to convert an ArrayList to a String

You could probably use the Joiner class ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
1,299 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP