سؤال

I am using netbeans 7.4. I created a JFrame which search data from a mysql database and display the results in a JTable. I need to make the user able to make a selection on the resulted rows(Here Im using a database which is having a table named "Vehicle_R" which contains data about vehicles. So the user should be able to select a vehicle). Here is my coding

public class CreateConnection {
Connection con;
Statement statmnt;

public CreateConnection() {
    try{
    this.con =DriverManager.getConnection("jdbc:mysql://localhost:3306/vehiclereserve","root","nadun");

    statmnt=con.createStatement();
    }
    catch(SQLException e)
    {System.out.println(e.toString());}
}
public Connection getCon()
{
    return con;
}
public Statement getStatement()
{
    return statmnt;
}

in the main i got

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try{
    CreateConnection con=new CreateConnection();
    ResultSet res=con.getStatement().executeQuery("select * from Vehicle_R");
    jTable1.setModel(DbUtils.resultSetToTableModel(res));      //rs2xml.jar library added
    //TableColumn tc=new TableColumn();
    //jTable1.addColumn(tc);



   }
catch (SQLException e)
   {
       JOptionPane.showMessageDialog(null, e.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
   }

A new column should be added with check boxes to make selections. Please consider Im new in java.

هل كانت مفيدة؟

المحلول

DbUtils.resultSetToTableModel(res));      

It depends what the DbUtils does and what TableModel is uses. Maybe the TableModel supports an "addColumn(...)" method should you can add your own column of checkboxes.

If the TableModel doesn't support that feature then you need to use a different TableModel. You can also use the DefaultTableModel. It does support an addColumn() method, so you can load the data from the database into the DefaultTableModel and then add a separate column for your checkboxes.

Check out the Table From Database Example from Table From Database. It is a simple example of loading data from the ResultSet into the DefaoltTableModel. You only need to make a few changes to add you own column of checkboxex. Basically all you nned to do is add another column heading to the "columnNames" Vector for your check boxes. Then when you process each row from the ResultSet you need to add a Boolean.false value to the "row" Vector.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top