gpt4 book ai didi

java - JList 的 getSelectedIndex() 始终返回 -1,即使选择了一个项目

转载 作者:行者123 更新时间:2023-11-29 07:36:59 25 4
gpt4 key购买 nike

http://prntscr.com/9jhrwa “GUI 的外观”

公共(public)类 Okno1 扩展了 javax.swing.JFrame {

static Konto[]konto;
static DefaultListModel listModel;
static int indexKonta;
public Okno1() {
initComponents();
napolniKonto();
jScrollPane1.setVisible(false);
button_potrdiKonto.setVisible(false);
}

here I fill my array with Objects and add them to DefaultListModel, also I create a new list with the mentioned DefaultListModel

    listModel=new DefaultListModel();
list_konto.setModel(listModel);
konto=new Konto[4];
konto[0]=new Konto("10000/20000", "Test konto primer1");
konto[1]=new Konto("20000/30000", "Test konto primer2");
konto[2]=new Konto("50000/60000", "Test konto primer3");
konto[3]=new Konto("30000/50000", "Test konto primer4");
for (int i = 0; i < konto.length; i++) {
listModel.addElement(konto[i].getID()+" | "+konto[i].getOpis());
}

list_konto=new JList(listModel);
jScrollPane1.repaint();
}

Here I show the jScrollPanel when this button is pressed, I also show the button which must be pressed if I want to get the index of the selected element in the JList displayed

   private void button_prikaziKontoActionPerformed(java.awt.event.ActionEvent evt) {                                                    
jScrollPane1.setVisible(true);
button_potrdiKonto.setVisible(true);
//revalidate();
//repaint();
}

Here I press a button and it should get me the index of the selected item, but it keeps giving me -1 and it doesn't matter if an item on the JList is selected or is not

private void button_potrdiKontoActionPerformed(java.awt.event.ActionEvent evt) {                                                   
//indexKonta=list_konto.getSelectedIndex();
text_opisKonta.setText(Integer.toString(list_konto.getSelectedIndex()));
}

最佳答案

不清楚您的代码哪里出了问题。这个竞争性的例子可以让你孤立地研究问题。还可以考虑添加 ListSelectionListener看看效果。

myList.addListSelectionListener((ListSelectionEvent e) -> {
myLabel.setText(getSelectionIndex());
});

image

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;

/** @see http://stackoverflow.com/a/34497773/230513 */
public class Test extends JPanel {

private final String[] values = {"Value1", "Value2", "Value3", "Value4"};
private final JList myList = new JList(values);
private final JLabel myLabel = new JLabel();

public Test() {
myList.setSelectedIndex(values.length - 1);
myLabel.setText(getSelectionIndex());
this.add(myList);
this.add(myLabel);
this.add(new JButton(new AbstractAction("Show Selected Index") {

@Override
public void actionPerformed(ActionEvent e) {
myLabel.setText(getSelectionIndex());
}
}));
}

private String getSelectionIndex() {
return String.valueOf(myList.getSelectedIndex());
}

public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Test());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
});
}
}

关于java - JList 的 getSelectedIndex() 始终返回 -1,即使选择了一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34497078/

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