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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:28:05+00:00 2026-06-06T22:28:05+00:00

I have a program which creates JButtons which are then added to a JPanel

  • 0

I have a program which creates JButtons which are then added to a JPanel with a BoxLayout which is set to place them vertically. Occasionally the first button is intentionally removed from the JPanel. Initially the Buttons are centered correctly and the Buttons are removing successfully as well. The problem is that the remaining buttons then move apart to fill the space. This is not what I would like, instead, I would like them to re-center along the y-axis without moving apart.

I have a Class which extends JPanel. In the constructor the BoxLayout is created.

setPreferredSize(new Dimension(150, 500));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setAlignmentY(CENTER_ALIGNMENT);

Creating the buttons is currently a method in this class:

createButtons(int numButtons){
for (int i=0;i<numButtons;i++) {
    add(new JButton());
}

And removal is another method:

removeButton(){
    if(getComponentCount()>1){
        remove(0);
        validate();
        repaint();
    }
}

Does anybody know how the make the Buttons remain centered along the y-axis without spreading apart to fill the containing panel?

  • 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-06T22:28:06+00:00Added an answer on June 6, 2026 at 10:28 pm

    Have you heard of glue in BoxLayouts which uses a invisble component as a filler(glue), which I think should help to keep the buttons centered, see these 2 links: BoxLayout docs and BoxLayout – Filler this site too has some great tutorial on Boxlayout with glue: BoxLayout Glue and this

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

Sidebar

Related Questions

I have a program that streams media, which creates two huge MemoryStreams, and then
I have a program which prints a multi-page document. The first page is pre-printed
I have a program which creates DialogBox window when user clicks the menu item
I have a program which creates some files in the application's Documents directory. I
So I have a program which creates a child process and executes a command
I have developed a program (C#) which creates a SQL database using this code:
I have a library which creates objects (instances of class A) and pass them
I'm currently have a program which creates a list of all MAC addresses and
I have a main program which creates a collection of N child threads to
I have a program which creates certain save files during its use. Technically they

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.