gpt4 book ai didi

java - JComboBox(JList) - 每个单元格的可点击图像

转载 作者:行者123 更新时间:2023-11-29 04:41:10 24 4
gpt4 key购买 nike

我正在尝试编辑 JComboBox,以便它在每个单元格中都有一个“删除”按钮(图像/图标)以删除特定条目。我已经尝试创建自定义 CellRenderer,我能够添加图像但不能对 MouseClicks 使用react(至少我不知道单击了哪个图像/单元格),我只能获得 X 和 Y 位置。但是我不知道图像的位置。

enter image description here

如果有人对如何执行此操作有想法,我将不胜感激,我已经尝试过谷歌搜索,但我没有找到任何合适的东西。

最佳答案

这可能(?)给你一些想法。

它使用 JLayer 类来装饰 JList 并绘制关闭图标(在示例中是一个简单的“X”字符)。

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

class LayerList extends JPanel
{
LayerList()
{
LayerUI<JComponent> layerUI = new LayerUI<JComponent>()
{
public void paint(Graphics g, JComponent c)
{
super.paint(g, c);

JLayer layer = (JLayer)c;
JList list = (JList)layer.getView();
int selected = list.getSelectedIndex();

if (selected == -1 ) return;

Rectangle area = list.getCellBounds(selected, selected);

g.drawString("X", area.width - 15, area.y + area.height - 3);
}

public void installUI(JComponent c)
{
super.installUI(c);

JLayer jlayer = (JLayer)c;
jlayer.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
}

public void uninstallUI(JComponent c)
{
super.uninstallUI(c);

// reset the layer event mask
((JLayer) c).setLayerEventMask(0);
}

@Override
protected void processMouseEvent(MouseEvent e, JLayer l)
{
// e.consume();

if (e.getID() == MouseEvent.MOUSE_RELEASED)
{
JList list = (JList)e.getComponent();
int selected = list.getSelectedIndex();
Rectangle area = list.getCellBounds(selected, selected);

if (e.getX() >= area.width - 15)
{
e.consume();
DefaultComboBoxModel model = (DefaultComboBoxModel)list.getModel();
model.removeElementAt( selected );
list.setSelectedIndex(selected);
}
}
}
};

String[] data = { "a", "b", "c", "d", "e", "f" };

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

Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = (JList)popup.getList();
Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
JScrollPane scrollPane = (JScrollPane)c;
scrollPane.setViewportView( new JLayer<JComponent>(list, layerUI) );


setLayout( new BorderLayout() );
add( comboBox );
}

private static void createAndShowGUI()
{
JFrame frame = new JFrame("Layer List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new LayerList() );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}

阅读 How to Decorate Components with the JLayer Class 上的 Swing 教程部分有关使用 JLayer 类的更多信息和示例。

关于java - JComboBox(JList) - 每个单元格的可点击图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39102740/

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