java swing JList 中的快捷方式
Shortcut in java swing JList
我有一个 java swing JList 并希望能够使用双键移动到列表中的特定行。
看看我下面的清单。如果我按下键 2,列表中的焦点将跳转到第 2002 行。但我希望能够按下键 22(两个 2:s),然后焦点跳转到 2201.
我的名单:
1001
1002
1003
1101
1102
1103
2002
2003
2004
2201
2202
有人知道 JList 是否可行吗?
这是由 LAF 控制的。
默认逻辑表明,当您输入相同的键时,列表将循环到列表中的下一个项目。
因此您不能直接转到“22...”编号,因为它会遍历以“2...”开头的每个项目。
但是,如果您有“2301”和“2311”这样的号码,您可以直接转到这些号码。
这是在 BasicListUI 中找到的逻辑 class:
public void keyTyped(KeyEvent e) {
JList src = (JList)e.getSource();
ListModel model = src.getModel();
if (model.getSize() == 0 || e.isAltDown() ||
BasicGraphicsUtils.isMenuShortcutKeyDown(e) ||
isNavigationKey(e)) {
// Nothing to select
return;
}
boolean startingFromSelection = true;
char c = e.getKeyChar();
long time = e.getWhen();
int startIndex = adjustIndex(src.getLeadSelectionIndex(), list);
if (time - lastTime < timeFactor) {
typedString += c;
if((prefix.length() == 1) && (c == 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 = "" + c;
prefix = typedString;
}
lastTime = time;
if (startIndex < 0 || startIndex >= model.getSize()) {
startingFromSelection = false;
startIndex = 0;
}
int index = src.getNextMatch(prefix, startIndex,
Position.Bias.Forward);
if (index >= 0) {
src.setSelectedIndex(index);
src.ensureIndexIsVisible(index);
} else if (startingFromSelection) { // wrap
index = src.getNextMatch(prefix, 0,
Position.Bias.Forward);
if (index >= 0) {
src.setSelectedIndex(index);
src.ensureIndexIsVisible(index);
}
}
}
注意设置 "prefix" 变量的注释。
因此,如果您想更改行为,您需要创建自定义 UI 并覆盖该方法。不知道该方法是否使用私有变量或方法。
或者另一种选择是从 JList 中删除默认的 KeyListener。然后您可以实现自己的 KeyListener 并使用您自定义的前缀直接调用 getNextMatch(...)
。
我有一个 java swing JList 并希望能够使用双键移动到列表中的特定行。 看看我下面的清单。如果我按下键 2,列表中的焦点将跳转到第 2002 行。但我希望能够按下键 22(两个 2:s),然后焦点跳转到 2201.
我的名单:
1001
1002
1003
1101
1102
1103
2002
2003
2004
2201
2202
有人知道 JList 是否可行吗?
这是由 LAF 控制的。
默认逻辑表明,当您输入相同的键时,列表将循环到列表中的下一个项目。
因此您不能直接转到“22...”编号,因为它会遍历以“2...”开头的每个项目。
但是,如果您有“2301”和“2311”这样的号码,您可以直接转到这些号码。
这是在 BasicListUI 中找到的逻辑 class:
public void keyTyped(KeyEvent e) {
JList src = (JList)e.getSource();
ListModel model = src.getModel();
if (model.getSize() == 0 || e.isAltDown() ||
BasicGraphicsUtils.isMenuShortcutKeyDown(e) ||
isNavigationKey(e)) {
// Nothing to select
return;
}
boolean startingFromSelection = true;
char c = e.getKeyChar();
long time = e.getWhen();
int startIndex = adjustIndex(src.getLeadSelectionIndex(), list);
if (time - lastTime < timeFactor) {
typedString += c;
if((prefix.length() == 1) && (c == 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 = "" + c;
prefix = typedString;
}
lastTime = time;
if (startIndex < 0 || startIndex >= model.getSize()) {
startingFromSelection = false;
startIndex = 0;
}
int index = src.getNextMatch(prefix, startIndex,
Position.Bias.Forward);
if (index >= 0) {
src.setSelectedIndex(index);
src.ensureIndexIsVisible(index);
} else if (startingFromSelection) { // wrap
index = src.getNextMatch(prefix, 0,
Position.Bias.Forward);
if (index >= 0) {
src.setSelectedIndex(index);
src.ensureIndexIsVisible(index);
}
}
}
注意设置 "prefix" 变量的注释。
因此,如果您想更改行为,您需要创建自定义 UI 并覆盖该方法。不知道该方法是否使用私有变量或方法。
或者另一种选择是从 JList 中删除默认的 KeyListener。然后您可以实现自己的 KeyListener 并使用您自定义的前缀直接调用 getNextMatch(...)
。