gpt4 book ai didi

java - 将表字段转换为 jCheckBox

转载 作者:行者123 更新时间:2023-12-02 07:14:38 26 4
gpt4 key购买 nike

我创建了一个 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。现在的结果是这样的: enter image description here

如何改造我的表格???

最佳答案

利用表模型,特别是通过创建一个扩展 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() 将表模型应用到表中

关于java - 将表字段转换为 jCheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com