我正在尝试编写一些代码,允许用户通过单击JTable
.
我可以让程序将表中的数据输入到文本字段中,但我目前的执行方法涉及一个 JOptionPane,它出于某种奇怪的原因阻止表更改复选框值(即复选框没有t 从黑色变为勾选)。不仅如此,选择不会更新,因此最后一列中的值保持为假,即使选择应将其切换为真。
我认为这可能与以JOptionPane
某种方式覆盖选择事件有关,但我对对象的了解不够多,JOptionPane
无法说明如何。我的代码是:
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
//no rows are selected do nothing
} else {
//First find the row clicked
int selectedRow = lsm.getLeadSelectionIndex();
/*
* put a popup here to ask the user which peak to associate
* the energy with.
*/
System.out.println(selectedRow);
//Get user to associate with a peak
availablePeaks = getAvailablePeaks();
String returnVal = (String) JOptionPane.showInputDialog(
null,
"Select the peak:",
"Peak Matching",
JOptionPane.QUESTION_MESSAGE,
null,
availablePeaks, null);
System.out.println(returnVal);
//Determine the selection
int index = 0;
for (int i = 0; i < availablePeaks.length; i++) {
if (availablePeaks[i] == returnVal) {
index = i;
} else {
}
}
//Set the peak value in the peak specifier to the energy in the row
double energy = (Double) table.getValueAt(selectedRow, 0);
System.out.println(energy);
frame.getPeakSetter().getPeakSpecifiers()[index].setEnergy(energy);
frame.getPeakSetter().getPeakSpecifiers()[index].getTextField().setText("" + energy);
}
}
});
有谁知道为什么 aJOptionPane
会ListSelectionListener
阻止表格更新复选框?
谢谢!