如何从数据库中添加/显示表中的复选框列 [英] How Do I Add/Show Checkbox Column In Table From Database

查看:106
本文介绍了如何从数据库中添加/显示表中的复选框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到数据库表的组合框,所以当我选择一个数据库表时,它会显示在一个表中,但那里没有复选框。所以当我在组合框中选择一个数据库表时,我想添加复选框列。



这是我在组合框中的代码:



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:

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);





这是我在mtbdb类中的代码:



and this is my code in mtbdb class:

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];
    }
}

推荐答案

该方法:

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





...返回对象。所以你应该能够为你的GUI返回一个复杂的组件

GUI中的表也可能需要修改(取决于方式如何)设置了GUI。



玩得开心!



...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!


这篇关于如何从数据库中添加/显示表中的复选框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆