当JTextArea超过一定数量的行时启用滚动条 [英] Enabling scroll bars when JTextArea exceeds certain amount of lines

查看:931
本文介绍了当JTextArea超过一定数量的行时启用滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用任何StackExchange网站,所以让我们看看它是怎么回事。

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

所以我一直用Java制作一个'本地'聊天程序,并使用JTextField进行聊天输入。但我想允许多行聊天,所以我切换到JTextArea。我正在使用GroupLayout(使用Window Builder Pro-eclipse构建)来轻松调整窗口/组件大小。这是一张图片:

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:

JTabbedPane,JTextArea和Send按钮都包含在JPanel中,左边的所有内容都在它自己的JPanel中。所以我有JTextArea和按钮停靠在右侧JPanel的底部。允许JTextArea垂直调整大小,但按钮不允许。
当我输入新行时,我能够让JTextArea垂直增长,如下所示:

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:

但我无法想办法让我如果输入某个进入JTextArea的行数量,将出现滚动条并阻止JTextArea占用更多空间。
所以我尝试将JTextArea包装在JScrollPane中,但最初禁用滚动条,然后在我需要JTextArea开始滚动时启用它们,但我了解到如果我将它包装起来,JScrollPane将不会增长但仍会行动就像滚动条可见,但......没有它们。 :/

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. :/

**我想在这里放一个链接,但StackOverflow不喜欢我;)

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

所以,我有点被卡住了......
有没有这样的东西能让我失踪?
我以为我可以创建两个不同的GroupLayout对象,一个滚动窗格甚至没有效果,然后另一个滚动窗格有效,但卡在一定大小。在keyPress监听器上,我可以检查文本区域是否超过某个限制,然后它会切换面板的布局?内部JTextArea仍然是同一个对象,但只是不同的布局对象。关于这种方法的意见?

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

推荐答案

我写了一个小程序,只使用Swing控件将JTextArea的大小调整为最多4行

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);
    }
}

以下是1行,4行的查找方式,和8行:

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

这篇关于当JTextArea超过一定数量的行时启用滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆