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 7082445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:03:19+00:00 2026-05-28T07:03:19+00:00

I have an application that allows users to select an option and based on

  • 0

I have an application that allows users to select an option and based on the user selection a JPanel is removed from the Component, the new JPanel is added and the component is revalidated

see code:

          if (c != null) {
                contentPane.remove(c);
            }
            c = new AddBookInterface(theLibrary);
            contentPane.add(c);
            contentPane.revalidate();
            break;

c is a Component

I have several JPanels that the user can switch between and the switch works properly. However, when I add this JPanel upon user selection the JPanels that are added afterward do not load properly. What is causing this?

   public class RemoveBookInterface extends JPanel {

private Library theLibrary;

public RemoveBookInterface(Library theLibrary) {
    this.theLibrary = theLibrary;
    setSize(400, 400);
    setLayout(new BorderLayout());
    setVisible(true);
    removeBook(theLibrary);
}

public void removeBook(Library theLibrary) {
    // prompt user for book id of book to remove
    Long ID = Long
            .parseLong(JOptionPane
                    .showInputDialog("Enter the library book ID for the book you want to remove"));
    try {
        // get library book info and store it to display in message
        LibraryBook lb = theLibrary.getInventory().findLibraryBook(ID);
        // remove book
        theLibrary.removeBook(ID);
        // display message indicating book was removed
        JOptionPane
                .showMessageDialog(
                        null,
                        "The following library book was removed:\n"
                                + lb.toString());
    } catch (Exception e1) {
        JOptionPane.showMessageDialog(null, e1.getMessage());

    }
}

}

  • 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-28T07:03:20+00:00Added an answer on May 28, 2026 at 7:03 am

    Better approach is to shift to CardLayout. but if you want to stick to your approach, then try to add this after your line

    if (c != null) {
                contentPane.remove(c);
            }
            c = new AddBookInterface(theLibrary);
            contentPane.add(c);
            contentPane.revalidate();
            contentPane.repaint(); 
            frame.validate();
            frame.repaint();
            break;
    

    Or you might have forgotten to schedule a job for your event dispatcher thread.
    A sample program to help your cause :

    import java.awt.event.*;
    import javax.swing.*;
    
    public class TwoPanelTest implements ActionListener
    {
        private JFrame frame;
    
        private JPanel panel1;
        private JPanel panel2;
    
        private JButton button1;
        private JButton button2;
    
        private JLabel label1;
        private JLabel label2;
    
        private JTextField tfield1;
        private JTextField tfield2;
    
        public TwoPanelTest()
        {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            panel1 = new JPanel();
            panel2 = new JPanel();
    
            label1 = new JLabel("This is Label 1");
            label2 = new JLabel("This is Label 2");
    
            button1 = new JButton("BUTTON 1");
            button2 = new JButton("BUTTON 2");
            button1.addActionListener(this);
            button2.addActionListener(this);
    
            tfield1 = new JTextField(20);
            tfield2 = new JTextField(20);
    
            panel1.add(label1);
            panel1.add(button1);
            panel1.add(tfield1);
    
            panel2.add(label2);
            panel2.add(button2);
            panel2.add(tfield2);
    
            tfield1.setText("MY TEXT WILL CHANGE.");
            frame.setContentPane(panel1);
            frame.pack();
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent ae)
        {
            JButton button = (JButton)ae.getSource();
            if (button == button1)
            {
                frame.remove(panel1);
                frame.setContentPane(panel2);
                tfield2.setText("TEXTFIELD 2");
                frame.validate();
                frame.repaint();
            }
            else if (button == button2)
            {
                frame.remove(panel2);
                frame.setContentPane(panel1);
                tfield1.setText("TEXTFIELD 1");
                frame.validate();
                frame.repaint();
            }
        }
    
    
        public static void main(String[] args)
        {
                    // Here Event Dispatcher thread is responsible for 
                    // calling the function which creates and displays your GUI
                    // or it itself contains the code for creating and displaying
                    // the GUI, to remove hickups experienced while updating the 
                    // GUI on the run.
            SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        new TwoPanelTest();
                    }
                });
        }
    }
    

    Hope that might help in some sorts.

    Regards.

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

Sidebar

Related Questions

I'm new to Android development. I have an application that allows the user to
I have an application that allows users to write their own code in a
Let say, I have a web application that allows users to upload images and
We have created a web application, using ASP.NET, that allows users to upload documents
I have an application that allows the user to download a csv. This works
I have a web application that allows a user to search on some criteria,
I have a simple Desktop Facebook application that allows the user to retrieve some
I have an application that allows users to save events. I would like to
I have a simple C# application that allows users to specify that it should
We have an ASP .NET MVC application that allows users to provide a set

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.