Forum Stats

  • 3,875,502 Users
  • 2,266,930 Discussions
  • 7,912,239 Comments

Discussions

adding scrollbars to textArea

fagus
fagus Member Posts: 1
edited Mar 17, 2018 11:20AM in Java Programming

Hello

I 'm new to java and I have trouble adding scrollbars to the textArea of my program. The program makes a kind of magical calculation and displays the result - a lot of numbers - in a textarea: resultField. The program runs well, except for the scrollbars I want to add.

The class for the window of the program is:

package h03;import javax.swing.JFrame;public class scrollbars extends JFrame    {    public scrollbars()    {        JFrame window = new JFrame();        window.setSize(300, 300);        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        window.setTitle("scrollbars");        window.setLocation(100, 50);        window.add(new scrollbarsPanel());        window.setVisible(true);    }            public static void main(String[] args)    {        new scrollbars();    }}

and for the panel the code is:

package h03;import javax.swing.*;import java.awt.event.*;public class scrollbarsPanel extends JPanel implements ActionListener{    private JTextField inputField;    private JTextArea resultField;    private JButton calcButton;    private JScrollBar scrollArea;    private int input;        public scrollbarsPanel()    {        calcButton = new JButton("start calculation");        inputField = new JTextField("000", 3);        resultField = new JTextArea(9, 24);        scrollArea = new JScrollBar();        resultField.setLineWrap(true);        resultField.setWrapStyleWord(true);        this.add(scrollArea);                add(new JLabel ("number"));        add(inputField);        inputField.addActionListener(this);        add(calcButton);        calcButton.addActionListener(this);        add(resultField);    }        public void showResult(int input)    {        input = Integer.parseInt(inputField.getText());        resultField.setText(input + "   ");        while (input != 1)        {            if (input % 2 == 0)            {                resultField.append((input / 2) + "   ");                input = input/2;                System.out.println(input);            }                        else             {                resultField.append((input * 3 + 1) + "   ");                input = input * 3 + 1;                System.out.println(input);            }        }    }        public void actionPerformed(ActionEvent e)    {        showResult(input);        }}

I really can 't figure out what I am doing wrong. Can someone help me please?

Thanks a lot!

fagus

Answers

  • Unknown
    edited Mar 17, 2018 11:20AM
    I really can 't figure out what I am doing wrong. Can someone help me please?

    Sure - the BEST way to learn how to use scrollbars is to examine code that uses them and already works.

    A good way to do that is read The Java Tutorials trail 'How to Use Scroll Panes'

    https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

    That trail will explain what Scroll Panes, what they are and how to use them. It includes example code that works.

    Try that exact example and make it it works for you. Then compare your code to the code used in the example and you will see your problem.

    Good luck.

This discussion has been closed.