选择SET行

时间:2015-03-25 21:41:00

标签: java jtable selection rows setting

我试图设置从JTable中选择的行,我得到并比较从我的数据库收到的所有数据,然后在比较数据是否存在时,它必须设置该行被选中。

我已尝试过多个函数,例如setSelectionModel或setSelectionInterval,并且这些函数都没有选中具有必须选择的值的行。

有什么建议吗?

下面是一些代码....不确定它会有什么帮助...但它是我在网上看到的...没有选择我想要的行... leerTripulantesVuelo带回了一系列的Tripulante这就是我的意思在列表中选择THAT 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();
    }

1 个答案:

答案 0 :(得分:0)

以下是选择表格中第二行的完整示例。 我使用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                   
}