gpt4 book ai didi

java - 使用复选框自动选择/突出显示组合框中的项目

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

我使用示例代码创建了带有复选框项的组合框。 Java - Check Boxes in a JComboBox

我使用 JComboCheckBox 的示例程序

import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class ComboBoxMainClass {
public static void main(String[] args) {
// Frame for our test

JComboCheckBox combo = new JComboCheckBox(new JCheckBox[] {
new JCheckBox(""), new JCheckBox("First"),
new JCheckBox("Second") });

JFrame f = new JFrame("Frame Form Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(combo);
// Show the frame
f.pack();
f.setVisible(true);
}
}

但我无法通过按键选择组合项目。例如如果组合文本是第一、第二等。 用户按下“S”键, 第二个应该突出显示/选择。就像在普通的 JComboBox 中一样。 有什么方法可以做到这一点,因为在我的一个应用程序中我们需要它。

最佳答案

组合框项目选择由 KeySelectionManager 完成。默认实现使用从添加到 ComboBoxModel 的对象的 toString() 方法返回的值来确定要选择的项目。

您可以创建自定义 JCheckBox 组件并覆盖 toString() 方法以返回 getText() 方法。

或者另一种方法是创建自定义 KeySelectionManager 以与自定义渲染器一起使用。这种方法涉及更多。

查看 Combo Box With Custom Renderer对于作为渲染器和 KeySelectionManager 的类。但是,此类旨在显示业务对象的属性,而不是 Swing 组件,因此需要进一步定制。

下面是显示 JCheckBox 的自定义版本:

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

/*
* This class can be used as the renderer and KeySelectionManager for an
* Object added to the ComboBoxModel.
*
* The class must be extended and the getDisplayValue() method must be
* implemented. This method will return a String to be rendered in the
* JComboBox. The same String will be used to do key selection of an
* item in the ComboBoxModel.
*/
public class CheckBoxKeySelectionRenderer extends BasicComboBoxRenderer
implements JComboBox.KeySelectionManager
{
// Used by the KeySelectionManager implementation to determine when to
// start a new search or append typed character to the existing search.

private long timeFactor;
private long lastTime;
private long time;
private String prefix = "";

public CheckBoxKeySelectionRenderer(JComboBox comboBox)
{
comboBox.setRenderer( this );
comboBox.setKeySelectionManager( this );

Long l = (Long)UIManager.get("ComboBox.timeFactor");
timeFactor = l == null ? 1000L : l.longValue();
}

/**
* This method must be implemented in the extended class.
*
* @param item an item from the ComboBoxModel
* @returns a String containing the text to be rendered for this item.
*/
public String getDisplayValue(Object item)
{
if (item instanceof JCheckBox)
{
JCheckBox checkBox = (JCheckBox)item;
return checkBox.getText();
}
else
return item.toString();
}

// Implement the renderer

@Override
public Component getListCellRendererComponent(
JList list, Object item, int index, boolean isSelected, boolean hasFocus)
{
super.getListCellRendererComponent(list, item, index, isSelected, hasFocus);

if (item instanceof JCheckBox)
{
Component c = (Component)item;

if (isSelected)
{
c.setBackground(list.getSelectionBackground());
c.setForeground(list.getSelectionForeground());
}
else
{
c.setBackground(list.getBackground());
c.setForeground(list.getForeground());
}

return c;
}

return this;
}

// Implement the KeySelectionManager

@Override
public int selectionForKey(char aKey, ComboBoxModel model)
{
time = System.currentTimeMillis();

// Get the index of the currently selected item

int size = model.getSize();
int startIndex = -1;
Object selectedItem = model.getSelectedItem();

if (selectedItem != null)
{
for (int i = 0; i < size; i++)
{
if ( selectedItem == model.getElementAt(i) )
{
startIndex = i;
break;
}
}
}

// Determine the "prefix" to be used when searching the model. The
// prefix can be a single letter or multiple letters depending on how
// fast the user has been typing and on which letter has been typed.

if (time - lastTime < timeFactor)
{
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 += aKey;
}
}
else
{
startIndex++;
prefix = "" + aKey;
}

lastTime = time;

// Search from the current selection and wrap when no match is found

if (startIndex < 0 || startIndex >= size)
{
startIndex = 0;
}

int index = getNextMatch(prefix, startIndex, size, model);

if (index < 0)
{
// wrap
index = getNextMatch(prefix, 0, startIndex, model);
}

return index;
}

/*
** Find the index of the item in the model that starts with the prefix.
*/
private int getNextMatch(String prefix, int start, int end, ComboBoxModel model)
{
for (int i = start; i < end; i++ )
{
Object item = model.getElementAt(i);

if (item != null)
{
String displayValue = getDisplayValue( item ).toLowerCase();

if (displayValue.startsWith(prefix))
{
return i;
}
}
}

return -1;
}
}

您可以通过在代码中添加以下内容来使用它:

new CheckBoxKeySelectionRenderer(combo);

这将替换您当前的渲染器。但是,您仍然无法切换当前所选项目的选择状态。这是因为组合框不会两次选择相同的项目。这是一个组合框问题,而不是渲染器问题。我不知道如何解决这个问题。

关于java - 使用复选框自动选择/突出显示组合框中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456532/

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