I'm trying to list a bunch of buttons vertically, but am having problems even getting that much done. I'm pretty sure I have the scroll pane working (it just isn't being used yet so I can't test it).
This is part of an assignment so I'd prefer not to post the whole code I've done so far, I'll just post snippets of it instead that are most relevant to help figure out why It's acting this way...
//(...)
// Set layout for the whole dialog
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(new BorderLayout());
// Set layout for contactListPane
JScrollPane scrollPane = new JScrollPane(contactListPanel);
contactListPanel.setLayout(new GridLayout(15, 1)); // 15 rows, 1 column
contactListPanel.setMinimumSize(new Dimension(windowWidth, windowHeight));
contactListPanel.setPreferredSize(new Dimension(windowWidth, windowHeight));
contactListPanel.setMaximumSize(new Dimension(windowWidth, windowHeight));
//(...)
//Add a button for each contact in the address book (from a LinkedList)
for (int i=0; i<15; i++)
{
JButton button = new JButton();
button.setText(contactList.get(i).getSurname()+", "+contactList.get(i).getGivenName());
contactListPanel.add(button);
}
//(...)
// Add the contactListPane to the main content area (inside scrollPane)
contentPane.add(scrollPane, BorderLayout.CENTER);
//(...)
I tried using BoxLayout instead originally, but while it forced the buttons to only be added vertically to the frame, I couldn't get buttons to stretch to the width of the frame as I'd like them to, and moved back to GridLayout instead.
When using the BoxLayout, I also tried manually specifying the size of the button with the setSize() method, from inside the for loop. That seemed to be ignored though.
Screenshot of what it looks like at the moment:
http://img822.imageshack.us/img822/639/daniwebno.jpg
What I'm trying to do: (ignore the stretched text...)
http://img812.imageshack.us/img812/672/daniwebyes.jpg