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

  • Home
  • SEARCH
  • 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 8759735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:52:42+00:00 2026-06-13T14:52:42+00:00

I unfortunately have to use multiple windows in this program and I don’t think

  • 0

I unfortunately have to use multiple windows in this program and I don’t think CardLayout is going to work because I can’t have any buttons constant between the different layouts. So I’m trying to code a button to hide the present JPanel (thePanel) and show a new one (thePlacebo).

I’m trying to hide thePanel in an ActionListener like this:

frame.getContentPane().remove(thePanel);

I thought this would work, but it just freezes my program as soon as I hit the button.

Here’s a chunk of the code for context:

public class Reflexology1 extends JFrame{
JButton button1, button2;
JButton movingButton;
JTextArea textArea1;
int buttonAClicked, buttonDClicked;
private long _openTime = 0;
private long _closeTime = 0;
JPanel thePanel = new JPanel();
JPanel thePlacebo = new JPanel();
final JFrame frame = new JFrame("Reflexology");

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

public Reflexology1(){


    frame.setSize(600, 475);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Reflexology 1.0");
    frame.setResizable(false);


    button1 = new JButton("Accept");
    button2 = new JButton("Decline");
    movingButton = new JButton("Click Me");

    ListenForAcceptButton lForAButton = new ListenForAcceptButton();
    ListenForDeclineButton lForDButton = new ListenForDeclineButton();
    button1.addActionListener(lForAButton);
    button2.addActionListener(lForDButton);
    //movingButton.addActionListener(lForMButton);

    JTextArea textArea1 = new JTextArea(24, 50);

    textArea1.setText("Tracking Events\n");
    textArea1.setLineWrap(true);
    textArea1.setWrapStyleWord(true);
    textArea1.setSize(15, 50);

    FileReader reader = null;
    try {
        reader = new FileReader("EULA.txt");
        textArea1.read(reader, "EULA.txt");
    } catch (IOException exception) {
        System.err.println("Problem loading file");
        exception.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException exception) {
                System.err.println("Error closing reader");
                exception.printStackTrace();
            }
        }
    }

    JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    AdjustmentListener listener = new MyAdjustmentListener();

    thePanel.add(scrollBar1);
    thePanel.add(button1);
    thePanel.add(button2);
    thePlacebo.add(movingButton);

    frame.add(thePanel);

    ListenForWindow lForWindow = new ListenForWindow();
    frame.addWindowListener(lForWindow);
    frame.setVisible(true);

}
// Implement listeners

private class ListenForAcceptButton implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == button1){
            Calendar ClCDateTime = Calendar.getInstance();
            System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
            _closeTime = ClCDateTime.getTimeInMillis() - _openTime;
            frame.getContentPane().remove(thePanel);
        }
    }
}

Does anybody know what I might be doing wrong?

  • 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-06-13T14:52:43+00:00Added an answer on June 13, 2026 at 2:52 pm

    After removing components from a container, it goes into the invalidate state. To bring it back to the valid state you have to revalidate and repaint that. In your case you are directly adding/removing components from JFrame so depending on the Java version you can do this :

    frame.revalidate(); // For Java 1.7 or above
    frame.getContentPane().validate(); // For Java 1.6 or below
    frame.repaint();
    

    Here is one working example for your help :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Assignment
    {
        private JFrame frame;
        private JPanel firstPanel;
        private JPanel secondPanel;
    
        private JButton forwardButton;
        private JButton backButton;
    
        private void displayGUI()
        {
            frame = new JFrame("Assignment");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            firstPanel = new JPanel();
            firstPanel.setOpaque(true);
            firstPanel.setBackground(Color.BLUE);
    
            secondPanel = new JPanel();
            secondPanel.setOpaque(true);
            secondPanel.setBackground(Color.RED);
    
            forwardButton = new JButton("Forward");
            forwardButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae)
                {
                    frame.remove(firstPanel);
                    frame.add(secondPanel);
                    frame.revalidate(); // For Java 1.7 or above.
                    // frame.getContentPane().validate(); // For Java 1.6 or below.
                    frame.repaint();
                }
            });
    
            backButton = new JButton("Back");
            backButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae)
                {
                    frame.remove(secondPanel);
                    frame.add(firstPanel);
                    frame.revalidate(); // For Java 1.7 or above.
                    // frame.getContentPane().validate(); // For Java 1.6 or below.
                    frame.repaint();
                }
            });
    
            firstPanel.add(forwardButton);
            secondPanel.add(backButton);
    
            frame.add(firstPanel);
            frame.setSize(300, 300);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new Assignment().displayGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to use monotouch, but unfortunately I don't have $400 to spend. So
I have a dot net windows service project which contains multiple services. This has
Unfortunately I have to use Windows Server 2003 on my 32 bit workstation due
Unfortunately, I have to use a C library with internal state in my Android
I'm trying to use eclipse encoding UTF-8, for polish characters. Unfortunately, I still have
I see that this has been asked many times. But, unfortunately I have not
Unfortunately I don't have access to JQuery and all it's nicety. But I do
I have a very easy question. Unfortunately, I can't find the answer. I just
I have a few questions regarding the Windows Azure Table Storage. Unfortunately my trial
In Windows, suppose you have multiple windows (HWNDs) of the same window class open.

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.