简体   繁体   中英

netbeans - Custom table model not showing in design view

I'm trying to use a custom TableModel in my java desktop application. The GUI has been made using NetBeans. But the thing is when I use the DefaultTableModel then I can see my table in the design view of the GUI.

在此处输入图片说明

But when I use my custom TableModel then It is not displayed in the design view, although it is displayed when I run the application.

在此处输入图片说明

What seems to be the problem?

Custom Table Model Code

public class BillingTableModel extends AbstractTableModel implements TableModel {

    String columnNames[] = {
        "Name", "Address", "1", "2", "3"
    };

    Object data[][] = {
        {null,null,null,null,null},
        {null,null,null,null,null},
        {null,null,null,null,null},
        {null,null,null,null,null},
        {null,null,null,null,null}
    };  
    Class types[] = new Class [] {
        String.class, String.class, String.class, String.class, String.class 
    };

    @Override
    public Class<?> getColumnClass (int columnIndex) {
        return types[columnIndex].getClass();
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    @Override
    public boolean isCellEditable (int row, int col) {
        if (col >= 2) 
            return true;
        else
            return false;
    }


    @Override
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

}

I edited the default code to include my class like this

customerTable = new javax.swing.JTable();

customerTable.setModel(new BillingTableModel());



tableScrollPane.setViewportView(customerTable);

Subclass JTable and then you can drag your custom JTable to the Netbeans designer instead of a regular JTable and your custom model will already be visible.

eg.

Put this file in your project as CustomerTable.java

public class CustomerTable extends JTable {

    public CustomerTable() {
        this.setModel(new BillingTableModel());
    }
}

Select the file and choose Run -> Compile File from the Menu. (or press F9)

Delete the JTable you have from your the Netbeans Design view, and drag CustomerTable.java from the projects window into the design view instead.

在设计器中自定义jtable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM