2011-06-21 46 views
2

我正在使用JTextPane(在JScrollPane中)作爲自定義日誌記錄系統的一部分。 (我需要多色輸出,所以不能使用JTextArea。)JTextPane - 如何創建滾動日誌

我有它的日誌記錄部分工作,但我現在需要能夠限制其內容,以便它不僅在內存中不斷增長。

沒有直接的用戶輸入,因爲所有日誌都是系統生成的。

我需要做的就是確定JTextPane何時達到了指定的行數,然後能夠在超過最大值時刪除第一行。這將允許我保持顯示屏中最後'x'行的緩衝區。

我該怎麼做呢?

+0

看到這個http://stackoverflow.com/q/102171/307767 – oliholz

+0

我已經看到那篇文章,並沒有解決我的問題。我需要能夠在整個日誌超過多行時刪除一行。 – Dave

+0

看到我的答案[這裏](http://stackoverflow.com/questions/6316272/)。我相信Message Console符合您的要求。 –

回答

3

使用DocumentFilter並檢查文檔的長度。您也可以使用Document的getText()方法並計算字符串中的「\ n」字符。 或者你可以重寫文檔的insertString()方法。如果達到行的最大可能的量只是調用remove(),然後super.insertString()

1

試試這個小例子:

public class Example { 

    private static int MAX = 7; 

    static public void main(String[] s) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { 

     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

     JFrame frame = new JFrame(); 
     frame.setBounds(50, 50, 200, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JTextPane pane = new JTextPane(); 

     pane.setText("1\n2\n3\n4"); 

     JPanel pnl = new JPanel(new BorderLayout()); 
     pnl.add(pane, BorderLayout.CENTER); 

     pane.getDocument().addDocumentListener(new DocumentListener() { 
      public void removeUpdate(DocumentEvent e) { 
      } 
      public void insertUpdate(DocumentEvent e) { 
       SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
         try { 
          View baseView = pane.getUI().getRootView(pane); 
          View root = baseView.getView(0); 
          for(int i = 0; i < root.getViewCount()-MAX; i++) { 
           int line = root.getViewIndex(i, Bias.Forward); 
           View lineview = root.getView(line); 
           pane.getDocument().remove(lineview.getStartOffset(), lineview.getEndOffset()); 
          } 
         } catch(BadLocationException e1) { 
          e1.printStackTrace(); 
         } 
        } 
       }); 
      } 
      public void changedUpdate(DocumentEvent e) { 
      } 
     }); 

     pnl.add(new JButton(new AbstractAction("Delete") { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        View baseView = pane.getUI().getRootView(pane); 
        View root = baseView.getView(0); 
        int line = root.getViewIndex(0, Bias.Forward); 
        View lineview = root.getView(line); 
        pane.getDocument().remove(lineview.getStartOffset(), lineview.getEndOffset()); 
       } catch(BadLocationException e1) { 
        e1.printStackTrace(); 
       } 
      } 
     }), BorderLayout.SOUTH); 

     pnl.add(new JButton(new AbstractAction("Add") { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        pane.getDocument().insertString(pane.getDocument().getEndPosition().getOffset(), new SimpleDateFormat("ss").format(new Date())+": This is a new line\n", null); 
       } catch(BadLocationException e1) { 
        e1.printStackTrace(); 
       } 
      } 
     }), BorderLayout.NORTH); 
     frame.setContentPane(pnl); 
     frame.setVisible(true); 
    } 
}