7

I would like my entire JFrame to vertically scroll.

I have added the following code, but it only creates a horizontal scrollbar.

frame.setContentPane(new JScrollPane(new GradeQuickResource())); 

I want to do the opposite. I ONLY want a vertical scrollbar and NO horizontal scrollbar.

I have seen the horizontal policy code, but it doesn't seem to work. I do not know what to name it. This code looks something like this:

setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

Thanks!

Philip McQuitty
  • 1,077
  • 9
  • 25
  • 36
  • 1
    The code you posted doesn't make sense so its no wonder it doesn't work. You don't have a reference to the scrollpane, so how can you set the horizontal scroll policy? Post your SSCCE (http://sscce.org) that demonstrates the problem. – camickr Apr 03 '11 at 04:23

2 Answers2

21

Try this:

public class AddScrollBarToJFrame {

    public static void main(String[] args) {
        JPanel panel = new JPanel();
        JScrollPane scrollBar = new JScrollPane(panel,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JFrame frame = new JFrame("AddScrollBarToJFrame");
        frame.add(scrollBar);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

Also, you can have a look at How to use a ScrollBar in both vertical and horizontal direction.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
javing
  • 12,307
  • 35
  • 138
  • 211
  • This is the link for the first part : http://java2everyone.blogspot.com/2008/12/add-scrollbar-to-jframe.html – javing Apr 03 '11 at 11:17
  • Ah, I see! I added the citation above. For reference, I specified `HORIZONTAL_SCROLLBAR_NEVER` to match the question's requirement. +1, BTW. – trashgod Apr 03 '11 at 11:29
  • For already created panes, these methods can be used: `pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);` and `pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);` – Halil İbrahim Oymacı Apr 01 '20 at 10:10
-1

AFIK: JScrollPane scrollableTa=new JScrollPane(targetComp); scrollableTa.setHorizontalScrollBar(null); works

judovana
  • 418
  • 2
  • 6