Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.5K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.8K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 27 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 393 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
adding scrollbars to textArea

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
-
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.