JTable 에 각 row 마다 삭제 버튼을 달고 싶었다
첫 행 또는 마지막 행 삭제시
오류가 나서 겁나 해맸었다
ActionListener 를 모두 삭제 했다가 다시 연결하라는 조언도 있었지만
그건 내 역량을 벋어나는 문제다
당장은 clone 을 만들어 우회하여 처리했다.
만약 JTable 상에 데이터량이 어마무시깜놀화들짝헐 이라면 이 방법은 그리 좋지 못하다
일단 ...
최종 소스 투척
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import java.awt.Component; import java.util.Vector; import javax.swing.AbstractCellEditor; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; @SuppressWarnings("serial") public class ExTableAddBtn extends JDialog { private Object[] objColNms = new Object[] { "Col1", "Col2", "Del" }; public static void main(String[] args) { new ExTableAddBtn(); } private JFrame jf; private DefaultTableModel dtm; private JTable jtable; private JScrollPane jsp; public ExTableAddBtn() { jf = new JFrame("JTable Add Delete Button"); jf.setLocationRelativeTo(null); jf.setSize(300, 300); dtm = new DefaultTableModel( new Object[][] { { "data1", "data2", "data3" }, { "data4", "data5", "data6" }, { "data7", "data8", "data9" } }, objColNms); jtable = new JTable(dtm); jsp = new JScrollPane(jtable); jtable.getColumnModel().getColumn(2).setCellRenderer(new TableCell()); jtable.getColumnModel().getColumn(2).setCellEditor(new TableCell()); jf.add(jsp); jf.setVisible(true); } @SuppressWarnings("rawtypes") public void JTableRemoveRow() { int row = jtable.getSelectedRow(); if (row == -1) return; DefaultTableModel model = (DefaultTableModel) jtable.getModel(); model.removeRow(row); int rowCnt = jtable.getRowCount(); if (rowCnt > 0) { Vector vector = model.getDataVector(); Object[][] objData = new Object[vector.size()][((Vector) vector.get(0)).size()]; for (int i = 0; i < vector.size(); i++) { Vector vec = (Vector) vector.get(i); for (int j = 0; j < vec.size(); j++) { objData[i][j] = vec.get(j); } } DefaultTableModel clonModel = new DefaultTableModel(objData, objColNms); JTable clonTable = new JTable(clonModel); jf.getContentPane().removeAll(); jtable = clonTable; jsp = new JScrollPane(jtable); jtable.getColumnModel().getColumn(2).setCellRenderer(new TableCell()); jtable.getColumnModel().getColumn(2).setCellEditor(new TableCell()); jf.add(jsp); jf.revalidate(); jf.repaint(); } else { DefaultTableModel clonModel = new DefaultTableModel(null, objColNms); JTable clonTable = new JTable(clonModel); jf.getContentPane().removeAll(); jtable = clonTable; jsp = new JScrollPane(jtable); jf.add(jsp); jf.revalidate(); jf.repaint(); } } // end public void JTableRemoveRow() class TableCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer { JButton jb; public TableCell() { jb = new JButton("Del"); jb.addActionListener(e -> { JTableRemoveRow(); }); } @Override public Object getCellEditorValue() { return null; } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { return jb; } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { return jb; } } // end class TableCell extends AbstractCellEditor implements TableCellEditor, // TableCellRenderer } | cs |
끝
참고 : 자바 스윙 테이블에 버튼 입력(체크박스) 후 이벤트 처리
반응형
'JAVA' 카테고리의 다른 글
[JAVA][SWING]디렉토리만 선택 (0) | 2018.09.19 |
---|---|
[JAVA][ETC]Java 에서 Interface를 사용하여 Callback 구현하기 (0) | 2018.09.17 |
[JAVA][SWING]Button Table Example : Grid Table (0) | 2018.09.13 |
[JAVA][SWING]setBorder TitledBorder LineBorder (0) | 2018.09.13 |
[JAVA][SWING]BorderLayout in BorderLayout (0) | 2018.09.13 |