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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:46:07+00:00 2026-06-02T23:46:07+00:00

I have a JPanel and for example, if I click on the button INSERT,

  • 0

I have a JPanel and for example, if I click on the button “INSERT”, I can add a JButton and a JLabel. My problem is I need to insert the JLabel under the JButton. The JLabel text must centred respect the JButton text. After that, I want a space around 10 pixels to use again my “INSERT” button and add horizontally a new pair on JButton and JLabel with the same orientation.

Thanks!

PD: Please, complement your question with an attempt.

  • 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-02T23:46:09+00:00Added an answer on June 2, 2026 at 11:46 pm

    Here is a quick example that shows a dynamic (which is what I assume you wanted) setup to allow insertion of an undefined number of panels:

    public class AwesomeAnswer {
    
      public static void main(String[] args) {
         // please not that this is only an example and not a 
         // Swing thread safe way of starting a JFrame
         JFrame frame = new JFrame();
    
         JPanel content = (JPanel)frame.getContentPane();
         // create our top panel that will hold all of the inserted panels
         JPanel page = new JPanel();
         page.setLayout( new BoxLayout( page, BoxLayout.Y_AXIS ) );
         // add our page to the frame content pane
         content.add( page );
         // add two button/label panels
         page.add( insert( "This is an awesome answer", "Accept" ) );
         page.add( insert( "Say thank you", "Thank" ) );
    
    
         frame.pack();
         frame.setVisible( true );
      }
    
      public static final JPanel insert( String labelText, String buttonText ) {
         // create the label and the button
         JLabel lbl = new JLabel( labelText );
         JButton btn = new JButton( buttonText );
         // create the panel that will hold the label and the button
         JPanel wrapPanel = new JPanel( new GridBagLayout() );
         wrapPanel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
         // tell the grid bag how to behave
         GridBagConstraints gbc = new GridBagConstraints();
         gbc.gridwidth = 0;
         gbc.gridheight = 2;
    
         // make the button centered
         JPanel buttonPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
         buttonPanel.add( btn );
    
         // make the label centered
         JPanel labelPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
         labelPanel.add( lbl );
    
         // add our button and label to the grid bag with our constraints
         wrapPanel.add( buttonPanel, gbc );
         wrapPanel.add( labelPanel, gbc );
    
         return wrapPanel;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JPanel to which when a button is pressed I want to
For example, suppose I have a drawing in a JPanel subclass with JScrollPanes, and
I have a ScrollPane with a JPanel in it and I add about 200
How can I sort things by priority in a JList? For example I have
I have a problem implementing this example in java, tableFilterDemo is a class that
In the example below I have a JTable a JList and two JButton s
In the example below I have a JPanel with BoxLayout which contains another JPanel
I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane.
I have a JPanel, which I would like to detect the following events (1)

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.