Angelo
Angelo

Reputation: 111

Make Java JScrollpane scroll vertically instead of horizontally

I am new to Java and currently trying to learn it. I am working on a simple program to display a number of buttons on a frame. I also want to make the panel scroll vertically but instead it scrolls horizontally.

Here is my code so far:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class GridView {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    JFrame frame = new JFrame("Display Buttons");
    frame.setBounds(30, 30, 300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout grid = new GridLayout(3, 4, 30, 20);
    Container content = frame.getContentPane(); 
    content.setLayout(grid); 
    JPanel panel = new JPanel();
    JButton button = null;
    for (int i = 1; i <= 100; i++) {
        panel.add(button = new JButton(" Press " + i));
    }
    content.add(panel);
    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JScrollPane(panel), BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}
}

Can anyone tell me why my scroll is shown horizontally and how to fix it? Any kind of response will be much appreciated.

EDIT: I am sorry. My question was incomplete. I want to make it something like this, but with vertical scroll.

enter image description here

Upvotes: 1

Views: 1653

Answers (2)

c0der
c0der

Reputation: 18812

It shows horizontally because the buttons are added horizontally so the width of the panel exceeds the view port.
If you added the buttons vertically, by using a different layout manager, the scroll pane would show vertically. For example:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));  //or setLayout(grid); if you meant to use tge gridlayout for the buttons 
/*JButton button = null;*/ //never used
for (int i = 1; i <= 100; i++) {
    panel.add(new JButton(" Press " + i));
}

Edit to answer the edited question. See comments:

public static void main(String[] args) {

    JFrame frame = new JFrame("Display Buttons");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout grid = new GridLayout(0, 4, 30, 20);
    JPanel panel = new JPanel(grid);
    for (int i = 1; i <= 100; i++) {
        panel.add(new JButton(" Press " + i));
    }

    JScrollPane sp = new JScrollPane(panel);
    //by default scrollpane will appear as needed, vertically AND horizontally
    //to prevent it from showing horizontally :
    sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    frame.add(sp, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

Upvotes: 2

camickr
camickr

Reputation: 324197

//GridLayout grid = new GridLayout(3, 4, 30, 20);
GridLayout grid = new GridLayout(0, 4, 30, 20);

If you want columns of component then don't specify the rows value in the layout manager. Just specify the columns and the components will wrap when required

//content.setLayout(grid); 
//JPanel panel = new JPanel();
JPanel panel = new JPanel(grid);

You add the buttons to the panel so you need to set the layout manager of the panel. Otherwise the JPanel will use the default FlowLayout, which display all the buttons on a single row.

Upvotes: 2

Related Questions