无法将带有工具提示的新项目添加到 JComboBox (Java Swing)
Can't add a new item with tooltip to JComboBox (Java Swing)
我有一个 JComboBox,其中包含 个都有工具提示的项目 。为了添加工具提示,我在这里使用了 Whosebug 解决方案 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);
}
}
框架打开,每个组合框项目都有其工具提示。但是当我按下 "Add" 按钮时,出现以下异常:
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.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.access0(Unknown Source) at
java.awt.EventQueue.run(Unknown Source) at
java.awt.EventQueue.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.run(Unknown Source) at
java.awt.EventQueue.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)
前面提到的 Whosebug 主题的解决方案仅适用于初始化,但不适用于动态添加新项目。
有没有人看到错误或者是否有一种特殊类型的 JComboBox 可以满足我的要求?
提前致谢!
是因为这一行:
tooltips = Arrays.asList(new String[] {"a", "b", "c"});
Arrays.asList
returns 一个不可变的列表,你不能向它添加元素。
您可以像这样创建一个可变列表:
tooltips = new ArrayList<>(Arrays.asList(new String[] {"a", "b", "c"}));
我有一个 JComboBox,其中包含 个都有工具提示的项目 。为了添加工具提示,我在这里使用了 Whosebug 解决方案 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);
}
}
框架打开,每个组合框项目都有其工具提示。但是当我按下 "Add" 按钮时,出现以下异常:
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.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.access0(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.awt.EventQueue.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.run(Unknown Source) at java.awt.EventQueue.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)
前面提到的 Whosebug 主题的解决方案仅适用于初始化,但不适用于动态添加新项目。
有没有人看到错误或者是否有一种特殊类型的 JComboBox 可以满足我的要求?
提前致谢!
是因为这一行:
tooltips = Arrays.asList(new String[] {"a", "b", "c"});
Arrays.asList
returns 一个不可变的列表,你不能向它添加元素。
您可以像这样创建一个可变列表:
tooltips = new ArrayList<>(Arrays.asList(new String[] {"a", "b", "c"}));