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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.