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

  • Home
  • SEARCH
  • 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 6720287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:11:41+00:00 2026-05-26T09:11:41+00:00

+——————————————–+ | +——-+ +———-+| | | +—+ | | +—–+ || | | |

  • 0
    +--------------------------------------------+
    |                 +-------+      +----------+|
    |                 | +---+ |      |  +-----+ ||
    |                 | | A | |      |  |  B  | ||
    |                 | +---+ |      |  +-----+ ||
    |                 +-------+      +----------+|
    +--------------------------------------------+
                          ^
                          |
                          |
                        Center

Background: I have

  • a JButton (“A”), size 50×25, within a JPanel (FlowLayout.CENTER)
  • a JLabel (“B”), size 100×25, within a JPanel (FlowLayout.RIGHT)
  • the two JPanels are in a JPanel

Desired Result: I want

  • The JButton “A” to always be centered horizontally,
  • The JLabel “B” to always be flush right.

Thing I’ve tried: These didn’t work for me

  • BorderLayout is not working for me because JButton “A” is shifted LEFT:
  • I’d prefer not to put an invisible component WEST to undo the shift

    +--------------------------------------------+
    |            +-------+           +----------+|
    |            | +---+ |           |  +-----+ ||
    |            | | A | |           |  |  B  | ||
    |            | +---+ |           |  +-----+ ||
    |            +-------+           +----------+|
    +--------------------------------------------+
                     ^    ^
                     |    |
                     |    |
                     |  Center
                     |
                   Shifted Left
    
  • GridLayout won’t work because I don’t want the “A” and “B” to be expanded

Appreciate any suggestions!

p.s.

The JButton/JLabels are each inside of their own JPanels because WITHOUT the Jpanel, BorderLayout.CENTER expands the JButton across the entire width of the major panel (up to the left edge of the JLabel). The JPanels are not needed/critical to the statement of the problem

Conclusion:

  • I went with “Hovercraft Full Of Eels” answer posted below. Thanks!
  • 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-26T09:11:41+00:00Added an answer on May 26, 2026 at 9:11 am

    You should nest JPanels and use a combination of layouts. Placing the panels holding the JButtons into another JPanel that uses GridLayout(1, 0) (1 row, variable number of columns) could work, and placing that JPanel into the BorderLayout.NORTH position of a BorderLayout-using JPanel could work.

    For example

    import java.awt.*;
    import javax.swing.*;
    
    public class Foo003 {
    
       private static void createAndShowGui() {
          JButton btnA = new JButton("A");
          JButton btnB = new JButton("B");
    
          btnA.setPreferredSize(new Dimension(50, 25));
          btnB.setPreferredSize(new Dimension(100, 25));
    
          JPanel btnAPanel = new JPanel(); // uses default FlowLayout.CENTER
          JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
          btnAPanel.add(btnA);
          btnBPanel.add(btnB);
    
          JPanel topPanel = new JPanel(new GridLayout(1, 0));
          topPanel.add(new JLabel("")); // empty placeholder label
          topPanel.add(btnAPanel);
          topPanel.add(btnBPanel);
    
          JPanel mainPanel = new JPanel(new BorderLayout());
          mainPanel.add(topPanel, BorderLayout.NORTH);
    
          mainPanel.setPreferredSize(new Dimension(400, 300));
    
          JFrame frame = new JFrame("Foo003");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So to start, I have an array of XML files. These files need to
I have set of xml elements like this: <item code=<HTML_CODE> /> For example: <items>
I basically have the classic many to many model. A user, an award, and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We have different types of hyphens/dashes (in some text) populated in db. Before comparing
&mdash; or &#8212; Is there a difference between these? Is one better-supported than the
Any idea whats the difference between the two commands below? Command: manage.py runfcgi method=threaded
I'm using a DatePicker ( org.apache.wicket.extensions.yui.calendar.DatePicker — Javadoc ) in a form. The form
I have a fairly mundane piece jQuery that toggles a div's visibility when a
I need to stack two tables on top of each other with content before

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.