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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:46:31+00:00 2026-05-26T05:46:31+00:00

I need to reinstantiate a JPanel in an application I’m writing (basically it’s an

  • 0

I need to reinstantiate a JPanel in an application I’m writing (basically it’s an input window where the user can build a graph, and I’d like the user to be able to empty it, so using CardLayout is not an option), and I was thinking of using something like this:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AppletProve extends JPanel implements MouseListener, MouseMotionListener{

private JPanel p1;
public AppletProve(){
    super();
    setLayout(null);
    Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
    dim.width=(int) (dim.width*0.66);
    dim.height=(int) (dim.height*0.75);
    setPreferredSize(dim);
    addMouseListener(this);
}
public void crea(){
    p1=new JPanel();
    p1.setBounds(10,10,60,70);
    p1.setBackground(Color.YELLOW);
    JLabel lbl1=new JLabel("prova1");
    p1.add(lbl1);
    add(p1);
}
public void collega(){
    remove(p1);
    p1=new JPanel();
    p1.setBounds(10,10,60,70);
    p1.setBackground(Color.BLUE);
    JLabel lbl2=new JLabel("prova2");
    p1.add(lbl2);
    add(p1);
}
@Override
public void mouseClicked(MouseEvent arg0) {
    System.out.println("chiamato");
    collega();

}
@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseDragged(MouseEvent arg0) {
    repaint();

}
@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
}

The problem is that while the panel p1 is reinstantiated, since the background color changes, lbl2 isn’t shown, which wouldn’t be that bad if I only had to reinstantiate the panel to a blank state, but I was thinking of using this method also for adding responsibilities to the panel through the decorator pattern, so I need to show the components of the new reinstatiated panel.

  • 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-26T05:46:31+00:00Added an answer on May 26, 2026 at 5:46 am

    I’d like the user to be able to empty it, so using CardLayout is not an option

    Sure it is. See, CardLayout.removeLayoutComponent(Component).

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

Sidebar

Related Questions

Need of time server services I am trying to build a web application in
Need a Math Editor to integrate to my application written on C# to be
need to open an excel sheet and then go an to cell range like
Need help with an error message that I just can't figure out. I am
I'm currently working with a part of my application that uses Dynamic Web User
Need suggestion for migration few modules of asp.net mvc 3 application: Right now we
I have 3 user controls, each one of which will need to pull data
Need some help. I have a table with some columns..like name , phone etc...
Need to create a customized membership with database and web application in ASP.net mvc.
need to return an array like this for example: array(30 => 'Mercedes Benz 310

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.