-
JTextArea Scrollers
Hey everyone,
I've been working on this problem for some time now and I'm still not able to figure it out.
I have two separate JTextAreas sitting next to each other in the same JPanel. I cannot figure out how to add a scroller to either of them. When the JTextArea is filled with text, it continues to fill up only I can't scroll down.
I've tried JScrollPanes but It still won't work, any suggestions?
-
Can you post what you have tried? Are you adding the JTextArea to the JScrollPane?
-
This is a part of the program.
public static class ContentPanel extends JPanel implements ActionListener {
JButton resetButton;
JButton spinIt;
JTextField titleText;
String clear = "";
String beenSpun;
ContentPanel() {
setLayout(null);
setBackground(Color.LIGHT_GRAY);
// ------ Action Listeners ------
// ------ JLabels ------
JLabel titleLabel = new JLabel("Title");
titleLabel.setBounds(50,35,50,20);
titleLabel.setForeground(Color.BLACK);
titleLabel.setBackground(Color.GRAY);
titleLabel.setFont(new Font("Serif",Font.BOLD,14));
titleLabel.setOpaque(true);
JLabel bodyLabel = new JLabel("Body");
bodyLabel.setBounds(50,85,50,20);
bodyLabel.setForeground(Color.BLACK);
bodyLabel.setBackground(Color.GRAY);
bodyLabel.setFont(new Font("Serif",Font.BOLD,14));
bodyLabel.setOpaque(true);
JLabel title = new JLabel("New Article Spinner Interface");
title.setBounds(350,10,300,30);
title.setForeground(Color.BLACK);
title.setBackground(Color.LIGHT_GRAY);
title.setFont(new Font("Tahoma", Font.BOLD, 18));
title.setOpaque(true);
// ------ JScrollPane ------
JScrollPane bodyScroll = new JScrollPane(bodyText);
// ------ LineBorder ------
LineBorder border = new LineBorder(Color.GRAY, 2, true);
// ------ JTextArea ------
titleText = new JTextField();
titleText.setBounds(50,60,900,30);
titleText.setBorder(border);
bodyText = new JTextArea();
bodyText.setBounds(50,110,440,500);
bodyText.setBorder(border);
bodyText.setEditable(true);
bodyText.setLineWrap(true);
bodyText.add(bodyScroll);
spunText = new JTextArea();
spunText.setBounds(510,110,440,500);
spunText.setBorder(border);
spunText.setEditable(false);
spunText.setLineWrap(true);
wordCount = new JTextField(" Word Count: ");
wordCount.setBounds(510,620,150,40);
wordCount.setBackground(Color.LIGHT_GRAY);
wordCount.setEditable(false);
// ------ JButtons ------
spinIt = new JButton("Spin It!");
spinIt.addActionListener(this);
spinIt.setBounds(850,625,100,40);
resetButton = new JButton("Reset");
resetButton.addActionListener(this);
resetButton.setBounds(750,625,100,40);
add(spinIt);
add(resetButton);
add(titleText);
add(bodyText);
add(spunText);
add(wordCount);
add(title);
}
I've tried a few other things but none of them seem to work. Any recommendations? Thanks for the help.
-
Does that compile? You have "JScrollPane bodyScroll = new JScrollPane(bodyText);" but I don't see bodyText declared anywhere. Later you have "bodyText.add(bodyScroll);".
It's best to post something that compiles and runs to illustrate the problem. This might involve constructing a small example of the problem (without all the font/label/etc stuff which isn't part of the problem.)
-
I meant to add: do read the section in Oracle's Tutorial on How to Use Text Areas as more or less the first thing it addresses is using scroll panes.
-
This runs by itself.....have a look at the changes I made, I put comments where i made them
edit: you will need to resize the JFrame....
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TextAreaScroll extends JPanel implements ActionListener {
JButton resetButton;
JButton spinIt;
JTextArea bodyText=null;
JTextArea spunText=null;
JTextField wordCount=null;
JTextField titleText;
JFrame frame=null;
String clear = "";
String beenSpun;
TextAreaScroll() {
setLayout(null);
setBackground(Color.LIGHT_GRAY);
// ------ Action Listeners ------
// ------ JLabels ------
JLabel titleLabel = new JLabel("Title");
titleLabel.setBounds(50,35,50,20);
titleLabel.setForeground(Color.BLACK);
titleLabel.setBackground(Color.GRAY);
titleLabel.setFont(new Font("Serif",Font.BOLD,14));
titleLabel.setOpaque(true);
JLabel bodyLabel = new JLabel("Body");
bodyLabel.setBounds(50,85,50,20);
bodyLabel.setForeground(Color.BLACK);
bodyLabel.setBackground(Color.GRAY);
bodyLabel.setFont(new Font("Serif",Font.BOLD,14));
bodyLabel.setOpaque(true);
JLabel title = new JLabel("New Article Spinner Interface");
title.setBounds(350,10,300,30);
title.setForeground(Color.BLACK);
title.setBackground(Color.LIGHT_GRAY);
title.setFont(new Font("Tahoma", Font.BOLD, 18));
title.setOpaque(true);
// ------ JScrollPane ------
// set to null
JScrollPane bodyScroll = null;
// ------ LineBorder ------
LineBorder border = new LineBorder(Color.GRAY, 2, true);
// ------ JTextArea ------
titleText = new JTextField();
titleText.setBounds(50,60,900,30);
titleText.setBorder(border);
bodyText = new JTextArea();
bodyText.setBorder(border);
bodyText.setEditable(true);
bodyText.setLineWrap(true);
// only adding bodyText to JScrollPane
bodyScroll=new JScrollPane(bodyText);
// ScrollPane gets bounds
bodyScroll.setBounds(50,110,440,500);
spunText = new JTextArea();
spunText.setBounds(510,110,440,500);
spunText.setBorder(border);
spunText.setEditable(false);
spunText.setLineWrap(true);
wordCount = new JTextField(" Word Count: ");
wordCount.setBounds(510,620,150,40);
wordCount.setBackground(Color.LIGHT_GRAY);
wordCount.setEditable(false);
// ------ JButtons ------
spinIt = new JButton("Spin It!");
spinIt.addActionListener(this);
spinIt.setBounds(850,625,100,40);
resetButton = new JButton("Reset");
resetButton.addActionListener(this);
resetButton.setBounds(750,625,100,40);
add(spinIt);
add(resetButton);
add(titleText);
// add ScrollPane not the TextArea
add(bodyScroll);
add(spunText);
add(wordCount);
add(title);
frame=new JFrame();
frame.getContentPane().add(this);
frame.setSize(1000,600);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[]args){
new TextAreaScroll();
}
}
-
Ok I understand now. I appreciate the help!