My requirement is, I have to add a table cell renderer in a cell with data which is fetching from DB and button.
When I double click in that cell on data, the cell should be editable so that user can change the data. When I double click on button one more dialog opens which accepts data from the user as soon as I click on OK button in that dialog,the data should populate in the previous table cell. Currently, I am able to render the data and image and when I click on the button, it's opening another dialog and accepting data from the user.
My issues:
- am not able to and edit the cell
- How to capture the edited value
- how to display the accepted value in other dialog in this cell.
Currently the below image is not showing text but for other cells, I am able to show.
JTable
cell with text and button
jTableTestCases.setModel(new javax.swing.table.DefaultTableModel( new Object [][] {
},
new String [] {
"Select", "Status", "ID", "Title", "Config", "Tester", "TestCase Name"
}
) {
Class[] types = new Class [] {
java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
true, false, false, false, false, false, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
}); static class MyTableEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
JPanel jpanel;
JLabel label;
JButton button;
MyTableEditor() {
label = new JLabel();
button = new JButton("Click Me!!!");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hi" );
}
});
GridLayout gridLayout = new GridLayout(0,2);
jpanel = new JPanel();
jpanel.setLayout(gridLayout);
jpanel.setBorder(new EmptyBorder(0, 10, 0, 10));
jpanel.add(label);
jpanel.add(button);
}
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if(null!=value)
label.setText(value.toString());
jpanel.setBackground(table.getSelectionBackground());
return jpanel;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(null!=value)
label.setText(value.toString());
if(isSelected){
jpanel.setBackground(table.getSelectionBackground());
}else{
jpanel.setBackground(table.getBackground());
}
return jpanel;
}
}jTableTestCases.getColumn("TestCase Name").setCellRenderer(new MyTableEditor());
jTableTestCases.getColumn("TestCase Name").setCellEditor(new MyTableEditor());