gpt4 book ai didi

java - 无法将带有工具提示的新项目添加到 JComboBox (Java Swing)

转载 作者:行者123 更新时间:2023-11-30 02:02:41 26 4
gpt4 key购买 nike

我有一个 JComboBox,其中包含都有工具提示的项目。为了添加工具提示,我在这里使用了 stackoverflow 解决方案 Java Swing: Mouseover text on JComboBox items? 。现在我想更进一步,向组合框添加新项目,并为每个新项目添加新的工具提示。

为了测试这一点,我创建了一个带有组合框和按钮的简单测试项目。单击该按钮时,将创建一个新项目并将其添加到组合框中。我的问题是我不知道如何同时添加正确的工具提示。在这种情况下,似乎不允许我手动将工具提示字符串添加到列表中。

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class ComboBoxWithToolTips extends JPanel
{
JComboBox<String> combo;
ComboboxToolTipRenderer renderer;

JButton button;

String[] items;
List<String> tooltips;

public ComboBoxWithToolTips()
{
items = new String[] {"red", "blue", "black"};
tooltips = Arrays.asList(new String[] {"a", "b", "c"});
combo = new JComboBox<>(items);
renderer = new ComboboxToolTipRenderer();
renderer.setTooltips(tooltips);
combo.setRenderer(renderer);
add(combo);

button = new JButton("Add");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Random random = new Random();
String name = "new" + random.nextInt(100);
String tooltip = name + " tooltip";

combo.addItem(name); // Add the new item
renderer.tooltips.add(tooltip); // Add the new tooltip to the list
}
});
add(button);
}

public class ComboboxToolTipRenderer extends DefaultListCellRenderer
{
List<String> tooltips;

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{

JComponent comp = (JComponent) super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);

if (-1 < index && null != value && null != tooltips)
{
list.setToolTipText(tooltips.get(index));
}
return comp;
}

public void setTooltips(List<String> tooltips)
{
this.tooltips = tooltips;
}
}

public static void main(String[] args)
{
JFrame frame = new JFrame("Tooltip Test");

ComboBoxWithToolTips comboBoxWithToolTips = new ComboBoxWithToolTips();
comboBoxWithToolTips.setPreferredSize(new Dimension(500, 500));
frame.getContentPane()
.add(comboBoxWithToolTips);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}

框架打开,每个组合框项目都有其工具提示。但是当我按下“添加”按钮时,出现以下异常:

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException at java.util.AbstractList.add(Unknown Source) at java.util.AbstractList.add(Unknown Source) at ComboBoxWithToolTips$1.actionPerformed(ComboBoxWithToolTips.java:48) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

前面提到的 stackoverflow 主题中的解决方案仅适用于初始化,但不适用于动态添加新项目

有人看到这个错误吗?或者是否有一种特殊类型的 JComboBox 适合我想要做的事情?

提前致谢!

最佳答案

这是因为这一行:

tooltips = Arrays.asList(new String[] {"a", "b", "c"});

Arrays.asList 返回一个不可变列表,您无法向其中添加元素。

您可以像这样创建一个可变列表:

tooltips = new ArrayList<>(Arrays.asList(new String[] {"a", "b", "c"}));

关于java - 无法将带有工具提示的新项目添加到 JComboBox (Java Swing),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52315324/

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