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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:32:00+00:00 2026-06-05T05:32:00+00:00

I’m creating an (j)applet where the user has access to several different views selectable

  • 0

I’m creating an (j)applet where the user has access to several different views selectable from a tree. The applet has a main area where the current view is displayed inside a scrollpane, replacing whatever was previously there. Most of the time this works fine, but when the view to be replaced has a more complicated layout, as in large (~100 components), not nested, the removal takes several seconds! Creation of the large view is lightning quick, only the removal is slow. This is only noticeable in applets, running the application in a JFrame is still as fast as I expect. I use GridBagLayout for the view.

After some experimentation, I found that if I replace

contents.remove(view); //contents is the view of the scrollpane.

with

view.removeAll();
contents.remove(view);

then the whole process is very fast.

My guess is that the cause of this behavior is some of Java’s layout “magic”, and although I have a solution it doesn’t feel right and seems to indicate that I don’t fully understand what’s going on. So my question(s) is this:
Does someone know what’s actually happening? Is there a better (clearer) way to accomplish the same thing?

Edit: SSCCE: http://pastebin.com/VC1T7zGv

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class SSCCE extends JApplet {
    private boolean state;

    public void init() {
        super.init();
        getContentPane().add(setupSwitchPanel(), BorderLayout.NORTH);
        getContentPane().add(setupSimplePanel(), BorderLayout.CENTER);
    }

    private JPanel setupSwitchPanel() {
        JPanel switchPanel = new JPanel();
        switchPanel.add(new JButton(new AbstractAction("Switch panels (slow)") {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                switchComponents();
            }
        }));

        switchPanel.add(new JButton(new AbstractAction("Switch panels (fast)") {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                ((JPanel)getContentPane().getComponent(1)).removeAll();
                switchComponents();
            }
        }));
        return switchPanel;
    }

    private void switchComponents() {
        getContentPane().remove(1);
        if (state)
            getContentPane().add(setupSimplePanel(), BorderLayout.CENTER);
        else
            getContentPane().add(setupComplexPanel(), BorderLayout.CENTER);
        state = !state;
        getContentPane().validate();
    }

    private JPanel setupComplexPanel() {
        JPanel contents = new JPanel();
        contents.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        for (int x = 0; x<10; x++) {
            c.gridx = x;
            for (int y = 0; y<10; y++) {
                c.gridy = y;
                contents.add(new JLabel(x + ", " + y), c);
            }
        }
        return contents;
    }

    private JPanel setupSimplePanel() {
        JPanel contents = new JPanel();
        contents.add(new JLabel("Simple view"));
        return contents;
    }
}
  • 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-05T05:32:01+00:00Added an answer on June 5, 2026 at 5:32 am

    Alternate strategy: Use a CardLayout.

    I would prefer to not keep all of the different views in memory.

    Why? The typical memory could allow the storage of hundreds of cards with hundreds of components.

    Create them lazily and the GUI might only have 20-40 cards in a typical usage run. Of course, re-use existing cards for common tasks, just change the content.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters

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.