[英]How to add rule to JtextPane according to the text entered
I want to have a rule in JtextPane such as it bolds the words if it contains "-" (dash) as a character else it doesnt.我想在 JtextPane 中有一个规则,例如如果它包含“-”(破折号)作为字符,它会加粗单词,否则它不会。
Can i add some rule in documentFilter?我可以在 documentFilter 中添加一些规则吗? Where can i add this rule
我在哪里可以添加此规则
First of all use StyledEditorKit (or extension eg HTMLEditorKit).首先使用 StyledEditorKit(或扩展,例如 HTMLEditorKit)。 Add a DocumentListener.
添加一个 DocumentListener。 On each change you can check whether newly typed word contains '-' char.
在每次更改时,您都可以检查新输入的单词是否包含“-”字符。 I would add the check on ' ' typed.
我会在 ' ' 类型上添加检查。 Use Utilities class to get word start and word end.
使用 Utilities class 获取单词开头和单词结尾。 Create a SimpleAttributeSet and use StyleConstants.setBold() to set it on or off.
创建一个 SimpleAttributeSet 并使用 StyleConstants.setBold() 将其设置为打开或关闭。 Then use ((StyledDocument)yourTextpane.getDocument()).setCharacterAttributes() to set/reset the bold state.
然后使用 ((StyledDocument)yourTextpane.getDocument()).setCharacterAttributes() 设置/重置粗体 state。
hmmm, yes Text Component Features tutorial and DocumentFilter can do that, for example:嗯,是的, 文本组件功能教程和DocumentFilter可以做到这一点,例如:
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class TextAreaTest extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea textArea;
public TextAreaTest() {
textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(60, 32));
textArea.setOpaque(true);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
string = string.replaceAll("-", "");
super.insertString(fb, offset, string, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
text = text.replaceAll("-", "");
//TODO must do something here
super.replace(fb, offset, length, text, attrs);
}
});
textArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent e) {
update(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
update(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
update(e);
}
private void update(DocumentEvent e) {
List<String> lines = getLines(textArea);
String lastLine = lines.get(lines.size() - 1);
int tabbedTextWidth = Utilities.getTabbedTextWidth(new Segment(
lastLine.toCharArray(), 0, lastLine.length()), textArea.getFontMetrics(textArea.getFont()), 0, null, 0);
int lineHeight = getLineHeight(textArea);
if (lines.size() * lineHeight > textArea.getHeight() || tabbedTextWidth > textArea.getWidth()) {
System.out.println("Too big! Should refuse the update!");
}
}
});
getContentPane().add(textArea);
}
private static List<String> getLines(JTextArea textArea) {
int lineHeight = getLineHeight(textArea);
List<String> list = new ArrayList<String>();
for (int num = 0;; num++) {
int i = textArea.viewToModel(new Point(0, num * lineHeight));
int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
if (i == 0 && j == 0) {
continue;
}
if (textArea.getDocument().getLength() == i && i == j) {
break;
}
String s = removeTrailingNewLine(textArea.getText().substring(i, j));
list.add(s);
//System.out.println(i + " " + j + " = " + s);
}
return list;
}
private static int getLineHeight(JTextArea textArea) {
return textArea.getFontMetrics(textArea.getFont()).getHeight();
}
private static String removeTrailingNewLine(String s) {
if (s.endsWith("\n")) {
return s.substring(0, s.length() - 1);
} else {
return s;
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TextAreaTest test = new TextAreaTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.pack();
test.setVisible(true);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.