Java 如何防止jscrollpane滚动到底部?

Java 如何防止jscrollpane滚动到底部?,java,swing,jscrollpane,jtextfield,Java,Swing,Jscrollpane,Jtextfield,我在JScrollPane(logScrollPane)元素中有JTextPane(log)。日志内容设置为“text/html” 我创建了一个附加此日志的方法,如下所示: public void appendLog(String someHTMLText) { HTMLDocument doc = (HTMLDocument) log.getDocument(); HTMLEditorKit editorKit = (HTMLEditorKit) log.getEditor

我在JScrollPane(logScrollPane)元素中有JTextPane(log)。日志内容设置为“text/html”

我创建了一个附加此日志的方法,如下所示:

public void appendLog(String someHTMLText)
  {
    HTMLDocument doc = (HTMLDocument) log.getDocument();
    HTMLEditorKit editorKit = (HTMLEditorKit) log.getEditorKit();
    try
      {
        editorKit.insertHTML(doc, doc.getLength(), someHTMLText, 0, 0, null);
      }
    catch (BadLocationException | IOException ex)
      {
        // handle exceptions
      }
  }
public void appendLog(String someHTMLText, boolean scroll)
  {
    if(scroll)
     {
        /* 
         * append log and set VerticalScrollBar to the bottom by
         * log.setCaretPosition(log.getDocument().getLength());
        */
     }
     else
     {
        // append log BUT make VerticalScrollBar stay at it's previous position
     }
  }
我想改进这个方法,并强制logScrollPane的垂直滚动条根据附加的布尔参数将\u移动到\u底部/停留在\u位置。 最后一种方法应如下所示:

public void appendLog(String someHTMLText)
  {
    HTMLDocument doc = (HTMLDocument) log.getDocument();
    HTMLEditorKit editorKit = (HTMLEditorKit) log.getEditorKit();
    try
      {
        editorKit.insertHTML(doc, doc.getLength(), someHTMLText, 0, 0, null);
      }
    catch (BadLocationException | IOException ex)
      {
        // handle exceptions
      }
  }
public void appendLog(String someHTMLText, boolean scroll)
  {
    if(scroll)
     {
        /* 
         * append log and set VerticalScrollBar to the bottom by
         * log.setCaretPosition(log.getDocument().getLength());
        */
     }
     else
     {
        // append log BUT make VerticalScrollBar stay at it's previous position
     }
  }

有什么建议吗?:)

这里有一些类似于我想实现这一目标时所做的事情。关键是改变scrollRectToVisible的行为

public class Docker extends JFrame {

    boolean dockScrollbar = true;
    MYTextPane textPane = new MYTextPane();
    JScrollPane sp = new JScrollPane(textPane);

    Docker() {

        JCheckBox scrollbarDockCB = new JCheckBox("Dock scrollbar");
        scrollbarDockCB.addItemListener(new DockScrollbarListener());
        scrollbarDockCB.setSelected(true);

        JButton insertText = new JButton("Insert text");
        insertText.addActionListener(new TextInserter());

        getContentPane().add(insertText, BorderLayout.PAGE_START);
        getContentPane().add(sp);
        getContentPane().add(scrollbarDockCB, BorderLayout.PAGE_END);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class MYTextPane extends JTextPane {

        MYTextPane() {

            setEditorKit(new HTMLEditorKit());
        }

        @Override
        public void scrollRectToVisible(Rectangle aRect) {

            if (dockScrollbar)
                super.scrollRectToVisible(aRect);
        }

        void insertText(String msg) {

            HTMLEditorKit kit = (HTMLEditorKit) getEditorKit();
            HTMLDocument doc = (HTMLDocument) getDocument();
            try {
                kit.insertHTML(doc, doc.getLength(), msg, 0, 0, null);
            } catch (BadLocationException | IOException e1) {
                e1.printStackTrace();
            }
            setCaretPosition(doc.getLength());
        }
    }

    class TextInserter implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            textPane.insertText("AAA\n");
        }
    }

    class DockScrollbarListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {

            if (e.getStateChange() == ItemEvent.SELECTED) {
                dockScrollbar = true;
                JScrollBar sb = sp.getVerticalScrollBar();
                sb.setValue(sb.getMaximum());
            }
            else if (e.getStateChange() == ItemEvent.DESELECTED)
                dockScrollbar = false;
        }
    }

    public static void main(String[] args) {

        new Docker();
    }
}
注释

  • 当您手动滚动我的代码时,我将停靠设置为false,您也可以将其添加到此处。通过向垂直滚动条添加鼠标侦听器
  • 我将
    布尔dockScrollbar
    作为文本窗格的一个字段,因为我有多个字段
  • 我没有用于
    JScrollPane
    的字段,而是通过文本窗格获得它

我从未在JEditorPane上尝试过,但您应该能够使用
插入符号更新策略来控制此操作


查看了解更多信息。

为什么JTextField(指定为单文本和短文本)、使用JTextArea表示纯文本、JTextPane如果您想进行装饰我太累了,没有注意到我键入了JTextField。我使用JTabPANE OFC:)@ SababieSZZADNIK,如果任何一个答案都解决了你的问题,请考虑接受它,或者投票支持有帮助的问题。