JTextArea Auto-Resize
Hi,
Is there any way to make a JTextArea automatically increase in size (vertically) so that text doesn't go off the bottom of it if too many lines of text are being displayed?
Thanks.
Hi,
Is there any way to make a JTextArea automatically increase in size (vertically) so that text doesn't go off the bottom of it if too many lines of text are being displayed?
Thanks.
Put the text area inside a scroll pane
It is inside a JScrollPane, however when there are too many lines of text the text just goes off the bottom out of view and you can't scroll to see it.
> It is inside a JScrollPane, however when there are
> too many lines of text the text just goes off the
> bottom out of view and you can't scroll to see it.
Then be clearer in your description of the problem and state how you have setup the textarea.
The code you want is:
int length = jTextArea.getDocument().getLength();
jTextArea.setCaretPosition(length);
If you can't scroll down to see everything then you have done something else wrong which I won't know unless I see all of your code
package toDo.gui;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
import toDo.data.ToDoHolder;
public class LogToDo extends JFrame implements ActionListener
{
private static final long serialVersionUID = -5545406794821718167L;
private int sWidth = 500,sHeight = 620;
private ToDoHolder holder;
private JPanel panelHeader, panelHistory, panelAppend;
private JLabel lHeader, lHistory;
private JTextArea taHistory, taAppend;
private JButton bAppend, bClose;
private MainGui main;
public LogToDo(ToDoHolder holder, MainGui m)
{
super("To Do Log");//form heading
//create container to place components in:
Container container = getContentPane();
container.setLayout(new FlowLayout());//set flow layout
//store references
this.holder = holder;
main = m;
panelHeader = new JPanel();
panelHistory = new JPanel();
panelAppend = new JPanel();
panelHeader.setPreferredSize(new Dimension(470,50));
panelHistory.setPreferredSize(new Dimension(470,300));
panelAppend.setPreferredSize(new Dimension(470,205));
panelHeader.setBorder(new LineBorder(Color.BLACK));
panelHistory.setBorder(new LineBorder(Color.BLACK));
panelAppend.setBorder(new LineBorder(Color.BLACK));
/*
* header panel
*/
String desc = holder.getDescription();
if (desc.length() > 20)
{
desc = desc.substring(0,18) + "...";
}
lHeader = new JLabel("Log for : " + desc);
lHeader.setFont(new Font("Comic Sans MS" , Font.BOLD, 24));
panelHeader.add(lHeader);
/*
* history panel
*/
lHistory = new JLabel("Log History :");
taHistory = new JTextArea();
taHistory.setPreferredSize(new Dimension(410,5000));
taHistory.setLineWrap(true);
taHistory.setWrapStyleWord(true);
taHistory.setEditable(false);
taHistory.setText(holder.getLog());
JScrollPane jsp = new JScrollPane(taHistory);
jsp.setPreferredSize(new Dimension(450,260));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panelHistory.add(lHistory);
panelHistory.add(jsp, FlowLayout.CENTER);
/*
* append panel
*/
taAppend = new JTextArea();
taAppend.setPreferredSize(new Dimension(410,1000));
taAppend.setLineWrap(true);
taAppend.setWrapStyleWord(true);
bAppend = new JButton("Append");
bClose = new JButton("Close");
Dimension dButton = new Dimension(220,25);
bAppend.setPreferredSize(dButton);
bClose.setPreferredSize(dButton);
bAppend.addActionListener(this);
bClose.addActionListener(this);
jsp = new JScrollPane(taAppend);
jsp.setPreferredSize(new Dimension(450,160));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panelAppend.add(jsp);
panelAppend.add(bAppend);
panelAppend.add(bClose);
/*
* add panels to container
*/
container.add(panelHeader);
container.add(panelHistory);
container.add(panelAppend);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//get screen resoloution
setLocation((d.width-sWidth)/2, (d.height-sHeight)/2);//centre form
setSize(sWidth,sHeight);//set form size
setVisible(true);//display screen
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bAppend)
{
String appText = taAppend.getText().trim();
if(appText.length() < 1)
{
JOptionPane.showMessageDialog(null, "You can't append an empty message!", "Error", JOptionPane.ERROR_MESSAGE);
}
else //append comments
{
holder.addToLog(ToDoHolder.LOG_COMMENT, appText);
taHistory.setText(holder.getLog());
taAppend.setText("");
taAppend.requestFocus();
main.refreshToDoDisplay();
System.out.println("boo");
}
}
if (e.getSource() == bClose)
{
dispose();
}
}
}
It is taHistory I mean, when "taHistory.setText(holder.getLog());" returns so many lines of text that they dont fit in the JTextArea, the text goes off the bottom of the JTextArea where you cant see it. I would like the vertical length of the JTextArea inside the JScrollPane to increase as needed so that all text can be viewed. I tried what you said but it doesn't change anything.
Thanks.
Whenever posting code you should use the code formatting tags so I don't get brain ache from looking at it.
And it should be compilable.
Try the following:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
import toDo.data.ToDoHolder;
public class LogToDo extends JFrame implements ActionListener {
private static final long serialVersionUID = -5545406794821718167;
private int sWidth = 500,sHeight = 620;
private ToDoHolder holder;
private JPanel panelHeader, panelHistory, panelAppend;
private JLabel lHeader, lHistory;
private JTextArea taHistory, taAppend;
private JButton bAppend, bClose;
private MainGui main;
public LogToDo(ToDoHolder holder, MainGui m)
{
super("To Do Log");//form heading
//create container to place components in:
Container container = getContentPane();
container.setLayout(new FlowLayout());//set flow layout
//store references
this.holder = holder;
main = m;
panelHeader = new JPanel();
panelHistory = new JPanel();
panelAppend = new JPanel();
panelHeader.setPreferredSize(new Dimension(470,50));
panelHistory.setPreferredSize(new Dimension(470,300));
panelAppend.setPreferredSize(new Dimension(470,205));
panelHeader.setBorder(new LineBorder(Color.BLACK));
panelHistory.setBorder(new LineBorder(Color.BLACK));
panelAppend.setBorder(new LineBorder(Color.BLACK));
/*
* header panel
*/
String desc = holder.getDescription();
if (desc.length() > 20)
{
desc = desc.substring(0,18) + "...";
}
lHeader = new JLabel("Log for : " + desc);
lHeader.setFont(new Font("Comic Sans MS" , Font.BOLD, 24));
panelHeader.add(lHeader);
/*
* history panel
*/
lHistory = new JLabel("Log History :");
taHistory = new JTextArea();
taHistory.setPreferredSize(new Dimension(410,5000));
taHistory.setLineWrap(true);
taHistory.setWrapStyleWord(true);
taHistory.setEditable(false);
taHistory.setText(holder.getLog());
JScrollPane jsp = new JScrollPane(taHistory);
jsp.setPreferredSize(new Dimension(450,260));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panelHistory.add(lHistory);
panelHistory.add(jsp, FlowLayout.CENTER);
/*
* append panel
*/
taAppend = new JTextArea();
taAppend.setPreferredSize(new Dimension(410,1000));
taAppend.setLineWrap(true);
taAppend.setWrapStyleWord(true);
bAppend = new JButton("Append");
bClose = new JButton("Close");
Dimension dButton = new Dimension(220,25);
bAppend.setPreferredSize(dButton);
bClose.setPreferredSize(dButton);
bAppend.addActionListener(this);
bClose.addActionListener(this);
jsp = new JScrollPane(taAppend);
jsp.setPreferredSize(new Dimension(450,160));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panelAppend.add(jsp);
panelAppend.add(bAppend);
panelAppend.add(bClose);
/*
* add panels to container
*/
container.add(panelHeader);
container.add(panelHistory);
container.add(panelAppend);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//get screen resoloution
setLocation((d.width-sWidth)/2, (d.height-sHeight)/2);//centre form
setSize(sWidth,sHeight);//set form size
setVisible(true);//display screen
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bAppend)
{
String appText = taAppend.getText().trim();
if(appText.length() < 1)
{
JOptionPane.showMessageDialog(null, "You can't append an empty message!", "Error", JOptionPane.ERROR_MESSAGE);
}
else //append comments
{
holder.addToLog(ToDoHolder.LOG_COMMENT, appText);
taHistory.setText(holder.getLog());
/**
* This is where you should be calling the methods to move the viewing area of the jTextAre
*/
int length = taAppend.getDocument().getLength();
taAppend.setCaretPosition(length);
taAppend.requestFocus();
main.refreshToDoDisplay();
System.out.println("boo");
}
}
if (e.getSource() == bClose)
{
dispose();
}
}
}
Sorry I am not familiar with how to use these forums i will bear that in mind in future.
I don't think you understand the problem (sorry if my explanation is poor), I will try to explain it step by step.
I have created a large JTextArea:
taHistory.setPreferredSize(new Dimension(410,5000));
I have placed it inside a smaller JScrollPane:
JScrollPane jsp = new JScrollPane(taHistory);
jsp.setPreferredSize(new Dimension(450,260));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
The user fills in a JTextArea called taAppend, then they click a button (called bAppend). The text appends on the end of the text in taHistory.
Although taHistory has a height of 5000, it will eventually not be large enough to show all the text (because more and more gets appended to it).
What I need is for the height of taHistory to increase when text is appended so that all the text is visible (and not off the bottom of the component).
i've created a runnable version of this class (nothing necessary cut out):
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class LogToDo extends JFrame implements ActionListener
{
private static final long serialVersionUID = -5545406794821718167;
private int sWidth = 500,sHeight = 620;
//private ToDoHolder holder;
private JPanel panelHeader, panelHistory, panelAppend;
private JLabel lHeader, lHistory;
private JTextArea taHistory, taAppend;
private JButton bAppend, bClose;
//private MainGui main;
public static void main(String[] args)
{
new LogToDo();
}
public LogToDo()
{
super("To Do Log");//form heading
//create container to place components in:
Container container = getContentPane();
container.setLayout(new FlowLayout());//set flow layout
//store references
//this.holder = holder;
//main = m;
panelHeader = new JPanel();
panelHistory = new JPanel();
panelAppend = new JPanel();
panelHeader.setPreferredSize(new Dimension(470,50));
panelHistory.setPreferredSize(new Dimension(470,300));
panelAppend.setPreferredSize(new Dimension(470,205));
panelHeader.setBorder(new LineBorder(Color.BLACK));
panelHistory.setBorder(new LineBorder(Color.BLACK));
panelAppend.setBorder(new LineBorder(Color.BLACK));
/*
* header panel
*/
//String desc = holder.getDescription();
/*if (desc.length() > 20)
{
desc = desc.substring(0,18) + "...";
}*/
lHeader = new JLabel("Log for : ");
lHeader.setFont(new Font("Comic Sans MS" , Font.BOLD, 24));
panelHeader.add(lHeader);
/*
* history panel
*/
lHistory = new JLabel("Log History :");
taHistory = new JTextArea();
taHistory.setPreferredSize(new Dimension(410,1000));
taHistory.setLineWrap(true);
taHistory.setWrapStyleWord(true);
taHistory.setEditable(false);
taHistory.setText("");
JScrollPane jsp = new JScrollPane(taHistory);
jsp.setPreferredSize(new Dimension(450,260));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panelHistory.add(lHistory);
panelHistory.add(jsp, FlowLayout.CENTER);
/*
* append panel
*/
taAppend = new JTextArea();
taAppend.setPreferredSize(new Dimension(410,1000));
taAppend.setLineWrap(true);
taAppend.setWrapStyleWord(true);
bAppend = new JButton("Append");
bClose = new JButton("Close");
Dimension dButton = new Dimension(220,25);
bAppend.setPreferredSize(dButton);
bClose.setPreferredSize(dButton);
bAppend.addActionListener(this);
bClose.addActionListener(this);
jsp = new JScrollPane(taAppend);
jsp.setPreferredSize(new Dimension(450,160));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panelAppend.add(jsp);
panelAppend.add(bAppend);
panelAppend.add(bClose);
/*
* add panels to container
*/
container.add(panelHeader);
container.add(panelHistory);
container.add(panelAppend);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//get screen resoloution
setLocation((d.width-sWidth)/2, (d.height-sHeight)/2);//centre form
setSize(sWidth,sHeight);//set form size
setVisible(true);//display screen
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bAppend)
{
String appText = taAppend.getText().trim();
if(appText.length() < 1)
{
JOptionPane.showMessageDialog(null, "You can't append an empty message!", "Error", JOptionPane.ERROR_MESSAGE);
}
else //append comments
{
//holder.addToLog(ToDoHolder.LOG_COMMENT, appText);
taHistory.setText(taHistory.getText() + "\n" + taAppend.getText());
taAppend.setText("");
int length = taHistory.getDocument().getLength();
taHistory.setCaretPosition(length);
taAppend.requestFocus();
//main.refreshToDoDisplay();
System.out.println("boo");
}
}
if (e.getSource() == bClose)
{
dispose();
}
}
}
If you keep appending text you will see it eventually goes off the bottom...
Don't set the preferred size of the text area.
Container contentPane=getContentPane();
JPanel panel=new JPanel();
JTextArea ta=new JTextArea(10,50);
JScrollPane scrollPane=new JScrollPane(ta);
JTextField tf=new JTextField(40);
contentPane.add(scrollPane,BorderLayout.NORTH);
panel.add(tf);
contentPane.add(panel,BorderLayout.SOUTH);
scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
}//this fuction makes the scrollbar movedown with every extra line
this is just a piece of code to show you as a example how it can be made
problem was fixed by getting rid of the code to set a preferred size of the text area - thanks.