Change Cell Editor for specific Cells with AbstractTableModel

别等时光非礼了梦想. 提交于 2020-01-11 07:21:35

问题


I have a written an AbstractTableModel for my JTable in my application. I see from tutorials that I can make the column to have a combobox by getting the column model and then the specific column for example:

TableColumn sportColumn = table.getColumnModel().getColumn(2);
...
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

But how do I do this for a specific cell or a row?


回答1:


The JTable’s default implementation is column-based. The only way to change that behavior, if you want to have row or single-cell based choices, is to create a subclass of JTable and override the method public TableCellEditor getCellEditor(int row, int column). Inside your implementation you can use the provided row and column indices to make a different choice. The JTable will always use this method to get the cell editor.




回答2:


you would need to use, override

  1. prepareEditor

  2. TableCellEditor(required to synchronize editor and renderer)



来源:https://stackoverflow.com/questions/18736206/change-cell-editor-for-specific-cells-with-abstracttablemodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!