Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Java JScrollpane only scroll vertically

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!

like image 257
Philip McQuitty Avatar asked Apr 03 '11 04:04

Philip McQuitty


People also ask

How do I make my Java GUI scrollable?

Create a JTextArea . Call new JScrollPane(textArea) to create a scrollable Text Area. Remember that JScrollPane is a container and you can add any component you want to it to make it scrollable. Use setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy to set the vertical and horizontal scroll bar policies.

What is the difference between JScrollPane and scrollbar?

What are the differences between a JScrollBar and a JScrollPane in Java? A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.

How do I make an image scroll vertically in HTML?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.


1 Answers

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.

like image 113
javing Avatar answered Oct 31 '22 18:10

javing