2017-03-28 8 views
2

私は通常のHTMLEditorKit()オブジェクトがあります。HTMLEditorKitのコンテンツをフィルタリング/検索する方法はありますか?

public class Logger { 

    public static ArrayList<String[]> log = new ArrayList<String[]>(); 

    public static void update(String s) { 
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy | HH:mm:ss"); 
     String historyText = "<b>" + sdf.format(new Date()) + "</b>: " + s; 
     String[] sArray = { sdf.format(new Date()), s }; 
     log.add(sArray); 
     append(historyText); 
    } 

    public static void append(String s) { 
     MainFrame.history.setEditorKit(MainFrame.historyKit); 
     MainFrame.history.setDocument(MainFrame.historyDoc); 
     try { 
      MainFrame.historyKit.insertHTML(MainFrame.historyDoc, 
        MainFrame.historyDoc.getLength(), s, 0, 0, null); 
     } catch (BadLocationException | IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

事は、私である:それは、このオブジェクトに応じて更新されますので、私は、「ログ」のいくつかの並べ替えとして使用

 historyKit = new HTMLEditorKit(); 
     historyDoc = new HTMLDocument(); 
     history = new JEditorPane("text/html", ""); 
     JScrollPane historyScrollPane = new JScrollPane(history); 
     historyPanel.add(historyScrollPane, "cell 0 0 1 2,grow"); 
     history.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, 
       null, null)); 

をコンポーネント自体をトラバースするよりも、JTextFieldに基づいてコンテンツをフィルタリングするために、データ構造を「トラバース」する方が複雑になると考えています(その場合、モデルと思われます)。テキストフィールドを「検索フィールド」として使用して、文書をフィルタリングするよく知られた方法はありますか?

+0

JTextPaneを使用すると、HTMLタグを気にせずにテキストを検索できます。 – camickr

+0

私は、テキストのさまざまな部分を強調表示できるようにHTMLキットを使用しています(日付は太字など)。同じフォント/色のすべての文字列は本当に恐ろしいです(おそらく、私はもっと巧みになり、ラベルでいくつかの黒い魔法を使ったり、高度にカスタマイズされたJTableを使用していたかもしれませんが)。もちろん、プラグマティズムに有利な形で美学を落とすという選択肢は常にあります。 –

答えて

1

私は、テキストの異なる部分を強調することができるように、私はここでJTextPane異なるフォントを使用することができ、色など

をHTMLキットを使用している簡単な例でありますあなたは始めるために:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class TextPaneAttributes extends JPanel 
{ 

    public TextPaneAttributes() 
    { 
     setLayout(new BorderLayout()); 

     JTextPane textPane = new JTextPane(); 
     textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight"); 

     StyledDocument doc = textPane.getStyledDocument(); 

     // Define some character and paragraph attribute sets 

     SimpleAttributeSet keyWord = new SimpleAttributeSet(); 
     StyleConstants.setBold(keyWord, true); 

     SimpleAttributeSet green = new SimpleAttributeSet(); 
     StyleConstants.setForeground(green, Color.GREEN); 

     SimpleAttributeSet center = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); 

     SimpleAttributeSet left = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT); 

     // Change attributes on some existing text 

     doc.setCharacterAttributes(0, 3, keyWord, false); 
     doc.setCharacterAttributes(8, 5, green, true); 
     doc.setParagraphAttributes(20, 1 , center, false); 

     // Add some text with attributes 

     try 
     { 
      doc.insertString(doc.getLength(), "\nNormal text", null); 
      doc.insertString(doc.getLength(), "\nGreen text centered", green); 
      doc.setParagraphAttributes(doc.getLength(), 1 , center, false); 
      doc.insertString(doc.getLength(), "\nKeyword text", keyWord); 
      doc.setParagraphAttributes(doc.getLength(), 1 , left, false); 

      // Newly typed text at the end of the document will inherit the 
      // "keyword" attributes unless we remove the attributes 

      textPane.setCaretPosition(doc.getLength()); 
      textPane.getInputAttributes().removeAttributes(keyWord); 
     } 
     catch(Exception e) {} 

     // Add text pane to frame 

     JScrollPane scrollPane = new JScrollPane(textPane); 
     scrollPane.setPreferredSize(new Dimension(200, 250)); 
     add(scrollPane); 

     // Create a Button panel 

     JPanel buttons = new JPanel(); 
     add(buttons, BorderLayout.PAGE_END); 

     // Add a Bold button 

     JButton bold = new JButton(new StyledEditorKit.BoldAction()); 
     buttons.add(bold); 

     // Add Right Alignment button 

     JButton right = new JButton(new StyledEditorKit.AlignmentAction("Align Right", StyleConstants.ALIGN_RIGHT)); 
     buttons.add(right); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new TextPaneAttributes()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(() -> createAndShowGUI()); 
/* 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
*/ 
    } 
} 

の詳細と例についてText Component Features上のSwingのチュートリアルからのセクションをお読みください。

関連する問題