Link to home
Start Free TrialLog in
Avatar of KPax
KPax

asked on

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

Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of KPax
KPax

ASKER

@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.
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).
Failing that, if you mean you want the bars there ALL THE TIME, then you have to set the policy for that.
Avatar of KPax

ASKER

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.
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.
Avatar of KPax

ASKER

I have posted my code! Look at the first post again.
it's that there is no JTA in the final result:

User generated image
Right, so if you knew there was no JTextArea, why was your question asking about the lack of a vertical scrollbar?
Avatar of KPax

ASKER

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.
OK so afaics, it's your layout manager that is causing the problem. Get rid of it.
ASKER CERTIFIED SOLUTION
Avatar of KPax
KPax

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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