我创建了一个与另一个类一起使用的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);
}
}
我想将abstract
到volatile
的列转换为jCheckBox。结果现在是这样的:
如何转换我的表???
答案 0 :(得分:2)
使用表模型,特别是通过创建扩展javax.swing.table.AbstractTableModel
并覆盖getColumnClass()
方法并指定方法为这些特定列返回Boolean.class
的类。
启动你的好地方是http://docs.oracle.com/javase/tutorial/uiswing/components/table.html,特别是Creating a Table Model上的链接
示例实现可以是:
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()