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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:21:14+00:00 2026-05-25T18:21:14+00:00

I look for a LayoutManager that has fixed height JButtons that expand in width

  • 0

I look for a LayoutManager that has fixed height JButtons that expand in width to fit the size of their Container. Above the JButtons there should be a JLabel by itself on the line that says “Choose file:”. This is intended to work as an accessory to a JFileChooser that lets the user choose recent files. I haven’t been able to make it look quite right, I’ve tried using multiple JPanels and LayoutManagers such as BoxLayout. When using BoxLayout the JButtons only expand as far as they have to to contain their text; but I would like for all of the JButtons to be the same width so they don’t look funny.

Note: I’ve also used other LayoutManagers such as Border and GridLayout but those mostly ignore my size settings and aren’t sophisticated enough it would seem. I have to do this by hand, Netbeans etc are not an option.

Working example, but visually incorrect

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Chooser extends JPanel {


    public Chooser(){
        this.setLayout(new GridLayout(0,1));

        JPanel labelPanel = new JPanel();
        JLabel label = new JLabel("Choose a file:");
        labelPanel.add(label);
        labelPanel.setBackground(Color.red);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("long pathname to a file goes here"));
        buttonPanel.setBackground(Color.blue);

        this.add(labelPanel);
        this.add(buttonPanel);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Chooser c = new Chooser();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAccessory(c);
        fileChooser.showOpenDialog(null);
    }

}
  • 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-25T18:21:14+00:00Added an answer on May 25, 2026 at 6:21 pm

    What about something where a GridLayout is nested in a BorderLayout (actually in a JScrollPane)…

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    
    import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class Chooser extends JPanel {
       private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
             "Hello Goodbye", "Adios", "This is a long String for WTF" };
    
       public Chooser() {
          this.setLayout(new BorderLayout());
    
          JPanel labelPanel = new JPanel();
          JLabel label = new JLabel("Choose a file:");
          labelPanel.add(label);
          labelPanel.setBackground(Color.red);
    
          JPanel buttonPanel = new JPanel(new GridLayout(0, 1));
          for (int i = 0; i < BUTTON_TEXTS.length; i++) {
             buttonPanel.add(new JButton(BUTTON_TEXTS[i]));
          }
          buttonPanel.add(Box.createVerticalGlue()); // to pad the bottom if needed
          buttonPanel.setBackground(Color.blue);
    
          this.add(labelPanel, BorderLayout.PAGE_START);
          this.add(new JScrollPane(buttonPanel), BorderLayout.CENTER);
       }
    
       public static void main(String[] args) {
          Chooser c = new Chooser();
          JFileChooser fileChooser = new JFileChooser();
          fileChooser.setAccessory(c);
          fileChooser.showOpenDialog(null);
       }
    
    }
    

    Example 2 with a simple JList that places its selection in the file chooser:

    import java.awt.*;
    import java.io.File;
    
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class Chooser extends JPanel {
       private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
             "Hello Goodbye", "Adios", "This is a long String for WTF", "Hello",
             "Goodbye", "Hello Goodbye", "Adios", "This is a long String for WTF" };
    
       public Chooser(final JFileChooser fileChooser) {
          setLayout(new BorderLayout());
    
          JPanel labelPanel = new JPanel();
          JLabel label = new JLabel("Choose a file:");
          labelPanel.add(label);
          labelPanel.setBackground(Color.red);
    
          final JList list = new JList(BUTTON_TEXTS);
          list.addListSelectionListener(new ListSelectionListener() {
             public void valueChanged(ListSelectionEvent e) {
                String selectedString = list.getSelectedValue().toString();
                fileChooser.setSelectedFile(new File(selectedString));
             }
          });
    
          add(labelPanel, BorderLayout.PAGE_START);
          add(new JScrollPane(list), BorderLayout.CENTER);
       }
    
       public static void main(String[] args) {
          JFileChooser fileChooser = new JFileChooser();
          Chooser c = new Chooser(fileChooser);
          fileChooser.setAccessory(c);
          fileChooser.showOpenDialog(null);
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

look at this image folks, instead of using overflow:hidden there is still some text
Look I have like this code. <MediaElement Source=Wildlife.wmv/> It didn't work.This file wildlife has
There are a few questions that approach an answer to this question, but none
Look at this makefile, it has some sort of primitive progress indication (could have
Look at the problem: Normally, in the interactive Haskell environment, non-Latin Unicode characters (that
Look at http://www.sydsvenskan.se/ or just look at the image bellow, and check their big
Look at this image: alt text http://img139.imageshack.us/img139/4488/picture2ep3.png I know how to add UITableView with
Look at this situation: www.websitea.com displays an img tag with a src attribute of
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
Look at the following picture, which is showing a smart tag for DataGridView. DataGridView

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.