【发布时间】:2017-08-20 22:28:02
【问题描述】:
我有一个普通的 HTMLEditorKit() 对象:
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));
我将它用作某种“日志”,因此它会根据此对象进行更新:
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();
}
}
}
问题是,我相信“遍历”数据结构以便根据某些 JTextField 过滤内容比“遍历”组件本身(在这种情况下,模型,我想)。是否有一种众所周知的过滤文档的方法,使用文本字段作为“搜索字段”?
【问题讨论】:
-
使用 JTextPane 然后你可以只搜索文本而不用担心 HTML 标签。
-
我正在使用 HTML 工具包,以便我可以突出显示文本的不同部分(例如,日期是粗体)。相同字体/颜色的所有字符串真的很难读(我知道,也许我可以更聪明一些,用标签做一些黑魔法,或者甚至使用高度定制的 JTable,但无论如何)。当然,总是可以选择放弃美学而支持实用主义。