ListView<String> listView = new ListView<String>();
//github上开源项目richtextfx的一个组件
CodeArea codeArea = new CodeArea();
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
Popup popup = new Popup();
popup.setAutoHide(true);
popup.getContent().addAll(listView);
this.codeArea.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
boolean temp = keyPressedChange;
keyPressedChange = false;
// if (temp){
if (true){
//截取newvalue光标前最后一个非字母到光标位置,作为提示信息输入源
if (!StringUtil.isEmpty(newValue)){
int caretPosition = codeArea.getCaretPosition();
newValue = newValue.substring(0,caretPosition);
int index = -1;
char[] chars = newValue.toCharArray();
for (int i=0;i<chars.length;i++){
boolean matches = String.valueOf(chars[i]).matches("^[A-Za-z0-9_]+$");
if (!matches){
index=i;
}
}
String substring = newValue.substring(index+1, newValue.length());
if (StringUtil.isEmpty(substring)){
popup.hide();
return;
}
LinkedList<String> linkedList = new LinkedList<String>();
for (Fun item:items){
String funTip = item.getFunTip();
int i = funTip.indexOf("(");
String s = funTip.substring(0, i);
if (s.toUpperCase().contains(substring.toUpperCase())){
linkedList.add(funTip);
}
}
ObservableList<String> filteredList = FXCollections.observableArrayList(linkedList);
if (filteredList!=null && filteredList.size()>0){
listView.setItems(filteredList);
popup.show(codeArea.getScene().getWindow());
}else {
popup.hide();
}
}else {
popup.hide();
}
}
}
});
javafx带行号的textarea 类似idea的一个代码编辑框 编码提示
最新推荐文章于 2024-05-14 10:07:39 发布