gpt4 book ai didi

java - JCombobox键盘输入速度

转载 作者:行者123 更新时间:2023-11-30 04:13:13 25 4
gpt4 key购买 nike

我有一个 JCombobox,上面有一长串项目。为了快速访问,您可以随时键入所需的项目,组合框将引导您找到列表中的项目。问题是您需要输入的速度相当高,如果您放慢输入项目的速度,组合框会将您带到列表中的其他项目。

我想知道 JCombobox 中是否有一个属性或一种方法可以在键盘输入忘记之前在内存中保留更长的时间。

提前致谢。

最佳答案

默认的 KeySelectionManager 仅硬编码 1 秒的延迟来组合键或开始新的搜索,因此您需要创建一个自定义 KeySelectionManager 来配置搜索延迟。看一下 BasicComboBoxUI 类中的默认代码。

以下内容或多或少是该代码的副本。它有点复杂,因为它无法直接访问 JList。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.*;

public class ComboBoxKeySelection extends JPanel
{
JComboBox<String> comboBox;

public ComboBoxKeySelection()
{
String[] data =
{
" 1", " 2", " 3", " 4",
"a", "ab", "abc", "abcd",
"b1", "b2", "b3", "b4", "be",
"c", "d", "e", "f"
};

comboBox = new JComboBox<String>( data );
add( comboBox );

UIManager.put("ComboBox.timeFactor", 3000);
comboBox.setKeySelectionManager( new MyKeySelectionManager(comboBox) );
}


static class MyKeySelectionManager implements JComboBox.KeySelectionManager
{
private JComboBox comboBox;
private JList listBox;
private boolean useComboBoxModel;

private int timeFactor;
private long lastTime;
private long time;

private String prefix = "";
private String typedString = "";

public MyKeySelectionManager(JComboBox comboBox)
{
this.comboBox = comboBox;

int uiTimeFactor = UIManager.getInt("ComboBox.timeFactor");
timeFactor = (uiTimeFactor == 0) ? 1000 : uiTimeFactor;

// Get the JList used by the UI to hold the comboBox entries

Object child = comboBox.getAccessibleContext().getAccessibleChild(0);

if (child instanceof BasicComboPopup)
{
BasicComboPopup popup = (BasicComboPopup)child;
listBox = popup.getList();
useComboBoxModel = false;
}
else
{
listBox = new JList();
useComboBoxModel = true;
}
}

public int selectionForKey(char aKey, ComboBoxModel aModel)
{
// Not using the basic UI so build our own JList to search

if (useComboBoxModel)
{
listBox.setModel( aModel );
}

time = System.currentTimeMillis();
boolean startingFromSelection = true;
int startIndex = comboBox.getSelectedIndex();

if (time - lastTime < timeFactor)
{
typedString += aKey;

if((prefix.length() == 1) && (aKey == prefix.charAt(0)))
{
// Subsequent same key presses move the keyboard focus to the next
// object that starts with the same letter.
startIndex++;
}
else
{
prefix = typedString;
}
}
else
{
startIndex++;
typedString = "" + aKey;
prefix = typedString;
}

lastTime = time;

if (startIndex < 0 || startIndex >= aModel.getSize())
{
startingFromSelection = false;
startIndex = 0;
}

int index = listBox.getNextMatch(prefix, startIndex, Position.Bias.Forward);

if (index < 0 && startingFromSelection)
{
// wrap
index = listBox.getNextMatch(prefix, 0, Position.Bias.Forward);
}

return index;
}
}


private static void createAndShowUI()
{
JFrame frame = new JFrame("ComboBoxKeySelection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ComboBoxKeySelection() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

关于java - JCombobox键盘输入速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19093433/

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