How to a add a vertical scroll bar to a JTextArea in Swing?

It's been a while since I was manually creating UI in Swing, so I lost routine.

Here is a piece of code which was working nice with two JTextArea, but then I decided to add two JScrollPanes and now JTextAreas are not visible anymore. What did I do wrong?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.zip.InflaterInputStream;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GUI extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	protected JTextArea textArea1;
	protected JTextArea textArea2;
	protected JScrollPane scrollPane1;
	protected JScrollPane scrollPane2;
	private JLabel label1;
	private JLabel label2;
	private JButton jbutton1;
	private JButton jbutton2;
	private JButton jbutton3;

	public static void main(final String[] args) {
		GUI.createAndShowGUI();
	}

	private static void createAndShowGUI() {
		final JFrame frame = new JFrame("S1 Log Decryptor v1.0");
		frame.setDefaultCloseOperation(3);
		frame.add(new GUI());
		frame.setSize(800, 600);
		frame.setVisible(true);
	}

	public GUI() {
		setLayout(null);

		(label1 = new JLabel()).setText("Enter encrypted string:");
		(label2 = new JLabel()).setText("Decryption result:");
		(jbutton1 = new JButton()).setText("Process");
		(jbutton2 = new JButton()).setText("Clear input");
		(jbutton3 = new JButton()).setText("About");

		jbutton1.addActionListener(this);
		jbutton2.addActionListener(this);
		jbutton3.addActionListener(this);

		(textArea1 = new JTextArea(10, 80)).setEditable(true);
		textArea1.setLineWrap(true);
		label1.setBounds(10, 10, 250, 25);
		textArea1.setBounds(10, 40, 765, 200);

		(textArea2 = new JTextArea(10, 80)).setEditable(false);
		textArea2.setLineWrap(true);
		label2.setBounds(10, 320, 200, 25);
		textArea2.setBounds(10, 350, 765, 200);

		jbutton1.setBounds(10, 270, 150, 25);
		jbutton2.setBounds(200, 270, 150, 25);
		jbutton3.setBounds(625, 270, 150, 25);

		scrollPane1 = new JScrollPane(textArea1);
		scrollPane2 = new JScrollPane(textArea2);

		this.add(label1);
		this.add(jbutton1);
		this.add(jbutton2);
		this.add(jbutton3);
		this.add(label2);
		//this.add(this.textArea1);
		this.add(scrollPane1);
		//this.add(this.textArea2);
		this.add(scrollPane2);
	}

Open in new window

KPaxAsked:
Who is Participating?
 
KPaxAuthor Commented:
I am not with problem neither - I found solution on my own.
		(textArea1 = new JTextArea(10, 80)).setEditable(true);
		textArea1.setLineWrap(true);
		label1.setBounds(10, 10, 250, 25);
		//textArea1.setBounds(10, 40, 765, 200);

Open in new window


		(textArea2 = new JTextArea(10, 80)).setEditable(false);
		textArea2.setLineWrap(true);
		label2.setBounds(10, 320, 200, 25);
		//textArea2.setBounds(10, 350, 765, 200);

Open in new window


 		scrollPane1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane1.setBounds(10, 40, 765, 200); // this is added
		scrollPane2 = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane2.setBounds(10, 350, 765, 200); // this is added

Open in new window


Screen-Shot-05-03-15-at-08.39-PM.PNG
0
 
krakatoaCommented:
You use a JScrollpane.

... and add the JTextArea to the scrollpane constructor, smthg like :

JScrollPane(Component view)
Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.
0
 
KPaxAuthor Commented:
@krakatoa I am sorry but I don't understand your advice. Have you looked at all at my code? I added
protected JScrollPane scrollPane1;
protected JScrollPane scrollPane2;

Open in new window

then I added text areas to them
scrollPane1 = new JScrollPane(textArea1);
scrollPane2 = new JScrollPane(textArea2);

Open in new window

and finally I added both JScrollPane to jframe
//this.add(this.textArea1);
this.add(scrollPane1);
//this.add(this.textArea2);
this.add(scrollPane2);

Open in new window


But it doesn't work.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
krakatoaCommented:
It probably didnt work because you didnt enter sufficient text to make the bars appear.

But if you did, then you've done something wrong, in which case follow this rough stuff :

import java.util.*;
import javax.swing.*;
import java.awt.*;

class Scr extends JFrame{

public static void main(String[] args){


Scr scr = new Scr();

scr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

scr.setSize(new Dimension(200,300));

JScrollPane jscrp = new JScrollPane(new JTextArea());

scr.add(jscrp);

scr.setVisible(true);

}




}

Open in new window



(and no, I didnt look at your code, because that has no impact on how the principle here works).
0
 
krakatoaCommented:
Failing that, if you mean you want the bars there ALL THE TIME, then you have to set the policy for that.
0
 
KPaxAuthor Commented:
I've been using scrollbars in the past, and I know about
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER

Open in new window

etc.
but there is something very wrong in this particular case: it's not that I haven't entered enough of text it's that THERE IS NO JTEXTAREA (altogether with JScrollPane) at all.
0
 
krakatoaCommented:
THERE IS NO JTEXTAREA

How are we supposed to know that? You haven't posted the code where the JTA is created - so  we cannot know. It's up to you to create the JTA - you can see clearly in my code that a JTA is created, and that the desired effect is achieved. Look at your code again. Or post it.
0
 
KPaxAuthor Commented:
I have posted my code! Look at the first post again.
it's that there is no JTA in the final result:

Screen-Shot-05-03-15-at-07.45-PM.PNG
0
 
krakatoaCommented:
Right, so if you knew there was no JTextArea, why was your question asking about the lack of a vertical scrollbar?
0
 
KPaxAuthor Commented:
What is your problem? What do you don't understand?
JTextArea is added to JScrollPane and JScrollPane is added to JFrame but as a final result neither JTextArea or JScrollPane are displayed.
0
 
krakatoaCommented:
OK so afaics, it's your layout manager that is causing the problem. Get rid of it.
0
 
krakatoaCommented:
You'll have to do some cleaning up (resizing etc), but this cut-down of your code works for me, (once you resize the frame manually, and then enter text to cause the scrollbars to kick in) :

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GUI extends JPanel {

	
	protected JTextArea textArea1;
	protected JTextArea textArea2;
	protected JScrollPane scrollPane1;
	protected JScrollPane scrollPane2;
	private JLabel label1;
	private JLabel label2;
	private JButton jbutton1;
	private JButton jbutton2;
	private JButton jbutton3;

	public static void main(String[] args) {
		
		final JFrame frame = new JFrame("S1 Log Decryptor v1.0");
		frame.setDefaultCloseOperation(3);
		frame.add(new GUI());
		frame.setSize(800, 600);
		frame.setVisible(true);
	}

	public GUI() {
		//setLayout(null);

		(label1 = new JLabel()).setText("Enter encrypted string:");
		(label2 = new JLabel()).setText("Decryption result:");
		(jbutton1 = new JButton()).setText("Process");
		(jbutton2 = new JButton()).setText("Clear input");
		(jbutton3 = new JButton()).setText("About");

		(textArea1 = new JTextArea(10, 80)).setEditable(true);
		textArea1.setLineWrap(true);
		label1.setBounds(10, 10, 250, 25);
		textArea1.setBounds(10, 40, 765, 200);

		(textArea2 = new JTextArea(10, 80)).setEditable(false);
		textArea2.setLineWrap(true);
		label2.setBounds(10, 320, 200, 25);
		textArea2.setBounds(10, 350, 765, 200);

		jbutton1.setBounds(10, 270, 150, 25);
		jbutton2.setBounds(200, 270, 150, 25);
		jbutton3.setBounds(625, 270, 150, 25);

		scrollPane1 = new JScrollPane(textArea1);
		scrollPane2 = new JScrollPane(textArea2);

		this.add(label1);
		this.add(jbutton1);
		this.add(jbutton2);
		this.add(jbutton3);
		this.add(label2);
		//this.add(this.textArea1);
		this.add(scrollPane1);
		//this.add(this.textArea2);
		this.add(scrollPane2);
	}
	
	}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.