Java JTextPane + JScrollPane:取消/激活自动滚动

发布于 2024-12-25 14:27:36 字数 7037 浏览 0 评论 0原文

我目前正在用 Java 编写一个简单的聊天,目前我陷入了这个问题: 我希望我的输出 JTextPane 的行为就像您期望它从良好的聊天中获得的那样,即默认情况下,当新文本到达时文本会自动滚动(使用 outputfield.setCaretPosition(outputdocument.getLength())),但是当用户向上滚动时应该被禁用,当然当用户再次滚动到底部时重新启用。

我尝试摆弄 ScrollBars 等,但我似乎找不到一种方法来检测 ScrollBar 是否位于底部。

我只需创建一个 JTextPane、一个 JScrollPane 并将一个放入另一个中即可创建可滚动输出区域。

JTextPane outputpane = new JTextPane
...
JScrollPane outputscroll = new JScrollPane(outputpane);
outputscroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

编辑:这是我用来创建组件并显示新消息的代码:

// lots of imports


public class RoomFrame extends JFrame implements ListSelectionListener, ActionListener, Comparable, Runnable
{   
public static final String DEFAULT_FONT = "Courier";
public static final int DEFAULT_FONT_SIZE = 12;

private JTextPane mOutputField;
private JScrollPane mOutputScroll;

private StyledDocument mOutputDocument;


public RoomFrame(...)
{
    super(...);

    createGUI();
    setOutputStyles();
    setVisible(true);

    new Thread(this).start();
}

// ========================================================

private void createGUI()
{
    Color borderhighlightouter = new Color(220, 220, 220);
    Color borderhighlightinner = new Color(170, 170, 170);
    Color bordershadowouter = new Color(120, 120, 120);
    Color bordershadowinner = new Color(170, 170, 170);

    setLayout(new GridBagLayout());
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(500, 400);


    // -----------------------------
    // Output
    mOutputField = new JTextPane();
    mOutputField.setEditable(false);
    mOutputField.setBackground(new Color(245, 245, 245));
    mOutputDocument = mOutputField.getStyledDocument();

    mOutputScroll = new JScrollPane(mOutputField);

    mOutputScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    mOutputScroll.setPreferredSize(new Dimension(250, 250));
    mOutputScroll.setMinimumSize(new Dimension(50, 50));
    mOutputScroll.setOpaque(false);
    mOutputScroll.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0), BorderFactory.createBevelBorder(BevelBorder.LOWERED, borderhighlightouter, borderhighlightinner, bordershadowouter, bordershadowinner)));

    getContentPane().add(mOutputScroll);
}

private void setOutputStyles()
{
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

    Style regular = mOutputDocument.addStyle("regular", def);
    StyleConstants.setFontFamily(def, DEFAULT_FONT);
    StyleConstants.setFontSize(def, DEFAULT_FONT_SIZE);
    StyleConstants.setFirstLineIndent(def, 100.0f);

    Style nickname = mOutputDocument.addStyle("username", def);
    StyleConstants.setBold(nickname, true);
    StyleConstants.setForeground(nickname, new Color(50, 50, 220));

    Style highlight = mOutputDocument.addStyle("highlight", def);
    StyleConstants.setBold(highlight, true);
    StyleConstants.setBackground(highlight, new Color(150, 0, 0));

    Style join = mOutputDocument.addStyle("join", def);
    StyleConstants.setBold(join, true);
    StyleConstants.setForeground(join, new Color(20, 100, 20));

    Style leave = mOutputDocument.addStyle("leave", def);
    StyleConstants.setBold(leave, true);
    StyleConstants.setForeground(leave, new Color(100, 100, 20));

    Style topic = mOutputDocument.addStyle("topic", def);
    StyleConstants.setBold(topic, true);
    StyleConstants.setUnderline(topic, true);

    Style error = mOutputDocument.addStyle("error", def);
    StyleConstants.setBold(error, true);
    StyleConstants.setForeground(error, new Color(255, 0, 0));

    Style kick = mOutputDocument.addStyle("kick", def);
    StyleConstants.setBold(kick, true);
    StyleConstants.setForeground(kick, new Color(150, 0, 0));
}

private final boolean shouldScroll()
{
    int min = mOutputScroll.getVerticalScrollBar().getValue() + mOutputScroll.getVerticalScrollBar().getVisibleAmount();
    int max = mOutputScroll.getVerticalScrollBar().getMaximum();

    return min == max;
}

// ========================================================

public void displayMessage(String message)
{
    displayMessage(message, "");
}

public void displayMessage(String message, String style)
{
    displayMessage(message, style, true);
}

public void displayMessage(String message, String style, boolean addnewline)
{
    String newline = (addnewline ? "\n" : "");

    style = (style.equals("") ? "regular" : style);
    message = message.replace("\n", " ");

    // check for highlight

    try
    {
        mOutputDocument.insertString(mOutputDocument.getLength(),
                                        String.format("%s%s", message, newline),
                                        mOutputDocument.getStyle(style));
    }
    catch (Exception e) {}

    // if (shouldScroll())
    //  mOutputField.setCaretPosition(mOutputDocument.getLength());
}

public void run()
{
    while (true)
    {
        if (shouldScroll())
        {
            SwingUtilities.invokeLater(
                new Runnable()
                {
                    public void run()
                    {
                        mOutputScroll.getVerticalScrollBar().setValue(mOutputScroll.getVerticalScrollBar().getMaximum());
                    }
                });
        }

        try { Thread.sleep(500); }
        catch (InterruptedException e) { break; }
    }
}

public void valueChanged(ListSelectionEvent event)
{

}

public void actionPerformed(ActionEvent event)
{

}

public final int compareTo(Object o) { return this.toString().compareTo(o.toString()); }
}


编辑:感谢fireshadow52的链接到另一个类似的问题我终于让它按照我想要的方式工作:

private boolean isViewAtBottom()
{
    JScrollBar sb = mOutputScroll.getVerticalScrollBar();
    int min = sb.getValue() + sb.getVisibleAmount();
    int max = sb.getMaximum();
    System.out.println(min + " " + max);
    return min == max;
}

private void scrollToBottom()
{
    SwingUtilities.invokeLater(
        new Runnable()
        {
            public void run()
            {
                mOutputScroll.getVerticalScrollBar().setValue(mOutputScroll.getVerticalScrollBar().getMaximum());
            }
        });
}

public void displayMessage(String message, String style, boolean prependnewline)
{
    String newline = (prependnewline ? "\n" : "");
    boolean scroll = isViewAtBottom() && prependnewline;

    style = (style.equals("") ? "regular" : style);
    message = message.replace("\n", " ");

    try
    {
        mOutputDocument.insertString(mOutputDocument.getLength(),
                                        String.format("%s%s", newline, message),
                                        mOutputDocument.getStyle(style));
    }
    catch (Exception e) {}

    if (scroll)
        scrollToBottom();
}

再次感谢您的所有帮助!

I'm currently writing a simple chat in Java and currently I'm stuck on this problem:
I want my output JTextPane to behave like you would expect it to from a good chat, ie by default the text scrolls automatically when new text arrives (using outputfield.setCaretPosition(outputdocument.getLength())), but when the user scrolls up this should be disabled and of course re-enabled when the user scrolls to the bottom again.

I tried toying around with the ScrollBars and all, but I can't seem to find a way to detect whether the ScrollBar is at the bottom or not.

I create the scrollable output area simply by creating a JTextPane, a JScrollPane and put one into the other.

JTextPane outputpane = new JTextPane
...
JScrollPane outputscroll = new JScrollPane(outputpane);
outputscroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

Edit: Here's the code I use to create the components and display new messages:

// lots of imports


public class RoomFrame extends JFrame implements ListSelectionListener, ActionListener, Comparable, Runnable
{   
public static final String DEFAULT_FONT = "Courier";
public static final int DEFAULT_FONT_SIZE = 12;

private JTextPane mOutputField;
private JScrollPane mOutputScroll;

private StyledDocument mOutputDocument;


public RoomFrame(...)
{
    super(...);

    createGUI();
    setOutputStyles();
    setVisible(true);

    new Thread(this).start();
}

// ========================================================

private void createGUI()
{
    Color borderhighlightouter = new Color(220, 220, 220);
    Color borderhighlightinner = new Color(170, 170, 170);
    Color bordershadowouter = new Color(120, 120, 120);
    Color bordershadowinner = new Color(170, 170, 170);

    setLayout(new GridBagLayout());
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(500, 400);


    // -----------------------------
    // Output
    mOutputField = new JTextPane();
    mOutputField.setEditable(false);
    mOutputField.setBackground(new Color(245, 245, 245));
    mOutputDocument = mOutputField.getStyledDocument();

    mOutputScroll = new JScrollPane(mOutputField);

    mOutputScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    mOutputScroll.setPreferredSize(new Dimension(250, 250));
    mOutputScroll.setMinimumSize(new Dimension(50, 50));
    mOutputScroll.setOpaque(false);
    mOutputScroll.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0), BorderFactory.createBevelBorder(BevelBorder.LOWERED, borderhighlightouter, borderhighlightinner, bordershadowouter, bordershadowinner)));

    getContentPane().add(mOutputScroll);
}

private void setOutputStyles()
{
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

    Style regular = mOutputDocument.addStyle("regular", def);
    StyleConstants.setFontFamily(def, DEFAULT_FONT);
    StyleConstants.setFontSize(def, DEFAULT_FONT_SIZE);
    StyleConstants.setFirstLineIndent(def, 100.0f);

    Style nickname = mOutputDocument.addStyle("username", def);
    StyleConstants.setBold(nickname, true);
    StyleConstants.setForeground(nickname, new Color(50, 50, 220));

    Style highlight = mOutputDocument.addStyle("highlight", def);
    StyleConstants.setBold(highlight, true);
    StyleConstants.setBackground(highlight, new Color(150, 0, 0));

    Style join = mOutputDocument.addStyle("join", def);
    StyleConstants.setBold(join, true);
    StyleConstants.setForeground(join, new Color(20, 100, 20));

    Style leave = mOutputDocument.addStyle("leave", def);
    StyleConstants.setBold(leave, true);
    StyleConstants.setForeground(leave, new Color(100, 100, 20));

    Style topic = mOutputDocument.addStyle("topic", def);
    StyleConstants.setBold(topic, true);
    StyleConstants.setUnderline(topic, true);

    Style error = mOutputDocument.addStyle("error", def);
    StyleConstants.setBold(error, true);
    StyleConstants.setForeground(error, new Color(255, 0, 0));

    Style kick = mOutputDocument.addStyle("kick", def);
    StyleConstants.setBold(kick, true);
    StyleConstants.setForeground(kick, new Color(150, 0, 0));
}

private final boolean shouldScroll()
{
    int min = mOutputScroll.getVerticalScrollBar().getValue() + mOutputScroll.getVerticalScrollBar().getVisibleAmount();
    int max = mOutputScroll.getVerticalScrollBar().getMaximum();

    return min == max;
}

// ========================================================

public void displayMessage(String message)
{
    displayMessage(message, "");
}

public void displayMessage(String message, String style)
{
    displayMessage(message, style, true);
}

public void displayMessage(String message, String style, boolean addnewline)
{
    String newline = (addnewline ? "\n" : "");

    style = (style.equals("") ? "regular" : style);
    message = message.replace("\n", " ");

    // check for highlight

    try
    {
        mOutputDocument.insertString(mOutputDocument.getLength(),
                                        String.format("%s%s", message, newline),
                                        mOutputDocument.getStyle(style));
    }
    catch (Exception e) {}

    // if (shouldScroll())
    //  mOutputField.setCaretPosition(mOutputDocument.getLength());
}

public void run()
{
    while (true)
    {
        if (shouldScroll())
        {
            SwingUtilities.invokeLater(
                new Runnable()
                {
                    public void run()
                    {
                        mOutputScroll.getVerticalScrollBar().setValue(mOutputScroll.getVerticalScrollBar().getMaximum());
                    }
                });
        }

        try { Thread.sleep(500); }
        catch (InterruptedException e) { break; }
    }
}

public void valueChanged(ListSelectionEvent event)
{

}

public void actionPerformed(ActionEvent event)
{

}

public final int compareTo(Object o) { return this.toString().compareTo(o.toString()); }
}

Edit: Thanks to fireshadow52's link to another similar question I finally got it to work exactly how I want it to:

private boolean isViewAtBottom()
{
    JScrollBar sb = mOutputScroll.getVerticalScrollBar();
    int min = sb.getValue() + sb.getVisibleAmount();
    int max = sb.getMaximum();
    System.out.println(min + " " + max);
    return min == max;
}

private void scrollToBottom()
{
    SwingUtilities.invokeLater(
        new Runnable()
        {
            public void run()
            {
                mOutputScroll.getVerticalScrollBar().setValue(mOutputScroll.getVerticalScrollBar().getMaximum());
            }
        });
}

public void displayMessage(String message, String style, boolean prependnewline)
{
    String newline = (prependnewline ? "\n" : "");
    boolean scroll = isViewAtBottom() && prependnewline;

    style = (style.equals("") ? "regular" : style);
    message = message.replace("\n", " ");

    try
    {
        mOutputDocument.insertString(mOutputDocument.getLength(),
                                        String.format("%s%s", newline, message),
                                        mOutputDocument.getStyle(style));
    }
    catch (Exception e) {}

    if (scroll)
        scrollToBottom();
}

Again, thanks for all your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓬勃野心 2025-01-01 14:27:36

如果您只想在底部时滚动,那么这应该有帮助。

使用此方法检查滚动条是否位于末尾(或底部),如果是,则使用 setCaretPosition(Component.getDocument().getLength()); 自动向下滚动:

public boolean shouldScroll() {
    int minimumValue = scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getVisibleAmount();
    int maximumValue = scrollPane.getVerticalScrollBar().getMaximum();
    return maximumValue == minimumValue;
}

我发现使用 Google 时得到了类似的结果,这使我找到了一种与此类似的方法,该方法按要求工作。

编辑:确保它是在 invokeLater() 内完成的,因为它需要在滚动完成之前更新。

我使用的完整示例:

public class ScrollTest extends Thread {

    private JFrame frame;

    private JTextArea textArea;

    private JScrollPane scrollPane;

    public static void main(String[] arguments) {
            new ScrollTest().run();
    }

    public ScrollTest() {
            textArea = new JTextArea(20, 20);
            scrollPane = new JScrollPane(textArea);

            frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(scrollPane);
            frame.pack();
            frame.setVisible(true);
    }

    public void run() {
            while (true) {
                    textArea.append("" + Math.random() + "\n");
                    if (shouldScroll()) {
                            SwingUtilities.invokeLater(new Runnable() {

                                    @Override
                                    public void run() {
                                            scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
                                    }

                            });
                    }
                    try {
                            Thread.sleep(500);
                    } catch (Exception exception) {
                            exception.printStackTrace();
                    }
            }
    }

    public boolean shouldScroll() {
            int minimumValue = scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getVisibleAmount();
            int maximumValue = scrollPane.getVerticalScrollBar().getMaximum();
            return maximumValue == minimumValue;
    }

}

If you're only wanting to scroll when you're at the bottom then this should help.

Use this method to check and see if the scrollbar is at the end (or bottom), and if so, scroll down automatically using setCaretPosition(Component.getDocument().getLength());:

public boolean shouldScroll() {
    int minimumValue = scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getVisibleAmount();
    int maximumValue = scrollPane.getVerticalScrollBar().getMaximum();
    return maximumValue == minimumValue;
}

I found similar results when using Google which led me to a method similar to this one which worked as requested.

Edit: Make sure that it is done within invokeLater() as it needs to be updated before scrolling is done.

A full example of what I use:

public class ScrollTest extends Thread {

    private JFrame frame;

    private JTextArea textArea;

    private JScrollPane scrollPane;

    public static void main(String[] arguments) {
            new ScrollTest().run();
    }

    public ScrollTest() {
            textArea = new JTextArea(20, 20);
            scrollPane = new JScrollPane(textArea);

            frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(scrollPane);
            frame.pack();
            frame.setVisible(true);
    }

    public void run() {
            while (true) {
                    textArea.append("" + Math.random() + "\n");
                    if (shouldScroll()) {
                            SwingUtilities.invokeLater(new Runnable() {

                                    @Override
                                    public void run() {
                                            scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
                                    }

                            });
                    }
                    try {
                            Thread.sleep(500);
                    } catch (Exception exception) {
                            exception.printStackTrace();
                    }
            }
    }

    public boolean shouldScroll() {
            int minimumValue = scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getVisibleAmount();
            int maximumValue = scrollPane.getVerticalScrollBar().getMaximum();
            return maximumValue == minimumValue;
    }

}

身边 2025-01-01 14:27:36

JScrollBar 有 getValue() 方法。只需在 SwingUtilities.invokeLater() 中扭曲它即可在文本附加后调用。

JScrollBar has getValue() method. Just warp it in SwingUtilities.invokeLater() to be called after text appending.

久夏青 2025-01-01 14:27:36

您可以使用 AdjustmentListener来条件滚动,如本示例所示。

You can use an AdjustmentListener to condtion scrolling, as shown in this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文