Java 有没有办法过滤/搜索HTMLEditorKit中的内容?
我有一个普通的HTMLEditorKit()对象: 我将其用作某种“日志”,因此它会根据以下对象进行更新:Java 有没有办法过滤/搜索HTMLEditorKit中的内容?,java,swing,search,filter,Java,Swing,Search,Filter,我有一个普通的HTMLEditorKit()对象: 我将其用作某种“日志”,因此它会根据以下对象进行更新: public class Logger { public static ArrayList<String[]> log = new ArrayList<String[]>(); public static void update(String s) { SimpleDateFormat sdf = new SimpleDateFo
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();
}
}
}
公共类记录器{
public static ArrayList log=new ArrayList();
公共静态无效更新(字符串s){
SimpleDataFormat sdf=新的SimpleDataFormat(“dd/MM/yyyy | HH:MM:ss”);
字符串historyText=“+sdf.format(新日期())+”:“+s;
字符串[]sArray={sdf.format(new Date()),s};
log.add(sArray);
追加(历史文本);
}
公共静态void append(字符串s){
MainFrame.history.setEditorKit(MainFrame.historyKit);
MainFrame.history.setDocument(MainFrame.historyDoc);
试一试{
MainFrame.historyKit.insertHTML(MainFrame.historyDoc,
MainFrame.historyDoc.getLength(),s,0,0,null);
}捕获(BadLocationException | IOException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
问题是,我相信,为了根据某个JTextField过滤内容,“遍历”数据结构要比“遍历”组件本身(在这种情况下,我认为是模型)复杂得多。是否有一种众所周知的过滤文档的方法,使用文本字段作为“搜索字段”
我正在使用HTML工具包,以便突出显示文本的不同部分
JTextPane
可以与不同的字体、颜色等一起使用
下面是一个简单的示例,让您开始学习:
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();
}
});
*/
}
}
阅读Swing教程中关于的部分以了解更多信息和示例。使用JTextPane,您可以只搜索文本而不必担心HTML标记。我使用HTML工具包,以便突出显示文本的不同部分(例如,日期为粗体)。所有相同字体/颜色的字符串读起来都非常糟糕(我知道,也许我可以更聪明一些,用标签做一些黑色魔术,甚至使用高度定制的JTable,但无论如何)。当然,总有放弃美学而转向实用主义的选择。
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();
}
});
*/
}
}