0

我创建了一个与另一个类一起使用的 jTable 类。这里的代码:

public class Data_Table extends JFrame{
    DefaultTableModel dtm;
    JTable table;
    JScrollPane scrollPane;
    JFrame ventana;
    JButton button1,button2;
    JPanel pCentral,pSouth,pWindow;

    public void init() {
        String[] columnNames = {"CBD","abstract","final","native","private","protected","public",
                                "static","strictfp","synchronized","transient","volatile"};
        dtm = new DefaultTableModel(columnNames,0);     
        table = new JTable(dtm);
        scrollPane = new JScrollPane(table);

        button1 = new JButton("Ok");
        button2 = new JButton("Cancel");
    }

    public void addData(Object[] data) {
        dtm.addRow(data);
    }

    public void createWindow() {
        pCentral=new JPanel();
        pCentral.add(scrollPane);

        pSouth=new JPanel();
        pSouth.add(button1);
        pSouth.add(button2);

        pWindow=new JPanel(new BorderLayout());

        pWindow.add(pCentral,BorderLayout.CENTER);
        pWindow.add(pSouth,BorderLayout.SOUTH);

        ventana=new JFrame("");
        ventana.setContentPane(pWindow);

        ventana.add(scrollPane);
        ventana.setSize(1000,200);
        ventana.setLocationRelativeTo(null);
        ventana.setVisible(true);
    }
}

我想将列从abstracttovolatile转换为 jCheckBox。现在的结果是这样的: 在此处输入图像描述

我怎样才能改变我的桌子???

4

1 回答 1

2

利用表模型,特别是通过创建一个扩展javax.swing.table.AbstractTableModel和覆盖该getColumnClass()方法的类并指定该方法返回Boolean.class这些特定列。

一个很好的起点是http://docs.oracle.com/javase/tutorial/uiswing/components/table.html特别是关于创建表模型的链接

一个示例实现可能是:

    public class MyTableModel extends AbstractTableModel {
        /* Implement the various abstract methods and override any 
         * other methods you need to
         */

        public Class<?> getColumnClass ( int columnIndex ) {
            if ( (columnIndex == 1) || (columnIndex == 11 ) ) {
                return Boolean.class;
            }
        }
    }

之后,您将使用JTable' 方法将表格模型应用于您的表格,setModel()

于 2013-02-26T10:40:41.600 回答