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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:08:19+00:00 2026-06-13T08:08:19+00:00

I have problems with understanding the behavior of my application. I want to create

  • 0

I have problems with understanding the behavior of my application. I want to create a simple window (1000x700px), divided into two parts (250px and 750px width respectively). I tried the following code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example extends JFrame
{
    private static final long serialVersionUID = 1L;

    public Example()
    {
        this.setSize(1000, 700);
        this.setTitle("Example");
        this.setResizable(false);
        this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

        JPanel navigation_panel_wrap = new JPanel();
        JPanel content_panel_wrap = new JPanel();
        navigation_panel_wrap.setPreferredSize(new Dimension(250, 700));
        content_panel_wrap.setPreferredSize(new Dimension(750, 700));
        content_panel_wrap.setBackground(Color.green);
        navigation_panel_wrap.setBackground(Color.red);
        this.getContentPane().add(navigation_panel_wrap);
        this.getContentPane().add(content_panel_wrap);
    }

    public static void main(String[] args)
    {
        Example example = new Example();
        example.setVisible(true);
    }
}

As you can see I manually set layout manager for JFrame (FlowLayout instead of BorderLayout with zero horizontal and vertical gaps). Of course, I can just use BorderLayout and than use add() method with BorderLayout.EAST and BorderLayout.WEST parameters, but I want to understand what’s wrong with FlowLayout.
When I run my application, I get the following (no green JPanel): enter image description here

If I decrease width of, for example, content_panel_wrap and make it 744px instead of 750px, everything works correctly.
enter image description here
So the question is – what are these strange 6 pixels? I’m not sure this value is constant for all operating systems, so I want to understand its origin.

  • 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-13T08:08:20+00:00Added an answer on June 13, 2026 at 8:08 am

    As for your codes problem (+1 to @Reimeus) calling pack() is the solution.
    as per docs:

    Causes this Window to be sized to fit the preferred size and layouts
    of its subcomponents. If the window and/or its owner are not yet
    displayable, both are made displayable before calculating the
    preferred size. The Window will be validated after the preferredSize
    is calculated.

    Tips:

    • Dont extend JFrame unnecessarily.
    • Use Event Dispatch Thread when creating and changing UI components:

      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            // create UI components etc here
          }
        });
      
    • Dont call setPreferredSize() rather override getPrefferedSize() of component.
    • Dont call setSize(...) on JFrame rather call JFrame#pack() before setting it visible.
    • Dont forget to call JFrame#defaultCloseOperation(..) or your initial/EDT thread will not be terminated when JFrame is closed.

    Here is an example combining my advice and your code:

    enter image description here

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Example {
    
        private final JFrame frame;
    
        public Example() {
            frame = new JFrame();
            frame.setTitle("Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//app exited when frame closes
            frame.setResizable(false);
            frame.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    
            JPanel navigation_panel_wrap = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(250, 700);
                }
            };
            JPanel content_panel_wrap = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(750, 700);
                }
            };
    
            content_panel_wrap.setBackground(Color.green);
            navigation_panel_wrap.setBackground(Color.red);
    
            frame.add(navigation_panel_wrap);
            frame.add(content_panel_wrap);
            //pack frame (size components to preferred size)
            frame.pack();
            frame.setVisible(true);//make frame visible
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Example();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems with understanding how jQuery create headers for ajax post. I have
I am new to JPA and have some problems in understanding bidirectional relations. I
I'm having problems understanding how latest.integration works. I have an example that is not
I am relatively new to C++ and am having problems understanding struct. I have
I have ran into a bit of a problem with my understanding of the
i have some problems on understanding gdb. i have a main function, i wrote
I have some problems understanding a way to get the correct result from my
I have some problems understanding how the 'repair button' works in burn. If my
I have some understanding problems regarding the stringstream . Example: stringstream stream(commands); while (true)
We have problems with deadlock situations in our application. I have read a lot

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.