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

Is it possible to extract Strings out of ListSelectionEvents in java swing?

我在 java swing 中使用 JList of Strings。我想要一个 ListSelectionListener,其中 returns Listentry 的字符串,由用户选择。但我无法到达 String 本身。我唯一能找到的东西

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);
            }
        }
    }
});

有用的链接: