Results 1 to 7 of 7
Thread: JTextArea Scrollers
- 10-27-2010, 09:01 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
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?
- 10-27-2010, 09:21 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Can you post what you have tried? Are you adding the JTextArea to the JScrollPane?
- 10-27-2010, 09:51 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
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.
- 10-27-2010, 10:04 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.)
- 10-27-2010, 10:11 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 10-27-2010, 10:17 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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....
Java 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(); } }Last edited by al_Marshy_1981; 10-27-2010 at 10:19 PM.
- 10-27-2010, 10:23 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
help me with JTextArea
By javanew in forum New To JavaReplies: 3Last Post: 05-04-2010, 08:51 PM -
JTextArea on PopUp -JTextArea isn't editable
By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 07:51 PM -
Tab key in JTextArea
By KristoZ in forum New To JavaReplies: 1Last Post: 09-25-2009, 07:27 PM -
About JTEXTAREA
By makpandian in forum AWT / SwingReplies: 4Last Post: 03-19-2009, 06:53 AM -
JTextArea
By saytri in forum New To JavaReplies: 0Last Post: 01-13-2008, 01:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks