Reputation: 177
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);
}
};
Actual 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;
}
}
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..!
Upvotes: 1
Views: 756
Reputation: 10994
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)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);
Upvotes: 2
Reputation: 2702
@user3318622 : You need to use TableCellEditor
and TableCellRenderer
if you want to display checkbox in the last column cell and in the TableCellEditor
you need to handle ItemListener
for checkbox.
Upvotes: 1