Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Java.swing JScrollPane

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java.swing JScrollPane

    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:

     
    //scrollbars.java
     
    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:
     
    //scrollbarsPane.java
     
    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 JScrollPane scrollArea;
    	private int input;
     
    	public scrollbarsPanel()
    	{
    		calcButton = new JButton("start calculation");
    		inputField = new JTextField("000", 3);
    		resultField = new JTextArea(9, 24);
    		JScrollPane scrollArea = new JScrollPane(resultField);
    		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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java.swing JScrollPane

    What is the purpose of the while loop in showResults? It never stops!!!
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java.swing JScrollPane

    It stops at number 1 if you other a number of 3 digits or less. But my problem is that I can 't get the scrollbars in the resultfield ... .
    Fagus

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java.swing JScrollPane

    What if the calcButton is pressed without the user changing the contents of inputField? The value is always 0 and the loop never ends.

    Look at where resultField is added to the GUI? It should only be added one time.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Java.swing JScrollPane

    You should add the JScrollPane to the panel, but NOT the JTextArea.
    this.add(scrollArea); // This is good
     
    add(resultField); // Take this out

  6. #6
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java.swing JScrollPane

    Thanx but it doesn' work. Now I have a scrollbar, but no result field.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java.swing JScrollPane

    Please post the current code so we can see what has been changed and can test it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java.swing JScrollPane

    Okay
    The code for the window 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:

    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);
     
    }
    }

    Thanks!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java.swing JScrollPane

    What happened to the ScrollPane?
    A ScrollBar does not cause a text area to scroll. It is to allow a user to give input to a program

    Did you try what I suggested?
    Post the code that does what I suggested. That change worked for me.

    Also posted at: https://coderanch.com/t/691804/java/...lbars-textArea
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    fagus (March 17th, 2018)

  11. #10
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [SOLVED]Re: Java.swing JScrollPane

    okay thanks a lot I got it working finaly

    the code:
    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:
    package h03;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
     
    public class ScrollbarsPanel extends JPanel implements ActionListener
    {
    	private JTextField inputField;
    	private JTextArea resultField;
    	private JButton calcButton;
    	//private JScrollBar scrollArea; //
    	private JScrollPane jScrollPanel;
    	private int input;
     
    	public ScrollbarsPanel()
    	{
    		calcButton = new JButton("start calculation");
    		inputField = new JTextField("000", 3);
    		resultField = new JTextArea(4, 24);
    		//scrollArea = new JScrollBar(); //
    		resultField.setLineWrap(true);
    		resultField.setWrapStyleWord(true);
     
    		add(new JLabel ("number"));
    		add(inputField);
    		inputField.addActionListener(this);
    		add(calcButton);
    		calcButton.addActionListener(this);
    		//add(resultField);
    		jScrollPanel = new JScrollPane(resultField);
    		//this.add(scrollArea);
    		this.add(jScrollPanel);
    	}
     
    	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);
     
    }
    }

    As for the remark on the endless loop: I 'm learning Java with a book in which endless loops is the next chapter. I suppose this code will be an illustration for that chapter. Anyways, I can continue with my book now. Thanks

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java.swing JScrollPane

    OK, I'm glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Mar 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java.swing JScrollPane

    A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically. Other containers used to save screen space include split panes and tabbed panes. The code to create a scroll pane can be minimal.

Similar Threads

  1. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 07:32 PM
  2. Need help with jScrollPane
    By PuddinPie in forum Other Programming Languages
    Replies: 2
    Last Post: May 30th, 2013, 12:57 PM
  3. Java Swing
    By arvin17 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2013, 09:40 AM
  4. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  5. JScrollPane
    By MysticDeath in forum AWT / Java Swing
    Replies: 1
    Last Post: February 17th, 2010, 10:21 PM