带有可编辑复选框的JTable [英] JTable with editable checkbox
问题描述
下面的代码是我的项目类之一。当我点击查找按钮时,会产生一个带有 JTable
的框架,一些数据将动态加载到表格中。表格的最后一列必须是带事件的复选框。
我试过这个代码用于复选框(从另一个项目中取出它......没有用)
The below code is one of my project class.It produce a frame with JTable
when I click find button some data will load to table dynamically.The last column of table must be checkbox with event.
I tried this code for checkbox (took it from another project..its not working)
DefaultTableModel dtm = new DefaultTableModel(data, colName){
public Class getColumnClass(int c) {
return (c == 5? Boolean.class : String.class);
}
};
实际课程
public class BookReturnPanel {
JPanel retunBookPanel;
JTextField txtRegNo;
JButton btnFind, btnSave;
JTable retunTable = null;
public JScrollPane jScrollPane = null;
private int i;
static Object[][] data;
String regNo = null;
Member member = null;
DefaultTableModel model = new DefaultTableModel();
/**
* Create the panel.
*/
public BookReturnPanel() {
}
public JPanel getRetunBookPanel() {
if (retunBookPanel == null) {
retunBookPanel = new JPanel();
retunBookPanel.setLayout(null);
model.addColumn("Member");
model.addColumn("Book");
model.addColumn("Issue Date");
model.addColumn("Return Date");
model.addColumn("Return");
retunTable=new JTable(model);
retunTable.setLocation(new Point(0,60));
retunTable.setSize(new Dimension(517, 386));
JLabel lblRegNo = new JLabel("Member Reg No:");
lblRegNo.setBounds(24, 40, 108, 14);
retunBookPanel.add(lblRegNo);
retunBookPanel.add(getJScrollPane(), BorderLayout.CENTER);
txtRegNo = new JTextField();
txtRegNo.setBounds(129, 37, 200, 20);
retunBookPanel.add(txtRegNo);
txtRegNo.setColumns(10);
btnFind = new JButton("Find");
btnFind.setBounds(359, 36, 91, 23);
btnFind.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (model.getRowCount() > 0) {
for (int i = model.getRowCount() - 1; i > -1; i--) {
model.removeRow(i);
}
}
regNo = txtRegNo.getText();
member = ServiceFactory.getMemberServiceImpl().findByregNo(regNo);
List<Issue> issues = ServiceFactory.getIssueServiceImpl()
.FindAllIssueByMemberId(member.getSerialNo());
for(Issue issue:issues){
Vector<Object>row=new Vector<Object>();
row.addElement( issue.getMemberId().getName());
row.addElement( issue.getBookId().getName());
row.addElement( issue.getIssueDate());
row.addElement( issue.getReturnDate());
row.addElement(issue.getStatus());
model.addRow(row);
}
}
});
retunBookPanel.add(btnFind);
btnSave = new JButton("Save");
btnSave.setBounds(425, 448, 91, 23);
retunBookPanel.add(btnSave);
}
return retunBookPanel;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(0, 60, 517, 386));
jScrollPane.setViewportView(retunTable);
}
return jScrollPane;
}
}
以上代码生成 JTable
完美但我需要在最后一栏显示 JCheckbox
。
也需要在单击复选框时添加事件..!
The above code produce JTable
perfectly but I need to show JCheckbox
at the last column.
also it need to add event when a checkbox is clicked..!
推荐答案
1) JTable
列索引从0开始,如果你想将 JCheckBox
设置为最后一列,你需要使用索引4而不是5代码。
1) JTable
columns indexes starts from 0, if you want to set JCheckBox
to last column, you need to use index 4 , not 5 as in your code.
2)为了向 JCheckBox
列添加操作,您可以使用 DefaultCellEditor
带 JCheckBox
,例如:
2)For adding action to your JCheckBox
column you can use DefaultCellEditor
with JCheckBox
, for example:
JCheckBox chb = new JCheckBox();
chb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println("action");
}
});
DefaultCellEditor editor = new DefaultCellEditor(chb) {
@Override
public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row,int column) {
Component tableCellEditorComponent = super.getTableCellEditorComponent(table, value, isSelected, row, column);
((JCheckBox)tableCellEditorComponent).setHorizontalAlignment(JCheckBox.CENTER);
return tableCellEditorComponent;
}
};
retunTable.getColumnModel().getColumn(4).setCellEditor(editor);
这篇关于带有可编辑复选框的JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!