保持JScrollPane为Java的相同长度 [英] Keep JScrollPane as same length java

查看:70
本文介绍了保持JScrollPane为Java的相同长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个JScrollPane,每个JScrollPane都包含一个JTextArea. JTextArea填充有不断更新的变量列表.

I have a several JScrollPanes that contains a single JTextArea each. The JTextAreas are populated with a list of variables that are constantly updated.

我有一个ScheduledExecutorService,它会不断清除JTextArea s并替换变量.对于大多数JScrollPan es,更改是非常无缝的,并且列表中包含的数字似乎正在以平滑的方式更改.

I have a ScheduledExecutorService that constantly clears the JTextAreas and replaces the variables. For most the the JScrollPanes, the change are very seamless, and it looks like the numbers contained in the lists are changing in a smooth manner.

有一个JScrollPane发出了问题.它会不断发出闪光(文本区域和滚动条变为空白并重新出现),这与我正在重新绘制的内容有关(我应该注意,即使其他窗格的更新方式完全相同,它们也不会遇到此问题) ).滚动条也将疯狂滚动(返回顶部).总的来说看起来很凌乱.这个特定的JScrollPane也是最长的,因此我认为这是原因.此特定窗格与其他窗格没有其他区别.

There is one JScrollPane that is giving an issue. It will constantly give off a flash (the text area and scroll bar go blank and reappear) which I correlate to it being repainted (I should note that even though the other panes get updated in the exact same manner, they do not encounter this problem). The scroll bar also will scroll wildly (back to the top); all in all looking very messy. This particular JScrollPane is also the longest so I assume that is the reason. There isn't anything else different about this particular pane from the others.

除非有人有更好的解决方案,否则我相信最好的选择是将插入符号设置为 NEVER_UPDATE ,但是为了使此功能正常运行,JScrollPane即使我将所有文字都排除为空,也必须保持相同的大小.

Unless someone has a better solution, I believe my best bet is to set the Caret to NEVER_UPDATE, but in order for this to work, the JScrollPane would have to remain the same size, even when I null out all of the text.

有人知道该怎么做吗?为了澄清起见,我不想使窗格变大,即使窗格为空,我也希望能够向下滚动到设置的长度.

Does anyone know how to do this? To clarify, I don't want to make the pane larger, I want to essentially be able to scroll down to a set length, even if the pane is empty.

推荐答案

我不确定这是否是您要寻找的东西,但是在过去,我创建了一个JTextPane,该JTextPane会随着异常消息而不断更新,每种颜色编码.它具有一个滚动窗格和一个文本大小缓冲区,因此到达后,它将从头开始删除添加到末尾的文本量.

I'm not sure if this is what you're looking for, but in the past I've created a JTextPane that was constantly updated with exception messages, each colour coded. It had a scroll pane, and a text size buffer so when reached, it will remove from the start the amount of text appended to the end.

每次我遇到异常时,我都会执行此操作:

Every time I caught an exception, I ran this:

printExceptionMessage(new TCException(ex));

依次为:

protected void printExceptionMessage(final TCException exception) {

    SwingUtilities.invokeLater(
        new Runnable() {
            public void run() {

                Date recordDate = exception.getTimeStamp();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z");
                String recordTime = sdf.format(recordDate);

                String message = "(" + recordTime + ") EXCEPTION:   " + exception.getMessage() + "\n";

                statusColorTextPane.setEditable(true);

                // trim the textPane to the buffer size
                Document doc = statusColorTextPane.getStyledDocument();
                int overLength = doc.getLength() + message.length() - Foo.TEXT_BUFFER_SIZE;

                if (overLength > 0) {
                    try {
                        doc.remove(0, overLength);
                    } catch (Exception ex) {

                        System.err.println("Error occured while trimming the textPane.");
                        ex.printStackTrace();
                    }
                }
                statusColorTextPane.append(Color.red, message);
                statusColorTextPane.setEditable(false);
            }
        });     
}

在这种情况下,statusColorTextPane是JTextPane的扩展类.它需要以下两种方法:

In this case, statusColorTextPane is an extended class of JTextPane. It needs these two methods in it:

public void appendNaive(Color c, String s) { // naive implementation

    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);

    int len = getText().length();
    setCaretPosition(len); // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
}

public void append(Color c, String s) { // better implementation--uses StyleContext

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                                    StyleConstants.Foreground, c);

    int len = getDocument().getLength(); // same value as getText().length();
    setCaretPosition(len);  // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s); // there is no selection, so inserts at caret
}

对我来说,Foo.TEXT_BUFFER_SIZE已设置为20000.

Foo.TEXT_BUFFER_SIZE for me I have set to 20000.

这篇关于保持JScrollPane为Java的相同长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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