gpt4 book ai didi

java - 是否可以在java swing中从ListSelectionEvents中提取字符串?

转载 作者:行者123 更新时间:2023-12-02 11:34:38 24 4
gpt4 key购买 nike

我在 java swing 中使用字符串的 JList。我想要一个 ListSelectionListener,它返回用户选择的 Listentry 的字符串。但我无法到达字符串本身。我唯一能找到的东西

private void registerListeners()
{
gui.getListMeasurements().addListSelectionListener(new ListSelectionListener()
{

@Override
public void valueChanged(ListSelectionEvent event)
{
System.out.println(event.getSource());

}
});
}

输出以下4次:javax.swing.JList[,0,0,240x340,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1, fixedCellWidth=-1,horizo​​ntalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b =51],visibleRowCount=8,layoutOrientation=0]

此输出中没有任何内容涉及列表中选择的字符串。我也找不到该事件的任何有用的方法。是否可以按照我描述的方式提取字符串?

最佳答案

gui.getListMeasurements().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
if (e.getSource() instanceof JList) {
JList list = (JList) e.getSource();
int index = list.getSelectedIndex();
Object element = list.getModel().getElementAt(index);
if (element instanceof String) {
System.out.println("Selected value at " + index + " = " + element);
}
}
}
});

有用的链接:

关于java - 是否可以在java swing中从ListSelectionEvents中提取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041197/

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