- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试运行此代码:
How to change the color of specific words in a JTextPane?
private final class CustomDocumentFilter extends DocumentFilter
{
private final StyledDocument styledDocument = yourTextPane.getStyledDocument();
private final StyleContext styleContext = StyleContext.getDefaultStyleContext();
private final AttributeSet greenAttributeSet = styleContext.addAttribute(styleContext.getEmptySet(), StyleConstants.Foreground, Color.GREEN);
private final AttributeSet blackAttributeSet = styleContext.addAttribute(styleContext.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
// Use a regular expression to find the words you are looking for
Pattern pattern = buildPattern();
@Override
public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributeSet) throws BadLocationException {
super.insertString(fb, offset, text, attributeSet);
handleTextChanged();
}
@Override
public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
super.remove(fb, offset, length);
handleTextChanged();
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributeSet) throws BadLocationException {
super.replace(fb, offset, length, text, attributeSet);
handleTextChanged();
}
/**
* Runs your updates later, not during the event notification.
*/
private void handleTextChanged()
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
updateTextStyles();
}
});
}
/**
* Build the regular expression that looks for the whole word of each word that you wish to find. The "\\b" is the beginning or end of a word boundary. The "|" is a regex "or" operator.
* @return
*/
private Pattern buildPattern()
{
StringBuilder sb = new StringBuilder();
for (String token : ALL_WORDS_THAT_YOU_WANT_TO_FIND) {
sb.append("\\b"); // Start of word boundary
sb.append(token);
sb.append("\\b|"); // End of word boundary and an or for the next word
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1); // Remove the trailing "|"
}
Pattern p = Pattern.compile(sb.toString());
return p;
}
private void updateTextStyles()
{
// Clear existing styles
styledDocument.setCharacterAttributes(0, yourTextPane.getText().length(), blackAttributeSet, true);
// Look for tokens and highlight them
Matcher matcher = pattern.matcher(yourTextPane.getText());
while (matcher.find()) {
// Change the color of recognized tokens
styledDocument.setCharacterAttributes(matcher.start(), matcher.end() - matcher.start(), greenAttributeSet, false);
}
}
}
还有
((AbstractDocument) yourTextPane.getDocument()).setDocumentFilter(new CustomDocumentFilter());
我想迭代地使用它,也就是说,任何新字符串ALL_WORDS_THAT_YOU_WANT_TO_FIND
都会自动着色。我想到了删除
styledDocument.setCharacterAttributes(0, yourTextPane.getText().length(), blackAttributeSet, true);
(即不破坏之前的彩色单词),但它不起作用:它只保留上次迭代时给出的输入单词的颜色。我怎样才能做到这一点?
最佳答案
编辑:在评论中提出两个问题后更新
您想将单词添加到列表中并更新 JTextPane
吗?在这种情况下,您需要确保每次 updateTextStyles
方法运行时都会更新并使用该列表。
您可以使用多个单词列表,这些单词列表可以对文本应用独特的格式。您开始使用的代码使用正则表达式,您可以将其扩展为多个正则表达式。您还可以搜索子字符串(或文本片段)的区分大小写的精确匹配项,而无需查看单词边界,如下面的代码中所使用的那样。
这意味着某些文本的格式可能会因不同组的匹配而多次更改。您搜索的顺序将决定最终结果。例如,这个小示例允许您填充文本 Pane 并将新单词添加到三个突出显示组(颜色为红色、橙色和蓝色):
以下是示例中三个类的代码(使用 Java 8):
IterativeDocumentFilter.java:
import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
public class IterativeDocumentFilter {
public static void main(String[] arguments) {
SwingUtilities.invokeLater(
() -> new IterativeDocumentFilter().createAndShowGui()
);
}
private void createAndShowGui() {
JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 1000, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout());
JTextPane textPane = new JTextPane(new DefaultStyledDocument());
CustomDocumentFilter documentFilter = new CustomDocumentFilter(textPane);
textPane.setBorder(new LineBorder(Color.BLACK, 1));
enlargeFont(textPane);
mainPanel.add(textPane, BorderLayout.CENTER);
mainPanel.add(createBottomPanels(documentFilter), BorderLayout.PAGE_END);
frame.getContentPane().add(mainPanel);
frame.setVisible(true);
}
private JPanel createBottomPanels(CustomDocumentFilter documentFilter) {
JPanel bottomPanels = new JPanel();
bottomPanels.setLayout(new BoxLayout(bottomPanels, BoxLayout.PAGE_AXIS));
for (HighlightGroup highlightGroup : documentFilter.getHighlightGroups()) {
List<String> textFragments = highlightGroup.getTextFragments();
JPanel groupPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JLabel textFragmentsLabel = new JLabel("Current text fragments: "
+ textFragments);
textFragmentsLabel.setForeground(highlightGroup.getColor());
JLabel addTextFragmentLabel = new JLabel("Additional text fragment:");
addTextFragmentLabel.setForeground(highlightGroup.getColor());
JTextField addTextFragmentTextField = new JTextField(28);
JButton addTextFragmentButton = new JButton("Add text fragment");
addTextFragmentButton.setForeground(highlightGroup.getColor());
addTextFragmentButton.addActionListener(actionEvent -> {
String newTextFragment = addTextFragmentTextField.getText().trim();
if (!textFragments.contains(newTextFragment)) {
textFragments.add(newTextFragment);
documentFilter.handleTextChanged();
textFragmentsLabel.setText("Current text fragments: "
+ textFragments);
}
addTextFragmentTextField.setText("");
});
groupPanel.add(addTextFragmentLabel);
groupPanel.add(addTextFragmentTextField);
groupPanel.add(addTextFragmentButton);
textFragmentsLabel.setBorder(new EmptyBorder(0, 42, 0, 0));
groupPanel.add(textFragmentsLabel);
enlargeFont(addTextFragmentLabel);
enlargeFont(addTextFragmentTextField);
enlargeFont(addTextFragmentButton);
enlargeFont(textFragmentsLabel);
bottomPanels.add(groupPanel);
}
return bottomPanels;
}
private void enlargeFont(Component component) {
component.setFont(component.getFont().deriveFont(16f));
}
}
CustomDocumentFilter.java:
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.text.*;
public class CustomDocumentFilter extends DocumentFilter
{
private final JTextPane textPane;
private final List<HighlightGroup> highlightGroups;
private final StyleContext styleContext = StyleContext.getDefaultStyleContext();
private final AttributeSet blackAttributeSet
= styleContext.addAttribute(styleContext.getEmptySet(),
StyleConstants.Foreground, Color.BLACK);
public CustomDocumentFilter(JTextPane textPane) {
this.textPane = textPane;
highlightGroups = createHighlightGroups();
((AbstractDocument) textPane.getDocument()).setDocumentFilter(this);
}
private List<HighlightGroup> createHighlightGroups() {
List<HighlightGroup> groups = new ArrayList<>();
groups.add(new HighlightGroup(Arrays.asList("one", "two", "three"), Color.RED));
groups.add(new HighlightGroup(Arrays.asList("a", "the"), Color.ORANGE));
groups.add(new HighlightGroup(Arrays.asList("th", "o"), Color.BLUE));
return groups;
}
public List<HighlightGroup> getHighlightGroups() {
return highlightGroups;
}
@Override
public void insertString(FilterBypass fb, int offset, String text,
AttributeSet attributeSet) throws BadLocationException {
super.insertString(fb, offset, text, attributeSet);
handleTextChanged();
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
super.remove(fb, offset, length);
handleTextChanged();
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attributeSet) throws BadLocationException {
super.replace(fb, offset, length, text, attributeSet);
handleTextChanged();
}
/**
* Runs your updates later, not during the event notification.
*/
public void handleTextChanged()
{
SwingUtilities.invokeLater(this::updateTextStyles);
}
private void updateTextStyles()
{
// Reset the existing styles by using the default black style for all text.
StyledDocument document = textPane.getStyledDocument();
document.setCharacterAttributes(0, textPane.getText().length(),
blackAttributeSet, true);
// Apply styling for the different groups (the order of the groups is relevant).
for (HighlightGroup highlightGroup : highlightGroups) {
highlightGroup.highlightWords(textPane);
}
}
}
HighlightGroup.java:
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.text.*;
public class HighlightGroup {
private final List<String> textFragments;
private final Color color;
private final AttributeSet attributeSet;
public HighlightGroup(List<String> textFragments, Color color) {
this.textFragments = new ArrayList<>(textFragments);
this.color = color;
StyleContext styleContext = StyleContext.getDefaultStyleContext();
this.attributeSet = styleContext.addAttribute(styleContext.getEmptySet(),
StyleConstants.Foreground,
color);
}
public List<String> getTextFragments() {
return textFragments;
}
public Color getColor() {
return color;
}
public void highlightWords(JTextPane textPane) {
String text = textPane.getText();
StyledDocument styledDocument = textPane.getStyledDocument();
for (String textFragment : textFragments) {
int fromIndex = 0;
int startIndex = text.indexOf(textFragment, fromIndex);
while (startIndex != -1) {
// Change the color of recognized text fragments.
styledDocument.setCharacterAttributes(startIndex, textFragment.length(),
attributeSet, false);
fromIndex = startIndex + textFragment.length();
startIndex = text.indexOf(textFragment, fromIndex);
}
}
}
}
关于java - 迭代使用 DocumentFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35554959/
我正在创建一个自定义DocumentFilter。 但是,我必须在几个不同的组件上使用它。它们之间的唯一区别是字符限制,可以通过更改单个变量来更改字符限制。 问题是,如何将该变量传递给 Documen
您好,我有以下代码: AbstractDocument d = (AbstractDocument)editorText.getDocument(); d.setDocumen
我知道这是一个常见问题,但我正在尝试创建一个只接受 int 数字的 TextField,这几乎完成了,这是代码: 创建文本框: nome = new JFormattedTextField();
我正在尝试运行此代码: How to change the color of specific words in a JTextPane? private final class CustomDocu
我想在我的DocumentFilter上有一个这样的方法 public void replaceUpdate(int offset, int length, String text) {
我尝试将其他人为我需要的自定义 PlainDocument 组合在一起,但由于我不知道 PlainDocument 的机制,所以我失败了,它不起作用。我需要一些东西来确保我的文本字段只允许 2 个字母
Click here to show the gif DocumentFilter df = new DocumentFilter(){ @Override public void insertSt
我有一个程序,可以从 JTextField 中删除所有非数字字符并将其限制为 5 位数字。但此文档过滤器还删除了退格功能,这意味着我无法编辑已完成的输入。如何在不删除过滤器的情况下再次添加退格键? 编
我正在尝试为我的 JTextArea 设置一个 documentFilter。重写 insert(...) 方法后,我承认它从未被调用。怎么了?一段代码: package jaba; import j
所以,我有一个带有 JTextArea 和 DocumentFilter 的程序。它应该(除其他事项外)过滤掉制表符并防止它们完全输入到 JTextArea 中。 好吧,我打字的时候效果很好,但我仍然
我对 JFormattedTextField 有疑问(我将它用作我们所有文本字段的基类)。 今天我尝试向该字段的文档添加一个文档过滤器,它工作得很好,但前提是它没有设置格式化程序工厂。 问题是,当设置
Qestion First: I need to regex to match 111 or 111. or 111.111 (just aritrarty numbers) for a Docume
我正在使用 jtextarea 和文档过滤器。我希望只要用户在其中按下“b”,整个文本就会被删除,除了第一个字母。我怎样才能做到这一点。有些想法会很有用。 public void replace(Fi
我认为这一定是代码中的一个简单错误或我的误解,但我无法获得 DocumentFilter 来检测 insertString 事件。下面是一个简单的大写字母过滤器,但这并不重要,因为 insertStr
我正在尝试为我的 JTextArea 设置一个 documentFilter。覆盖了 insert(...) 方法后,我承认它从未被调用过。怎么了?一段代码: package jaba; import
我一直在努力理解这个 DocumentFilter 业务,就在我觉得我已经基本理解它时,我尝试了一个简单的测试用例,但它没有任何意义。 因此最初的目标是创建一个简单的 DocumentFilter 以
我想确保我的 JTextField 中始终有一个正整数。例如,当创建 GUI 时,JTextField 目前有一个默认值“1”,我希望它能够在用户决定按退格键时,而不是成为一个空文本字段,我希望它自动
我的问题如下: 我有一个: public class WWFormattedTextField extends JFormattedTextField implements FocusListener
我正在使用: String s = JOptionPane.showInputDialog(...); 从用户那里得到对问题的回复;该对话框设置为显示响应的文本字段。我想将响应中允许的字符限制为仅字母
我需要一个 JFormattedTextField 只允许输入 ##-###** 其中连字符始终出现在文本字段中,最后一个2 个字符,由 * 表示,可以是 2 个字母表 (a-z/A-Z),也可以什么
我是一名优秀的程序员,十分优秀!