gpt4 book ai didi

java - 迭代使用 DocumentFilter

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:05 25 4
gpt4 key购买 nike

我正在尝试运行此代码:

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 并将新单词添加到三个突出显示组(颜色为红色、橙色和蓝色):

Screenshot of the example program

以下是示例中三个类的代码(使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com