- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已将自动完成功能应用于两个 textFields
。为此,我使用了 AutocompleteDecorator 。我发现它的速度减慢到我什至无法输入单个字符的程度。有什么解决办法吗?
谢谢
JTextArea textArea = main.getEditor();
String content = textArea.getText();
try {
if (content.length() > 0) {
String[] lines = content.split("\n");
String txt1 = null;
String txt2 = null;
for (String line : lines) {
txt1= line.substring(0, 10);
txt2 = line.substring(11, 15);
listTxt1.add(txt1);
listTxt2.add(txt2);
}
AutoCompleteDecorator.decorate(txtField1, listTxt1, false);
AutoCompleteDecorator.decorate(txtField2, listTxt2, false);
}
} catch (Exception ex) {
System.out.println(ex);
}
最佳答案
根据我的评论,我决定验证这一点。
我通过以下方式将 1000 个随机字母 String
添加到我的字典中:
for (int i = 0; i < 1000; i++) {
words.add(generateString(r, "abcdefghijklmnopqrstuvwxyz", 10));
}
...
public static String generateString(Random rng, String characters, int length) {
char[] text = new char[length];
for (int i = 0; i < length; i++) {
text[i] = characters.charAt(rng.nextInt(characters.length()));
}
return new String(text);
}
并且根本没有明显的减速。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/**
* @author David
*/
public class Test {
Random r = new Random();
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField f = new JTextField(10);
//create list for dictionary this in your case might be done via calling a method which queries db and returns results as arraylist
ArrayList<String> words = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
words.add(generateString(r, "abcdefghijklmnopqrstuvwxyz", 10));
}
AutoSuggestor autoSuggestor = new AutoSuggestor(f, frame, words, Color.WHITE.brighter(), Color.BLUE, Color.RED, 0.75f);
JPanel p = new JPanel();
p.add(f);
frame.add(p);
frame.pack();
frame.setVisible(true);
}
public static String generateString(Random rng, String characters, int length) {
char[] text = new char[length];
for (int i = 0; i < length; i++) {
text[i] = characters.charAt(rng.nextInt(characters.length()));
}
return new String(text);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test();
}
});
}
}
class AutoSuggestor {
private final JTextField textField;
private final Window container;
private JPanel suggestionsPanel;
private JWindow autoSuggestionPopUpWindow;
private String typedWord;
private final ArrayList<String> dictionary = new ArrayList<>();
private int currentIndexOfSpace, tW, tH;
private DocumentListener documentListener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent de) {
checkForAndShowSuggestions();
}
@Override
public void removeUpdate(DocumentEvent de) {
checkForAndShowSuggestions();
}
@Override
public void changedUpdate(DocumentEvent de) {
checkForAndShowSuggestions();
}
};
private final Color suggestionsTextColor;
private final Color suggestionFocusedColor;
public AutoSuggestor(JTextField textField, Window mainWindow, ArrayList<String> words, Color popUpBackground, Color textColor, Color suggestionFocusedColor, float opacity) {
this.textField = textField;
this.suggestionsTextColor = textColor;
this.container = mainWindow;
this.suggestionFocusedColor = suggestionFocusedColor;
this.textField.getDocument().addDocumentListener(documentListener);
setDictionary(words);
typedWord = "";
currentIndexOfSpace = 0;
tW = 0;
tH = 0;
autoSuggestionPopUpWindow = new JWindow(mainWindow);
autoSuggestionPopUpWindow.setOpacity(opacity);
suggestionsPanel = new JPanel();
suggestionsPanel.setLayout(new GridLayout(0, 1));
suggestionsPanel.setBackground(popUpBackground);
addKeyBindingToRequestFocusInPopUpWindow();
}
private void addKeyBindingToRequestFocusInPopUpWindow() {
textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released");
textField.getActionMap().put("Down released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {//focuses the first label on popwindow
for (int i = 0; i < suggestionsPanel.getComponentCount(); i++) {
if (suggestionsPanel.getComponent(i) instanceof SuggestionLabel) {
((SuggestionLabel) suggestionsPanel.getComponent(i)).setFocused(true);
autoSuggestionPopUpWindow.toFront();
autoSuggestionPopUpWindow.requestFocusInWindow();
suggestionsPanel.requestFocusInWindow();
suggestionsPanel.getComponent(i).requestFocusInWindow();
break;
}
}
}
});
suggestionsPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released");
suggestionsPanel.getActionMap().put("Down released", new AbstractAction() {
int lastFocusableIndex = 0;
@Override
public void actionPerformed(ActionEvent ae) {//allows scrolling of labels in pop window (I know very hacky for now :))
ArrayList<SuggestionLabel> sls = getAddedSuggestionLabels();
int max = sls.size();
if (max > 1) {//more than 1 suggestion
for (int i = 0; i < max; i++) {
SuggestionLabel sl = sls.get(i);
if (sl.isFocused()) {
if (lastFocusableIndex == max - 1) {
lastFocusableIndex = 0;
sl.setFocused(false);
autoSuggestionPopUpWindow.setVisible(false);
setFocusToTextField();
checkForAndShowSuggestions();//fire method as if document listener change occured and fired it
} else {
sl.setFocused(false);
lastFocusableIndex = i;
}
} else if (lastFocusableIndex <= i) {
if (i < max) {
sl.setFocused(true);
autoSuggestionPopUpWindow.toFront();
autoSuggestionPopUpWindow.requestFocusInWindow();
suggestionsPanel.requestFocusInWindow();
suggestionsPanel.getComponent(i).requestFocusInWindow();
lastFocusableIndex = i;
break;
}
}
}
} else {//only a single suggestion was given
autoSuggestionPopUpWindow.setVisible(false);
setFocusToTextField();
checkForAndShowSuggestions();//fire method as if document listener change occured and fired it
}
}
});
}
private void setFocusToTextField() {
container.toFront();
container.requestFocusInWindow();
textField.requestFocusInWindow();
}
public ArrayList<SuggestionLabel> getAddedSuggestionLabels() {
ArrayList<SuggestionLabel> sls = new ArrayList<>();
for (int i = 0; i < suggestionsPanel.getComponentCount(); i++) {
if (suggestionsPanel.getComponent(i) instanceof SuggestionLabel) {
SuggestionLabel sl = (SuggestionLabel) suggestionsPanel.getComponent(i);
sls.add(sl);
}
}
return sls;
}
private void checkForAndShowSuggestions() {
typedWord = getCurrentlyTypedWord();
suggestionsPanel.removeAll();//remove previos words/jlabels that were added
//used to calcualte size of JWindow as new Jlabels are added
tW = 0;
tH = 0;
boolean added = wordTyped(typedWord);
if (!added) {
if (autoSuggestionPopUpWindow.isVisible()) {
autoSuggestionPopUpWindow.setVisible(false);
}
} else {
showPopUpWindow();
setFocusToTextField();
}
}
protected void addWordToSuggestions(String word) {
SuggestionLabel suggestionLabel = new SuggestionLabel(word, suggestionFocusedColor, suggestionsTextColor, this);
calculatePopUpWindowSize(suggestionLabel);
suggestionsPanel.add(suggestionLabel);
}
public String getCurrentlyTypedWord() {//get newest word after last white spaceif any or the first word if no white spaces
String text = textField.getText();
String wordBeingTyped = "";
if (text.contains(" ")) {
int tmp = text.lastIndexOf(" ");
if (tmp >= currentIndexOfSpace) {
currentIndexOfSpace = tmp;
wordBeingTyped = text.substring(tmp);
}
} else {
wordBeingTyped = text;
}
return wordBeingTyped.trim();
}
private void calculatePopUpWindowSize(JLabel label) {
//so we can size the JWindow correctly
if (tW < label.getPreferredSize().width) {
tW = label.getPreferredSize().width;
}
tH += label.getPreferredSize().height;
}
private void showPopUpWindow() {
autoSuggestionPopUpWindow.getContentPane().add(suggestionsPanel);
autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
autoSuggestionPopUpWindow.setSize(tW, tH);
autoSuggestionPopUpWindow.setVisible(true);
int windowX = 0;
int windowY = 0;
windowX = container.getX() + textField.getX() + 5;
if (suggestionsPanel.getHeight() > autoSuggestionPopUpWindow.getMinimumSize().height) {
windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getMinimumSize().height;
} else {
windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getHeight();
}
autoSuggestionPopUpWindow.setLocation(windowX, windowY);
autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
autoSuggestionPopUpWindow.revalidate();
autoSuggestionPopUpWindow.repaint();
}
public void setDictionary(ArrayList<String> words) {
dictionary.clear();
if (words == null) {
return;//so we can call constructor with null value for dictionary without exception thrown
}
for (String word : words) {
dictionary.add(word);
}
}
public JWindow getAutoSuggestionPopUpWindow() {
return autoSuggestionPopUpWindow;
}
public Window getContainer() {
return container;
}
public JTextField getTextField() {
return textField;
}
public void addToDictionary(String word) {
dictionary.add(word);
}
boolean wordTyped(String typedWord) {
if (typedWord.isEmpty()) {
return false;
}
//System.out.println("Typed word: " + typedWord);
boolean suggestionAdded = false;
for (String word : dictionary) {//get words in the dictionary which we added
boolean fullymatches = true;
for (int i = 0; i < typedWord.length(); i++) {//each string in the word
if (!typedWord.toLowerCase().startsWith(String.valueOf(word.toLowerCase().charAt(i)), i)) {//check for match
fullymatches = false;
break;
}
}
if (fullymatches) {
addWordToSuggestions(word);
suggestionAdded = true;
}
}
return suggestionAdded;
}
}
class SuggestionLabel extends JLabel {
private boolean focused = false;
private final JWindow autoSuggestionsPopUpWindow;
private final JTextField textField;
private final AutoSuggestor autoSuggestor;
private Color suggestionsTextColor, suggestionBorderColor;
public SuggestionLabel(String string, final Color borderColor, Color suggestionsTextColor, AutoSuggestor autoSuggestor) {
super(string);
this.suggestionsTextColor = suggestionsTextColor;
this.autoSuggestor = autoSuggestor;
this.textField = autoSuggestor.getTextField();
this.suggestionBorderColor = borderColor;
this.autoSuggestionsPopUpWindow = autoSuggestor.getAutoSuggestionPopUpWindow();
initComponent();
}
private void initComponent() {
setFocusable(true);
setForeground(suggestionsTextColor);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
super.mouseClicked(me);
replaceWithSuggestedText();
autoSuggestionsPopUpWindow.setVisible(false);
}
});
getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), "Enter released");
getActionMap().put("Enter released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
replaceWithSuggestedText();
autoSuggestionsPopUpWindow.setVisible(false);
}
});
}
public void setFocused(boolean focused) {
if (focused) {
setBorder(new LineBorder(suggestionBorderColor));
} else {
setBorder(null);
}
repaint();
this.focused = focused;
}
public boolean isFocused() {
return focused;
}
private void replaceWithSuggestedText() {
String suggestedWord = getText();
String text = textField.getText();
String typedWord = autoSuggestor.getCurrentlyTypedWord();
String t = text.substring(0, text.lastIndexOf(typedWord));
String tmp = t + text.substring(text.lastIndexOf(typedWord)).replace(typedWord, suggestedWord);
textField.setText(tmp + " ");
}
}
甚至使用了多达 50000 个随机字符串
,甚至可以看到/感觉到/感觉到微小的停顿
关于java - 自动完成响应缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14350000/
我最近从 Indigo“升级”到了 Luna(Oracle OEPE 安装)。请注意围绕“升级”一词的引用。 不幸的是,事情很慢。 我使用的项目是一个 Maven 多模块项目。构建工作区操作大约需要
如果我的 JavaScript 事件似乎都不是网页性能问题的原因,我该如何诊断网页性能问题? 我有一个使用jqGrid的网络应用程序。单击网格会导致 2-3 秒的卡住,然后发生任何事情(包括点击任何单
从 appengine 访问我的应用程序时,我经常收到以下错误。有人可以知道这是什么原因吗? 原因:com.google.apphosting.api.DeadlineExceededExceptio
出于某种原因,我的 curl 调用非常慢。这是我使用的代码。 $postData = "test" $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $
Stackdriver 测试我的网站启动速度慢 我们使用 cloudflare 作为我们的站点 CDN 提供商。我们使用 stackdriver 从外部测试站点可用性,我们将时间检查间隔设置为 1 分
在插入/更新许多行时,我知道SQLite的“问题”,但事实并非如此。 我正在更新包含约250条记录的表中的ONE一行(由PK索引)中的ONE字段。查询通常需要200毫秒左右的时间。听起来很少,但很大。
我们的 Mongo 数据库会定期(有时每天一次)变慢约 30-40 分钟。在此缓慢时期,访问数据库的 API 会遇到每 5-10 分钟就会出现一次的高延迟峰值。 查看 mongod 日志文件,这两
这个问题已经在这里有了答案: 已关闭8年。 Possible Duplicate: C# WinForm Application - UI Hangs during Long-Running Oper
我最近将我的 Java Liquibase 版本从 3.5.3 升级到 3.6.3 我有一个非常繁重的环境,其中有很多数据库和表(我使用的是 Oracle)。 在这种环境下,我试图执行一个巨大的变更日
在我的项目中,为了整洁起见,模块被组织在子目录中。 我的项目目录层次结构: $ ls -R .: configure.in Makefile.am Makefile.cvs src
我正在 Debian 上使用存储库中的软件包运行 Gitlab。大多数时候Gitlab运行速度非常快,但是在较长的空闲时间后Gitlab非常慢甚至超时(错误502)。有一次我在远程 git 访问上也遇
这可能是菜鸟的错误,所以请原谅我。我在高处和低处寻找解决方案,但没有结果-因此,我想在此添加第一篇文章:-) 我有两个域类,一个称为Domain,一个称为Page。如下代码所示,域中有许多页面。 cl
我是 React 的新手,在使用 onChange 时遇到了问题在大数据列表中生成的输入字段上的方法。 如 parentcomponent是数据的拥有者,我提供了handleUpdate()子组件 (
我们使用 Webpack DefinePlugin 为不同的渲染模式生成输出包。因此,例如,我们的 webpack 配置将返回 [{ entry: { mode1: "./in
我在页面顶部有一个带有菜单的标题元素。当我向下滚动时,标题会动画到较低的高度。当我向上滚动并到达顶部时,标题会以动画方式显示为原始大小。 但它的工作并不完美。有时,事情发生之前需要两秒钟。特别是当我向
我今天在我的文本编辑器(Sublime)中写了一些正则表达式,试图快速找到特定的源代码段,这需要有点创意,因为有时函数调用可能包含更多函数调用。例如,我正在寻找 jQuery 选择器: $("div[
ParentSadly 我没有通过搜索“laggy/slow mouse wheel-scrolling in Rich Edit control”和类似的句子找到答案。 我创建了一个丰富的编辑控件
我遇到了“OR”运算符在 mysql 中未使用任何索引的典型性能问题: SELECT sms.smsID, sms.phonenumber, sms.text, date, mbr.name, mbr
我最近一直在玩 asyncio 模块。下面是我想出的用于发送一些并行请求的代码,这些请求在我的笔记本电脑 (Mac OS) 上似乎运行良好,但在另一台机器 (Ubuntu 18.04) 上似乎运行缓慢
我目前正在开发一个并行应用程序(C#、WinForms),它通过 COM 将消息注入(inject)应用程序。 此应用程序使用多个 foreach 语句,从接受 COM 的应用程序中轮询实体指标。 L
我是一名优秀的程序员,十分优秀!