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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:26:33+00:00 2026-06-15T07:26:33+00:00

I am struggling with java GUI – thanks for any help in advance! I

  • 0

I am struggling with java GUI – thanks for any help in advance! I have a JFrame in which I have several components: button (Jbutton) that triggers action listener, comp which is a component Im trying to replace a JScrollPane with a component in it (it doesnt matter what type of component, could be text field, table or anything).

I would like to trigger an action – delete the component, place a new one on the same place as the deleted one and repaint the window (I am using this to show different types of text fields and JTables). This is what I have:

JScrollPane sp = new JScrollPane(comp);
this.add(sp, BorderLayout.CENTER);
//this works so far - first time I display this is ok!

private void replace() {
 comp = new Component(...); //name and type of the components is not important
 sp = new JSCrollPane(comp);
 this.remove(sp); //remove old component
 add(sp, BorderLayout.CENTER);
 repaint();
 revalidate();
}

Why doesnt function replace work? It doesnt do anything (it changes the component in logic so if I access the content of comp, it is refreshed but it still shows the old one).

I wrote it kinda symbolic, cause my code is very long… Thanks for any help!
edit: forgot one line in my code..

  • 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-15T07:26:34+00:00Added an answer on June 15, 2026 at 7:26 am

    It’s not necessary for you to try to remove the scroll pane as you did.

    To change the component shown by the scroll pane simply make this call:

    sp.setViewportView(new Component(...));
    

    after that call, the old component is removed from view and replaced by the new component.

    So your code should look somewhat like this:

    JScrollPane sp = new JScrollPane(comp);
    this.add(sp, BorderLayout.CENTER);
    
    private void replace() {
        comp = new Component(...); //name and type of the components is not important
        sp.setViewportView(comp);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am struggling ( ! ) with java GUI development. All these small things
I'm new to Java EE and have been struggling with some basic middleware concepts
Struggling with styling the mouse over for a button ... I have managed to
I have written a Java Agent along with an SWT GUI for controlling the
For a while I have been struggling to integrate scala with java methods that
I am struggling to find a plugin which would allow me to use Java
import java.awt.Color; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyDrawPanel extends
I'm new to Java Swing and I have been struggling to start the GridBagLayout
I have been struggling with a nasty ANR problem with my app, which loads
I have been struggling with an issue involving a Processing sketch which I want

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.