А как насчет того, где GridLayout вложен в BorderLayout (фактически в 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);
}
}
Пример 2 с простым JList, который помещает свой выбор в средство выбора файлов:
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);
}
}