Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling scroll bars when JTextArea exceeds certain amount of lines

this is my first time using any StackExchange website, so let's see how it goes.

So I've been making a 'local' chat program in Java, and was using a JTextField for chat input. But I wanted to allow for multiline chatting so I switched to JTextArea. I'm using a GroupLayout (built with Window Builder Pro - eclipse) for easy window/component resizing. Here's a picture:

enter image description here

The JTabbedPane, the JTextArea and the Send button are all contained in a JPanel, and all the stuff to the left is in it's own JPanel. So I have the JTextArea and the button docked to the bottom of the right JPanel. The JTextArea is allowed to resize vertically, but the button isn't. I was able to get the JTextArea to grow vertically when I enter new lines, show below:

enter image description here

But I'm unable to think up a way so that if I enter a certain amount of lines into the JTextArea, scrollbars will appear and prevent the JTextArea from taking up any more space. So I tried wrapping the JTextArea in a JScrollPane but disable to scrollbars initially and then enable them when I needed the JTextArea to start scrolling, but I learned that if I wrap it, the JScrollPane won't grow but will still act like it would with the scrollbars visible but... without them. :/

** I wanted to put a link here, but StackOverflow doesn't like me ;)

So, I'm kind of stuck... Is there something that does this that I'm missing? I was thinking that I could just create two different GroupLayout objects, one with the scrollpane not even valid, and then other with the scrollpane valid but stuck at a certain size. On the keyPress listener I could check if the text area exceeds a certain limit, and then it would switch the layout for the panel? The inner JTextArea would still be the same object, but just different layout objects. Opinions on that approach?

Anyway, thanks in advance to all who take their time to answer this. :)

like image 276
Bradley Odell Avatar asked Feb 21 '12 00:02

Bradley Odell


People also ask

How do I stop JTextArea from expanding?

You can use JTextArea#setLineWrap(true) by default is set to false. Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped.

How many scroll bars are there in application window?

How many scroll bars are in a window? There is often only one scroll bar in a window, which is the vertical scroll bar that lets you scroll up and down in a window. Many programs automatically wrap the text in a window as you change the size of the window or zoom into the document.

What is the difference between a ScrollBar and a scrollPane?

A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.

How do I disable JTextArea?

If you would like to just disable text selection on any swing control such as JtextArea you can use the coding below: JtextArea. setHighlighter(null); This one line of coding will help disable the text selection and can be placed in the constructor or within a initialized method upon Frame execution.


1 Answers

I wrote a small program that resizes the JTextArea up to a maximum of 4 lines using only Swing controls

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ResizeTextArea {

    public static final int CHAT_ROW_LIMIT = 4;

    public static void main(String[] args) {
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(200, 200));
        topPanel.setBackground(Color.WHITE);

        final JTextArea chatArea = new JTextArea();
        final JScrollPane scrollPane = new JScrollPane(chatArea);

        final JPanel mainPanel = new JPanel(new BorderLayout(5,5));
        mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        mainPanel.add(topPanel, BorderLayout.CENTER);
        mainPanel.add(scrollPane, BorderLayout.SOUTH);

        chatArea.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLineCount();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateLineCount();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateLineCount();
            }

            private void updateLineCount() {
                int lineCount = chatArea.getLineCount();
                if (lineCount <= CHAT_ROW_LIMIT) {
                    chatArea.setRows(lineCount);
                    mainPanel.revalidate();
                }
            }
        });

        JFrame f = new JFrame("ResizeTextArea");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(mainPanel);
        f.pack();
        f.setVisible(true);
    }
}

Here is how it looks for 1 line, 4 lines, and 8 lines:

1 line4 lines8 lines

like image 78
ughzan Avatar answered Oct 10 '22 07:10

ughzan