Home > Blockchain >  How to make a JTextArea vertically scrollable in java?
How to make a JTextArea vertically scrollable in java?

Time:11-06

I have a JTextArea component as the following :

        textArea = new JTextArea();
        textArea.setBounds(0,0,490,390);
        textArea.setEditable(false);
        textArea.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,30));
        textArea.setFocusable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

and then I add the scrollPane to a JFrame with a null layout:

this.add(scrollPane);

but the scroll bar won't appear, I'd be glad if someone helps me.

CodePudding user response:

If you change the property VERTICAL_SCROLLBAR_AS_NEEDED to VERTICAL_SCROLLBAR_ALWAYS will cause the scrollbar to show even if the text buffer is empty as shown on the image below.

The entire code

public class ScrollBarDemo {
    public static void main(String[] args) {
        JTextArea textArea = new JTextArea();
        textArea.setBounds(0,0,490,390);
        textArea.setEditable(false);
        textArea.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,30));
        textArea.setFocusable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        JFrame frame = new JFrame();
        frame.add(scrollPane);
        frame.setTitle("Scrollbar Demo");
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

I know I am not using SwingUtilities to properly start the Swing application, but I just did it this way to provide a quick answer to the OP. Also, it is not relevant for the issue being asked.

enter image description here

EDIT:

Exaggerated the dimensions of the components for the OP to see his or her error. The problem is that, because the OP was not using a Layout Manager, whatever the OP created was exceeding the bounds of the frame. Therefore, the scrollbar was outside the viewable area.

public class ScrollBarDemo {
    public static void main(String[] args) {
        JTextArea textArea = new JTextArea();
        textArea.setBounds(0,0,490,390);
        textArea.setEditable(false);
        textArea.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,30));
        textArea.setFocusable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setBounds(50, 50, 400, 400);

        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        pane.setBounds(0, 0, 600, 800);
        pane.add(scrollPane);
        frame.add(pane);
        frame.setTitle("Scrollbar Demo");
        frame.setSize(800, 800);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

enter image description here

  • Related