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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:30:57+00:00 2026-05-24T04:30:57+00:00

I have a JPanel with a GridBagLayout inside of a JScrollPane. I also have

  • 0

I have a JPanel with a GridBagLayout inside of a JScrollPane. I also have an ‘add’ button within the JPanel which, when clicked, will be removed from the JPanel, adds a new instance of a separate component to the JPanel, then adds itself back to the JPanel. This sort of makes a growing list of components, followed by the ‘add’ button.

Adding new components works fine, the JPanel stretches to accommodate the new components, and the JScrollPane behaves as expected, allowing you to scroll through the entire length of the JPanel.

This is how the add works:

jPanel.remove(addButton);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIVE;
jPanel.add(new MyComponent(), c);
jPanel.add(addButton, c);
jPanel.validate();
jPanel.repaint();`

Removal works by clicking a button inside the added components themselves. They remove themselves from the JPanel just fine. However, the JPanel keeps it’s stretched-out size, re-centering the list of components.

This is how removal works:

Container parent = myComponent.getParent();
parent.remove(myComponent);
parent.validate();
parent.repaint();`

The question is, why does my GridBagLayout JPanel resize when adding components, but not when removing components?

  • 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-24T04:30:58+00:00Added an answer on May 24, 2026 at 4:30 am

    You have to revalidate and repaint the JScrollPane, here is an example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SwingTest {
    
        public static void main(String[] args) {
            final JPanel panel = new JPanel(new GridBagLayout());
    
            for (int i = 0; i < 25; i++) {
                JTextField field = new JTextField("Field " + i, 20);
    
                GridBagConstraints constraints = new GridBagConstraints();
                constraints.gridy = i;
    
                panel.add(field, constraints);
            }
    
            final JScrollPane scrollPane = new JScrollPane(panel);
    
            JButton removeButton = new JButton("Remove Field");
            removeButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (panel.getComponentCount() >= 1) {
                        panel.remove(panel.getComponentCount() - 1);
                        scrollPane.revalidate();
                        scrollPane.repaint();
                    }
                }
            });
    
    
            JFrame frame = new JFrame("Swing Test");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(640, 480);
            frame.setLocation(200, 200);
            frame.getContentPane().add(scrollPane);
            frame.getContentPane().add(removeButton, BorderLayout.SOUTH);
    
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane.
I have a JPanel to which I'd like to add JPEG and PNG images
I have a JPanel subclass on which I add buttons, labels, tables, etc. To
I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks
I have a JPanel, which I would like to detect the following events (1)
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JFrame which has 3 JPanels in GridBagLayout .. Now, when I
I have JPanel wrapped in JScrollPane and I want the rectangle to be drawn
I have a couple of Jlist within jscrollpanes in a gridbaglayout. The amount of
I have a simple problem with my JPanel / Gridbaglayout: import javax.swing.*; import java.awt.*;

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.