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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:48:09+00:00 2026-05-29T16:48:09+00:00

I have an application that has multiple panels; I would like to have the

  • 0

I have an application that has multiple panels; I would like to have the freedom to use different layout managers for the different panels, but would like them to behave similarly as the window is resized by the user.

    package example;

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;

    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;

    public class TP1 extends JFrame
    {
        public static void main(String[] args)
        {
            TP1 tp1 = new TP1();
            tp1.go();
        }

        public void go()
        {
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            // create a panel with some labels on it
            JPanel innerFirst = new JPanel();
            innerFirst.setLayout(new BoxLayout(innerFirst, BoxLayout.PAGE_AXIS));
            innerFirst.add(new JLabel("one"));
            innerFirst.add(new JLabel("two"));
            innerFirst.add(new JLabel("three"));
            innerFirst.add(new JLabel("four"));

            // put that panel in a scroll pane
            JScrollPane firstSP = new JScrollPane(innerFirst);

            // make another panel and put our scrolled panel in it
            JPanel outerFirst = new JPanel(); 
            outerFirst.setLayout(new BoxLayout(outerFirst, BoxLayout.PAGE_AXIS));
            outerFirst.add(firstSP); 

            // create a GridBagLayout panel with some text fields on it
            JPanel innerSecond = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = .25;
            gbc.anchor = GridBagConstraints.LINE_START;
            innerSecond.add(new JTextField(8), gbc);
            gbc.gridx = 0;
            gbc.gridy = 1;
            innerSecond.add(new JTextField(10), gbc);
            gbc.gridx =0;
            gbc.gridy = 2;
            innerSecond.add(new JTextField(12), gbc);

            // put that panel in a scroll pane
            JScrollPane secondSP = new JScrollPane(innerSecond);

            // make another panel and put our second scrolled panel in it
            JPanel outerSecond = new JPanel(); 
            outerSecond.setLayout(new BoxLayout(outerSecond, BoxLayout.LINE_AXIS));
            outerSecond.add(secondSP); 

            JPanel innerThird = new JPanel(new GridBagLayout());
            GridBagConstraints gbc3 = new GridBagConstraints();
            gbc3.anchor = GridBagConstraints.LINE_END;
            gbc.weightx = .25;
            gbc3.gridx = 0;
            gbc3.gridy = 0;
            innerThird.add(new JLabel("1st label"), gbc3);
            gbc3.gridy = 1;
            innerThird.add(new JLabel("second label"), gbc3);
            gbc3.gridy = 2;
            innerThird.add(new JLabel("IIIrd label"), gbc3);

            gbc3.anchor = GridBagConstraints.LINE_START;
            gbc3.gridx = 1;
            gbc3.gridy = 0;
            innerThird.add(new JTextField(8), gbc3);
            gbc3.gridy = 1;
            innerThird.add(new JTextField(12), gbc3);
            gbc3.gridy = 2;
            innerThird.add(new JTextField(14), gbc3);

            JScrollPane thirdSP = new JScrollPane(innerThird);
            JPanel outerThird = new JPanel();
            outerThird.setLayout(new BoxLayout(outerThird, BoxLayout.LINE_AXIS));
            outerThird.add(thirdSP);

            // put the scrolled panes onto a tabbed pane
            JTabbedPane tp = new JTabbedPane();
            tp.add("text fields", outerSecond);
            tp.add("labels", outerFirst);
            tp.add("mixed", outerThird);

            // add the tabbed pane to the frame
            this.add(tp);

            // pack it and ship it.
            pack();
            setVisible(true);
        }
    }

Running the above code, we get a window with a tabbed pane in it with three tabs. If we make the window smaller, all of them get scroll bars, as intended. If we make it larger, the three behave differently: the tab with labels only leaves them at the top left of the window, the tab with fields only centers them vertically on the left edge, and the one with the gridbag of mixed labels and fields centers them both horizontally and vertically in the enlarged window.

This is not acceptable for the application; I need to somehow make all the panels behave similarly in this way. I need them all to have scroll bars, and I would like them all to keep to the upper left if the window is made larger than the internal panel.

One other requirement: my tabs are occupied by something that extends JPanel, I’ve been told before I can put JScrollPane directly into the tab, but for my application I don’t want to do that either. It just makes other things more complicated than they need to be.

In addition to wanting all the extra space to be put at the bottom and the right, I would dearly love to understand WHY these three situations behave differently. I still believe that we would all be better off if we understood the principles behind what we’re doing, instead of just copying examples right and left and doing things by trial and error until they work.

(Incidentally, I have a GroupLayout panel that does seem to gravitate to the upper left, but didn’t think it was necessary for my question and this is 100 lines of code as it is.)

  • 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-29T16:48:17+00:00Added an answer on May 29, 2026 at 4:48 pm

    Seems like in order to understand why is this happening in your code, you need to understand certain terms. For example, PAGE_AXIS, LINE_AXIS, LINE_END, LINE_START and so on. Since you are providing them as constraints, these are the things that describe the orientation of the components being added to the container and their starting point, like as you writing :

    innerFirst.setLayout(new BoxLayout(innerFirst, BoxLayout.PAGE_AXIS));
    

    Here you telling your BoxLayout to start adding components from the point which refers to the start of the page. (When you start your Notepad, your cursor is placed at the PAGE_AXIS on the new document). But when you are writing this :

    JPanel innerSecond = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = .25;
    gbc.anchor = GridBagConstraints.LINE_START;
    

    Here the term anchor is used when the component is smaller than its display area. It determines where, within the display area, to place the component. But here since you mentioned that it has the value LINE_START which means :

    Place the component centered along the edge of its display area where lines of text 
    would normally begin for the current ComponentOrientation. Equal to WEST for horizontal,
    left-to-right orientations and EAST for horizontal, right-to-left orientations.
    

    That’s why the three JTextFields you created, you see them at the center on the left side.

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

Sidebar

Related Questions

I have an application that has multiple releases for different clients. For each client
I have an application that has multiple states, with each state responding to input
Hi Sitepoint wizard people, Say we have an admin application that has multiple users
In an application that has multiple webviews, is there any way to have the
I have a TreeView control in my WinForms .NET application that has multiple levels
I have an application that has Powershell 1 embedded into it, but we need
I have an application that has many different types of objects that each persist
I have an application that has several objects (about 50 so far, but growing).
I have to build an application using Maven for PHP that has multiple modules.
I have an application that has multiple threads processing work from a todo queue.

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.