Results 1 to 7 of 7
  1. #1
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

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

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Can you post what you have tried? Are you adding the JTextArea to the JScrollPane?

  3. #3
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

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

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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 11:19 PM.

  7. #7
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. help me with JTextArea
    By javanew in forum New To Java
    Replies: 3
    Last Post: 05-04-2010, 09:51 PM
  2. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 08:51 PM
  3. Tab key in JTextArea
    By KristoZ in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 08:27 PM
  4. About JTEXTAREA
    By makpandian in forum AWT / Swing
    Replies: 4
    Last Post: 03-19-2009, 07:53 AM
  5. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 02:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •