gpt4 book ai didi

java - 如何让 DocumentFilter 与 JComboBox 一起使用

转载 作者:行者123 更新时间:2023-12-02 07:08:53 25 4
gpt4 key购买 nike

我之前曾使用 DocumentListener 尝试过此操作,但这给我在听到某些内容后编辑文档时可能会出现问题。现在我尝试对 DocumentFilter 执行相同的操作,它似乎有效。

public class InputField extends JComboBox<String>{

//for finding suggestions
private SuggestionFinder _finder;
//the model to use for adding items
private DefaultComboBoxModel<String> _model;

public InputField(SuggestionFinder finder){
super();
_model = new DefaultComboBoxModel();
this.setModel(_model);
maximumRowCount = 5;
this.setEditable(true);
Dimension d = new Dimension(300, 75);
this.setMinimumSize(d);
this.setMaximumSize(d);
_finder = finder;
Component edComp = editor.getEditorComponent();
Document document = ((JTextComponent)edComp).getDocument();
if (document instanceof PlainDocument) {
((PlainDocument) document).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset,
String string, AttributeSet attr) throws BadLocationException {
System.out.println("1");
Document d = fb.getDocument();
giveSuggestions(d.getText(0, d.getLength()));
super.insertString(fb, offset, string, attr);
}

@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
System.out.println("2");
Document d = fb.getDocument();
giveSuggestions(d.getText(0, d.getLength()));
super.remove(fb, offset, length);
}

@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
System.out.println("3");
Document d = fb.getDocument();
giveSuggestions(d.getText(0, d.getLength()));
super.replace(fb, offset, length, text, attrs);
}
});
}
}

private void giveSuggestions(String word){
System.out.println(word);
_model.removeAllElements();
if (word.equals("")){
this.hidePopup();
}
else{
this.showPopup();
/*List<String> suggs = _finder.getSuggestions(word);
for (int i = 1; i <= suggs.size(); i++){
_model.addElement(suggs.get(i-1));
}*/
for (int i = 0; i < 5; i++){
System.out.println("adding");
_model.addElement("" + Math.floor(Math.random()*100));
}
}
System.out.println("done");
}

public static void main(String[] args) throws IOException{
//instantiate a finder, how I do this isn't really relevant to my issue
InputField field = new InputField(finder);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(field);
frame.pack();
frame.setVisible(true);
}

最佳答案

有件事告诉我您不想这样做,而是您确实想使用 DocumentFilter。例如:

import java.io.IOException;
import javax.swing.*;
import javax.swing.text.*;

public class DocFilterEg {
public static void main(String[] args) throws IOException {
JPanel panel = new JPanel();
InputField2 field2 = new InputField2();

panel.add(field2.getCombo());

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}

class InputField2 {
String[] foo = {"1", "2", "3"};
private JComboBox<String> combo = new JComboBox<String>(foo);

public InputField2() {
combo.setEditable(true);
Object editorComponent = combo.getEditor().getEditorComponent();
Document document = ((JTextComponent)editorComponent).getDocument();

if (document instanceof PlainDocument) {
((PlainDocument) document).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset,
String string, AttributeSet attr) throws BadLocationException {

Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
System.out.println("Original String: " + sb.toString());
sb.insert(offset, string);
System.out.println("New String: " + sb.toString());

super.insertString(fb, offset, string, attr);
}

@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {

Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
System.out.println("Original String: " + sb.toString());
sb.delete(offset, offset + length);
System.out.println("New String: " + sb.toString());

super.remove(fb, offset, length);
}

@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {

Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
System.out.println("Original String: " + sb.toString());
sb.replace(offset, offset + length, text);
System.out.println("New String: " + sb.toString());

super.replace(fb, offset, length, text, attrs);
}
});
}
}

public JComponent getCombo() {
return combo;
}
}

编辑,正如我昨天在上一篇文章中提到的。

关于java - 如何让 DocumentFilter 与 JComboBox 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15775356/

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