Frage

i am using swings for creating the desktop application . i have created the functionality which provided the data from the database and insert that into the Jtable .now i want to use provide the additional facility that include the additional column with the check box and the a button to delete that perticuler column (which is checked ) when the button is clicked .i have used the netbeans and it provide the maximum drag and drop option. i am not able to figure it out that the how and where to insert the instance of the checkbox in the current code for inserting the checkbox for the each and every row . For providing the checkbox with the each row do have to generate the multiple instance of the check box

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    java.sql.Connection con = new DBConnection().getConnection();
    PreparedStatement pst;
    String Q;
    Q = "select * from userregister ";
    try {
        pst = con.prepareStatement(Q);

        ResultSet rs = null;
        rs = pst.executeQuery();
        String a, b, c, d;
        int x = 0;
     //   DefaultTableModel dt = new DefaultTableModel(data, columnNames);
        JCheckBox c1 = new JCheckBox();
        for (int i = 0; rs.next(); i++) {
            a = rs.getString(1);
            b = rs.getString(2);
            c = rs.getString(3);
            d = rs.getString(4);
       jTable2.setValueAt(a, i, 0);
       jTable2.setValueAt(b, i, 1);
       jTable2.setValueAt(c, i, 2);
       jTable2.setValueAt(d, i, 3);
     jTable2.setValueAt(, i,4);
        }


   //jTable1.setAutoscrolls(rootPaneCheckingEnabled);
  // TODO add your handling code here:
    } catch (SQLException ex) {
        Logger.getLogger(NewJFrame1.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                        

this is the methods that insert the data into the table . also i want to know that how do i be able to find out that which check box is checked and how to use the variable to respond the request of the multiple deletes . plz help

War es hilfreich?

Lösung

You have to take a look to Concepts: Editors and Renderers section of How to Use Tables tutorial.

This JCheckBox you're looking for is the default renderer/editor for Boolean class. Having said this JTable makes use of TableModel.getColumnClass() to decide the proper renderer/editor. If you use DefaultTableModel the implementation of the aforementioned method always return Object.class so you would have to override it to return Boolean.class. For instance let's say the first column will contain booleans:

DefaultTableModel model = new DefaultTableModel() {
    @Override
    Class<?> getColumnClass(int columnIndex) {
        return columnIndex == 0 ? Boolean.class : super.getColumnClass(columnIndex);
    }
};

It is all well explained in the linked tutorial.

Addendum

Another approach is shown in this Q&A: Checkbox in only one JTable Cell. This is useful when a given column may contain different types of values (booleans, numbers, strings...) so overriding getColumnClass() is not feasible. Don't think it's your case but it might be helpful.


also i want to know that how do i be able to find out that which check box is checked and how to use the variable to respond the request of the multiple deletes

Just iterate over the rows asking for the column value (true/false). If it's "selected" (true) then delete it:

TableModel model = table.getModel();
for(int i = 0; i < model.getRowCount(); i++) {
    if((Boolean)model.getValueAt(i, 0)) {
        // delete the row
     }
}

Off-topic

Database calls are time consuming tasks and may block the Event Dispatch Thread (a.k.a. EDT) causing the GUI become unresponsive. The EDT is a single and special thread where Swing components creation and update take place. To avoid block this thread consider use a SwingWorker to perform database calls in a background thread and update Swing components in the EDT. See more in Concurrency in Swing trail.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top