我试图设置从JTable中选择的行,我获取并比较从我的DB中收到的所有数据,然后在比较数据时,如果数据存在,它必须设置所选的行。
我已经尝试了几个函数,如setSelectionModel或setSelectionInterval,但它们都没有设置选中具有必须选择的值的行。
有什么建议吗?
这里有一些代码。。。。但不确定它是否会有帮助。。。但这是我在网上读到的。。。根本没有选择我想要的行。。。leerTripulantesVuelo带回了一个Tripulantes数组,这就是我在列表中选择那个Tripulante的过程。
ModeloTablaTripulantes tablaPil = new ModeloTablaTripulantes();
tablaPilotos.setModel(tablaPil);
tablaPilotos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ArrayList<Tripulante> trip = new ArrayList();
String tipoPersona = "t";
String numVuelo = numeroVueloListaTripulantes.getText();
try {
trip = Aerolinea.leerTripulantesVuelo(numVuelo);
for (Tripulante t: trip){
for(int i =0; i<tablaPil.getRowCount();i++){
if (tablaPil.getValueAt(i, 0).equals(t.getCedula())){
tablaPilotos.getSelectionModel().setSelectionInterval(i, i);
}
}
}
} catch (BDException ex) {
ex.printStackTrace();
}
下面是一个完整的示例,用于选择表中的第二行。我使用NetBeans,所以不关心ui的代码。jButton1ActionPerformed
中的代码包含jTable1.getSelectionModel().setSelectionInterval(row, row)
,它正是我所期望的——选择行
你确定你的程序运行到setSelectionInterval()
吗?也许字符串末尾包含空格。调试应该有助于找到答案。
package tripulante;
public class Tripulante extends javax.swing.JFrame {
public Tripulante() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"sample 1", "a"},
{"sample 2", "b"},
{"sample 3", "c"},
{"sample 4", "d"}
},
new String [] {
"Title 1", "Title 2"
}
));
jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
jScrollPane1.setViewportView(jTable1);
jButton1.setText("Select lines");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
for (int row = 0; row < jTable1.getRowCount(); row++) {
String cellContent=jTable1.getValueAt(row, 0).toString();
if (cellContent.equalsIgnoreCase("sample 2")) {
jTable1.getSelectionModel().setSelectionInterval(row, row);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Tripulante().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}