Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a combobox that connect to database table so when i choose one of database table, it will show in a table, but there's no checkbox there. so i want to put add checkbox column when i choose one of database table in combobox.

this is my code in combobox:

Java
final String[] TABLE_NAMES = { "mtb", "bmx", "lipat", "anak" };
       javax.swing.table.TableModel model = null;
       model = new MtbDb(TABLE_NAMES[jComboBox1.getSelectedIndex() -1]);
       jTable1.setModel(model);


and this is my code in mtbdb class:
Java
public class MtbDb extends javax.swing.table.AbstractTableModel {
    private final String[] HEADERS = { "ID", "Nama Sepeda", "Merek", "Harga", "Model" };
    private ArrayList<DATA> data = new ArrayList<>();
    
    public MtbDb(String tableName) {
        try {
            Connection c = DriverManager.getConnection("jdbc:mysql://localhost/sepedadb", "root", "");
            Statement stat = c.createStatement();
            ResultSet result = stat.executeQuery("select * from " + tableName);
            while(result.next()) {
                data.add(new DATA(result.getString(1), result.getString(2), result.getString(3), result.getString(4), result.getString(5)));
            }
        } catch(SQLException e) { e.printStackTrace(); }
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        DATA d = data.get(rowIndex);
        switch(columnIndex) {
            case 0: return d.getID();
            case 1: return d.getnama_sepeda();
            case 2: return d.getmerek();
            case 3: return d.getharga();
            case 4: return d.getmodel();
            default: return null;
        }    
    }
    
    public String getColumnName(int column) {
        return HEADERS[column];
    }
}
Posted

1 solution

That method:
Java
@Override
    public Object getValueAt(int rowIndex, int columnIndex) { }


...returns an Object. So you should be able to return a complex Component for your GUI.
The table in the GUI might need to be modified too (depending on how the GUI is set up).

Have Fun!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900