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

It seems like the scrollpane should work.
Have you tried to use it yet?

As I said, the scroll pane seems to be working. I'm looking for a way to prevent the buttons from dividing themselves into two columns.

When I set the layout it seems to ignore the fact that I'm explicitly telling it to use only 1 column ! :S

Try a flowlayout or a Boxlayout

As I said in my original post (people don't seem to read it! :P)

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.

Will try the FlowLayout then (thanks for the suggestion!), although I was under the impression that FlowLayout only worked horizontally :S

- leiger

If you wantthe buttons to stretch the width of the Jframe then set the size of them based on the width of the frame

I can't see what your problem is. I took your code and wrapped some more around it and get a screen with a scollpane with 15 button in a column.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class MakeColumnOfButtons extends JFrame {

   int windowWidth = 600;
   int windowHeight = 400;

   public MakeColumnOfButtons() {
      setSize(600, windowHeight);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Set layout for the whole dialog
      JPanel contentPane = (JPanel)this.getContentPane();
      contentPane.setLayout(new BorderLayout());
      
      // Set layout for contactListPane
      JPanel contactListPanel = new JPanel();
      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(" Jones, Jim " + i); //contactList.get(i).getSurname() + ", " + contactList.get(i).getGivenName());
         contactListPanel.add(button);
      }
      
      //(...)
      
      JScrollPane scrollPane = new JScrollPane(contactListPanel);
      // Add the contactListPane to the main content area (inside scrollPane)
      contentPane.add(scrollPane, BorderLayout.CENTER);
      setVisible(true);
   
   } 
public static void main(String[] args) {
  new MakeColumnOfButtons();
}
}

Okay, your code does exactly what I need.

Yet, it looks identical to mine. Maybe something I removed (replacing with //...) is causing it to divide into two columns. Will have another look.

Thanks.

Okay, I've found the problem (but don't understand it):

This code is located just before the for loop that adds the rest of the buttons...

JButton buttonOK = new JButton();
buttonOK.setText("<- Back to Main Menu");
buttonOK.addActionListener(this);
contactListPanel.add(buttonOK);

If I comment out the last line where it's added to the panel, everything displays vertically as I'd like it to (albeit without the "back" button).

Tried adding this to the for loop inside an if(i==0) but that didn't make any difference. Any suggestions?


Edit: Now that I think about it, it's probably a better idea to add this button to a separate panel in the dialog anyway, using BorderLayout to position it. Will try that, but am interested to know if anyone here understands why that line of code is causing such a change in the layout.

Did adding that extra component push the number of components past the number that are to go in a column: 16 vs 15? Change the layout manager to have 16

That would have been it! I feel so stupid now ;-)

Will test that later but I assume it'll work if I increase the limit to 16. I ended up deciding to put the back button into it's own panel and that's where it is at the moment.

Will mark this as solved - thank you to everyone that replied and tried to help.

- leiger

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.