Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

adding scrollbars to textArea

fagusMar 17 2018 — edited Mar 17 2018

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

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 14 2018
Added on Mar 17 2018
1 comment
383 views