Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:50:20+00:00 2026-05-27T00:50:20+00:00

I have a JFrame which uses BorderLayout as a layout manager. I also have

  • 0

I have a JFrame which uses BorderLayout as a layout manager.
I also have 2 panels on this JFrame, at CENTER and SOUTH position.
The panel on CENTER position is changed dynamically on some user actions (actually when user press “Next” on the bottom panel).

The code is follwing:

private void switchToPanelByState(State state)
{
    this.getContentPane().removeAll();

    JPanel panel = _panels.get(state);
    this.getContentPane().add(panel, BorderLayout.CENTER);

    this.getContentPane().add(_controlPanel, BorderLayout.SOUTH);
    this.pack();
}

The problem is that after a few changes of panels the parts of “old” (from previous panels) GUI elements are appears on the current panel.

And also from time to time i see the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.JComponent._paintImmediately(Unknown Source)
        at javax.swing.JComponent.paintImmediately(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.JComponent._paintImmediately(Unknown Source)
        at javax.swing.JComponent.paintImmediately(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

However I don’t know if it’s related to the problem.

What is the possible source of these problems?
Any advise would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:50:20+00:00Added an answer on May 27, 2026 at 12:50 am

    BorderLayout accepts only one JComponent in the central Area, then there is no reason to remove()/ removeAll(); your code should be as shown below:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class MyFrame extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        public MyFrame() {
            final JPanel parentPanel = new JPanel();
            parentPanel.setLayout(new BorderLayout(10, 10));
            JButton myButton = new JButton("Add Component ");
            myButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    JPanel childPanel1 = new JPanel();
                    childPanel1.setBackground(Color.red);
                    childPanel1.setPreferredSize(new Dimension(300, 40));
                    JPanel childPanel2 = new JPanel();
                    childPanel2.setBackground(Color.blue);
                    childPanel2.setPreferredSize(new Dimension(800, 600));
    
                    parentPanel.add(childPanel1, BorderLayout.NORTH);
                    parentPanel.add(childPanel2, BorderLayout.CENTER);
    
                    parentPanel.revalidate();
                    parentPanel.repaint();
    
                    pack();
                }
            });
            setTitle("My Empty Frame");
            setLocation(10, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            parentPanel.add(myButton, BorderLayout.SOUTH);
            add(parentPanel);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MyFrame myFrame = new MyFrame();
                }
            });
        }
    }
    

    or directly

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class MyFrame extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JPanel childPanel1 = new JPanel();
        private JPanel childPanel2 = new JPanel();
        private JPanel childPanel3 = new JPanel();
    
        public MyFrame() {
            childPanel1.setBackground(Color.red);
            childPanel1.setPreferredSize(new Dimension(300, 40));
            childPanel2.setBackground(Color.blue);
            childPanel2.setPreferredSize(new Dimension(300, 40));
            childPanel3.setBackground(Color.yellow);
            childPanel3.setPreferredSize(new Dimension(300, 40));
    
            JButton myButton = new JButton("Add Component ");
            myButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    add(childPanel2, BorderLayout.CENTER);
                    pack();
                }
            });
    
            setTitle("My Empty Frame");
            setLocation(10, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            add(childPanel3, BorderLayout.CENTER);
            add(myButton, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MyFrame myFrame = new MyFrame();
                }
            });
        }
    } 
    

    2) nobody know what’s parameter state is, then without SSCCE isn’t possible answering this question

    3) I can simulating RepaintManager’s Exceptions, when CustomPainting is faster than latency from Native OS

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame with a CardLayout set as its layout manager. This has
I have this Java JFrame class, in which I want to use a boxlayout,
I have jFrame class MainForm which contains main() , placePanel(panel) and drop down menu.
I have a closeWindow() method which uses dispose() for the current JFrame to close
There is a panel in a JFrame called a WatchPanel extends JPanel which uses
I have a JFrame . I also have a Box class which extends Component
I have written a java applet which opens a JFrame (so when run in
I have a JFrame. This JFrame contains a JButton. I click the JButton and
I have a JFrame with null layout and two one character JLabels on the
This one's a tough one - I have a JFrame that generates JTextFields. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.