2011-06-21 7 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を使用して、ドキュメントの長さを確認します。また、ドキュメントのgetText()メソッドを使用し、文字列内の "\ n"文字をカウントすることもできます。 また、ドキュメントのinsertString()メソッドをオーバーライドすることもできます。ラインの最大可能量が達成されている場合は、単に((削除)コールして、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); 
    } 
} 
関連する問題