I am having trouble adding a JButton to a JTable. I can render it using CellRenderer, and get the look of JButton. But when I use CellEditor to add a JButton to the JTable, nothing seems clickable.
the TableCellEditor implementation class is:
package codesample;
import java.awt.Component;
import java.util.EventObject;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
import javax.swing.JButton;
import javax.swing.JSpinner;
import javax.swing.JLabel;
public class InputEditor implements TableCellEditor {
protected TableModel tableModel;
public InputEditor(TableModel tableModel) {
this.tableModel = tableModel;
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (column == CodeSample.BUY)
return new JButton("Buy");
if (column == CodeSample.SELL)
return new JButton("Sell");
if (column == CodeSample.QUANTITY)
return new JSpinner();
return new JLabel();
}
public Object getCellEditorValue() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public boolean stopCellEditing() {
return false;
}
public void removeCellEditorListener(CellEditorListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void addCellEditorListener(CellEditorListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void cancelCellEditing() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
and I thought it returns a JButton or JSpinner when the BUY, SELL, QUANTITY columns are selected. I am sure these values got called correctly because my cell renderer works fine. I can see the renderered JButton and JSpinner, just not able to push it down. I believe my JButton/JSpinner didn't actually get insert into JTable, and all I am seeing so far is rendered JButton/JSpinner.
In my main function, I added the CellEditor to the JTable:
public static void main(String[] args) {
// reading from file, internet, or database and feed it into a DataModel
DataModel data = initData();
// initialize a JTable with the DataModel
JTable table = initTable(data);
// get TableColumnModel from the table
TableColumnModel tcm = table.getColumnModel();
// initialize the CellRenderers for visual display
InputRenderer IRenderer = new InputRenderer();
CurrencyRenderer CRenderer = new CurrencyRenderer();
// initialize the CellEditors for interactive feedback
InputEditor IEditor = new InputEditor(table.getModel());
// pick the column, set its renderer and editor
TableColumn tc = tcm.getColumn(BUY);
tc.setCellRenderer(IRenderer);
tc.setCellEditor(IEditor); // here
tc = tcm.getColumn(SELL);
tc.setCellRenderer(IRenderer);
tc = tcm.getColumn(QUANTITY);
tc.setCellRenderer(IRenderer);
tc = tcm.getColumn(PRICE);
tc.setCellRenderer(CRenderer);
// add the JTable to a JScrollPane
JScrollPane scrollPane = new JScrollPane(table);
// add JFrame
JFrame frame = new JFrame("CodeSample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
// add scroll pane to the frame
frame.add(scrollPane, BorderLayout.CENTER);
// add dataChangeListener to data to listen to the event changes
frame.setSize(450, 150);
frame.setVisible(true);
}