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);
}
}
What about something where a GridLayout is nested in a BorderLayout (actually in a JScrollPane)...
Example 2 with a simple JList that places its selection in the file chooser: