Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JScrollPane does not work

Here is a code of a simple editor.

public class editor {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        final JTextArea area = new JTextArea(20,120);
        JScrollPane scrollingResult = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        f.getContentPane().add(scrollingResult);
        ...
    }
}

This doesn't work. JTextArea is fine, so is the Frame, but JScrollPane is still disabled. Why could that be?

like image 824
nicks Avatar asked Dec 14 '25 05:12

nicks


1 Answers

You have just created a JTextArea and added it to a JScrollPane. However I cannot see any text being added to your JTextArea. The scroll option activates only when you have something to scroll through.

Also I would suggest that you change your HorizontalScrollBarPolicy and VerticalScrollBarPolicy from being JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS & JScrollPane.VERTICAL_SCROLLBAR_ALWAYS to HORIZONTAL_SCROLLBAR_AS_NEEDED and VERTICAL_SCROLLBAR_AS_NEEDED respectively.

like image 53
Sujay Avatar answered Dec 15 '25 19:12

Sujay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!