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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:26:44+00:00 2026-06-09T20:26:44+00:00

The code below shows my program that, when switches between methods home(), plus, minus(),

  • 0

The code below shows my program that, when switches between methods home(), plus, minus(), times() and divide(), they removeAll() of the corresponding JPanels , addPnl() subPnl() and so on – these being stores in a JDesktopPane.

I’m quite new to Java, and the idea behind my code is so that it is a maths program: you press a button, the JFrame removes the content from the JPanel that was displaying at that moment, then display the correct JPanel depending on what type of sum you want: addition, multiplication etc.

My issue is that, as you flip to the menu, then to another JPanel, and back, and forth, it becomes slower and slower to flip to the new screen/JPanel. Is there a way to avoid this?

There may be some bad practice in my code but, like I said, I’m fairly new and need to learn these things!

Many thanks.

I added JPanels to a JDesktopPane so I could have a background image on my JFrame:

desk.add( bgImg , new Integer( 50 ) );
desk.add( mainPnl , new Integer( 350 ) );
desk.add( mainG , new Integer( 350 ) );
desk.add( addPnl , new Integer( 350 ) );
desk.add( subPnl , new Integer( 350 ) );
desk.add( mulPnl , new Integer( 350 ) );
desk.add( divPnl , new Integer( 350 ) );
setLayeredPane( desk );

The different buttons link to different methods, all in my Class called MiniMain

public void actionPerformed( ActionEvent event ){

if( event.getSource() == addBtn ) { plus(); }
if( event.getSource() == subBtn ) { minus(); }
if( event.getSource() == mulBtn ) { times(); }
if( event.getSource() == divBtn ) { divide(); }
if( event.getSource() == menuBtn ) { home(); }
if( event.getSource() == noteBtn ) { MiniPad.pad(); return; }

Each of the methods start with some declarations that removeAll() of the content that are in JPanels: panels that would be linking to the method

public void plus(){
mainPnl.removeAll(); mainG.removeAll(); addPnl.removeAll();
  • 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-09T20:26:46+00:00Added an answer on June 9, 2026 at 8:26 pm

    Have you thought about a CardLayout, you can then create each different JPanel and simply flip between your JPanels, it is better practice then removing all components from the JPanel each time and adding new ones:

    //Where instance variables are declared:
    JPanel cards;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    
    //Where the components controlled by the CardLayout are initialized:
    //Create the "cards".
    JPanel card1 = new JPanel();
    ...
    JPanel card2 = new JPanel();
    ...
    
    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);
    cards.add(card2, TEXTPANEL);
    ...
    
    getContentPane().add(cards, BorderLayout.CENTER);//add card panel to frame
    setVisible(true);//show frame
    
    CardLayout cl = (CardLayout)(cards.getLayout());//get cards
    cl.show(cards, BUTTONPANEL);//switch cards to button JPanel
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below shows me (I think) that the for each loop is about
I have the code below that hides and shows the navigational bar. It is
The code below shows a line in different colors, but that's a lot of
The code below shows a small WinForms app which includes a simple Control that
The code below shows a sample that I've used recently to explain the different
I have a favorite C# program similar to the one below that shows that
The code shows below what i have done till now. Now what i need
The example below shows the code I am using to test whether a user
The code below compiled in Debug configuration in VS2005 SP1 shows two messages with
The code below extracts links from a web page and shows them in a

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.