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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:47:08+00:00 2026-05-23T11:47:08+00:00

I have a custom GUI compomemt, which is based on Swing’s JPanel. This component

  • 0

I have a custom GUI compomemt, which is based on Swing’s JPanel. This component is placed in a JFrame, that uses BorderLayout. When I resize the frame, this component keeps resizing. How can I avoid this? I would like the component to keep the same size whatever happens. I’ve tried setSize, setPreferredSize, setMinimumSize with no success.

Thanks in advance!

M

  • 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-23T11:47:08+00:00Added an answer on May 23, 2026 at 11:47 am

    You have a few options:

    • Nest the component in an inner panel with a LayoutManager that does not resize your component

    • Use a more sophisticated LayoutManager than BorderLayout. Seems to me like GridBagLayout would suit your needs better here.

    Example of the first solution:

    import java.awt.*;
    import javax.swing.*;
    
    public class FrameTestBase extends JFrame {
    
        public static void main(String args[]) {
            FrameTestBase t = new FrameTestBase();
    
            JPanel mainPanel = new JPanel(new BorderLayout());
    
            // Create some component
            JLabel l = new JLabel("hello world");
            l.setOpaque(true);
            l.setBackground(Color.RED);
    
            JPanel extraPanel = new JPanel(new FlowLayout());
            l.setPreferredSize(new Dimension(100, 100));
            extraPanel.setBackground(Color.GREEN);
    
            // Instead of adding l to the mainPanel (BorderLayout),
            // add it to the extra panel
            extraPanel.add(l);
    
            // Now add the extra panel instead of l
            mainPanel.add(extraPanel, BorderLayout.CENTER);
    
            t.setContentPane(mainPanel);
    
            t.setDefaultCloseOperation(EXIT_ON_CLOSE);
            t.setSize(400, 200);
            t.setVisible(true);
        }
    }
    

    Result:

    enter image description here

    Green component placed in BorderLayout.CENTER, red component maintains preferred size.

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

Sidebar

Related Questions

I have a test that exercises a custom Swing component using java.awt.Robot. I'd like
I have a few models that need to have custom find conditions placed on
GUI development with Swing. I have a custom dialog for choosing a file to
I have a custom CLI debugger for which I'm interested in a GUI. The
I have custom classes that I currently instantiate within App.xaml as resources. I want
hi i would like to create a custom calendar, this calendar will have custom
Inside my XAML I have two custom components: <gui:CustomerBrowser x:Name=browser CustomerDetailView=??? VerticalAlignment=Stretch HorizontalAlignment=Stretch />
I have a few complex GUI elements (like a custom calendar with many days
We've got a WinForms app written in C# that has a very custom GUI.
I have a custom object that implements INotifyPropertyChanged. I have a collection of these

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.