Attaxika
Attaxika

Reputation: 58

JScrollPane Not Scrolling In JTextArea

There's something that I don't understand. My code does not like JScrollBar apparently. I add it and I cannot scroll horizontally nor vertically.

Here's what it looks like:

Keep in mind that I'm new and I'm still working on it, so I'm sorry if it was something really obvious and easily avoidable.

public ChangeLog() {

    //Init.
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextArea textarea = new JTextArea();
    JScrollPane scrollpane = new JScrollPane(textarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    //Text Stuff
    textarea.setFont(textarea.getFont().deriveFont(16f));
    textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
            + "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
    textarea.setForeground(Color.BLACK);
    Dimension d = new Dimension(250, 275);
    textarea.setPreferredSize(d);

    //Other Stuff
    scrollpane.setViewportView(textarea);
    scrollpane.getPreferredSize();

    //Layout
    panel.setLayout(null);
    scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
    textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));

    //Frame Stuff
    frame.setAlwaysOnTop(true);
    frame.setSize(300, 350);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);

    //Panel Stuff
    frame.add(panel);
    panel.setSize(frame.getSize());
    panel.setBackground(Color.BLUE);
    panel.add(textarea);
    panel.add(scrollpane);
} }

Upvotes: 0

Views: 1467

Answers (2)

Aman
Aman

Reputation: 777

I have created a working solution. Made some changes also.

public TestClass() {

        //Init.
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());
        JTextArea textarea = new JTextArea();
        JScrollPane scrollpane = new JScrollPane(textarea);
        panel.add(scrollpane, BorderLayout.CENTER);



        //Text Stuff
        textarea.setFont(textarea.getFont().deriveFont(16f));
        textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
                + "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
        textarea.setForeground(Color.BLACK);
        //Dimension d = new Dimension(250, 275);
        //textarea.setPreferredSize(d);


        //Other Stuff
        scrollpane.setViewportView(textarea);
        scrollpane.getPreferredSize();




        //Layout
        //scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
        //textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));

        //Listeners



        //Frame Stuff
        frame.setAlwaysOnTop(true);
        frame.setSize(300, 350);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);


        //Panel Stuff
        frame.add(panel);
        panel.setSize(frame.getSize());
        panel.setBackground(Color.BLUE);
        panel.add(scrollpane);
    }

Also when swing better works with the layout managers and null layout will leads to inconsistent look on different screen types.

Let me know if anything more required. And yes everybody starts from scratch. I am still learning. You will too get many things. Just keep the hunger of learning. :-)

Upvotes: 1

camickr
camickr

Reputation: 324098

Dimension d = new Dimension(250, 275);
textarea.setPreferredSize(d);

Don't hardcode a size for the text area. The size of the text area will change dynamically as text is added/removed and scrollbars will appear/disappear as required.

JTextArea textarea = new JTextArea();

Don't create the text area with no parameters. Instead, when you create the text area use something like:

JTextArea textarea = new JTextArea(5, 20);

to suggest a default size of the text area. Then when you have more than 5 lines of text the scrollbar will appear.

So I'm a relatively new Java developer

Start by reading the Swing Tutorial for Swing basics. There is a section on How to Use Text Areas to get you started.

panel.setLayout(null);
scrollpane.setBounds(...)

Don't a null layout. Don't use setBounds(). Swing was designed to be used with layout managers. See the above tutorial for working examples.

Upvotes: 1

Related Questions