Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop JTextArea from expanding

I'm trying to read information off of a data file and process it into a JTextArea for an about file for a term project that I'm making. The problem is that when I do scanner.nextLine() it's expanding the text area to fit the text and what I wanted was it to keep the size of the text area the same and if it runs out of space on the current line to start on the

    if(event.getSource() == forward)
    {
        text.setText(s.nextLine());
    }

The text area is setup like this:

     text = new JTextArea(11, 30); 

Basically what I want to know is if there's a method that text area offers (I looked) that relocates space to the next line if the current is full. I can most likely just edit the data file if there isn't a way to do it in the text area.

like image 676
Feek Avatar asked Feb 03 '26 17:02

Feek


1 Answers

Place the JTextArea within a JScrollPane and add the scroll pane to your container instead

text = new JTextArea(11, 30);
JScrollPane scrollpane = new JScrollPane(text);
//...
add(scrollpane);

See How to use Scroll Panes for more details

This will prevent the scroll pane from growing in the current container.

Also check out JTextArea#setLineWrap and JTextArea#setWrapStyleWord which will effect how the component treats text that would exceed the viewable width.

See How to use Text Areas for more details

like image 116
MadProgrammer Avatar answered Feb 06 '26 07:02

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!