Make Java JScrollpane only scroll vertically

I would like the whole JFrame to scroll vertically.

I 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 a horizontal scrollbar.

I saw the horizontal policy code, but it doesn't seem to work. I don’t know what to call it. This code looks something like this:

 setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

Thanks!

+6
java user-interface scrollbar jscrollpane
source share
1 answer

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); } } 

In addition, you can take a look at How to use ScrollBar in both vertical and horizontal directions .

+19
source share

All Articles