List of utility methods to do JTable Model
int | compactDefaultTableModel(DefaultTableModel defaultTableModel) compact Default Table Model int destIndex = 0; int lastSourceIndexNonEmpty = -1; int count = 0; for (int sourceIndex = 0; sourceIndex < defaultTableModel.getRowCount(); sourceIndex++) { Object object = defaultTableModel.getValueAt(sourceIndex, 0); if (object != null) { String entry = object.toString().trim(); entry = entry.trim(); ... |
int | convertModelIndexToView(JTable table, int modelIndex) Converts an index as it appears in a model to one in the view for (int col = 0; col < table.getColumnCount(); col++) { if (table.getColumnModel().getColumn(col).getModelIndex() == modelIndex) { return col; return -1; |
int[] | convertSelectionToModel(JTable table) Converts the selected view indexes to model indexes. int[] selected = table.getSelectedRows(); for (int i = 0; i < selected.length; i++) { selected[i] = table.convertRowIndexToModel(selected[i]); return selected; |
List | convertTableToList(DefaultTableModel table) Returns a list as a table with the first line representing the name of the columns. List<String> schedule = new Vector<String>(); StringBuilder builder = new StringBuilder(); int numCol = table.getColumnCount(); int numRow = table.getRowCount(); for (int i = 0; i < numCol; i++) { builder.append(table.getColumnName(i)).append(" "); schedule.add(builder.toString()); ... |
void | dumpToText(TableModel m, String filename) dump To Text try { PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename))); int nc = m.getColumnCount(); int nr = m.getRowCount(); StringBuffer buf = new StringBuffer(nc * 30); for (int i = 0; i < nc; i++) { buf.append("\t").append(m.getColumnName(i)); writer.println(buf.toString()); for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { writer.print("\t"); writer.print(m.getValueAt(i, j).toString()); writer.println(); writer.close(); } catch (IOException ioe) { System.err.println("Error " + ioe.getMessage() + "dumping table to text"); |
void | fireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel, final JTable table) DOCUMENT ME! final int selection_view = table.getSelectedRow(); int selection_model_tmp = -1; if (selection_view > -1) { selection_model_tmp = table.convertRowIndexToModel(selection_view); final int selection_model = selection_model_tmp; tableModel.fireTableDataChanged(); SwingUtilities.invokeLater(new Runnable() { ... |
DefaultTableModel | getDefaultTableModel() get Default Table Model return new DefaultTableModel() { @Override public boolean isCellEditable(int row, int column) { return false; }; |
int | getSelectedModelIndex(JTable table) get Selected Model Index int selectedRow = table.getSelectedRow(); if (selectedRow < 0) { return -1; return rowToModelIndex(table, selectedRow); |
LinkedHashMap | getTableContent(TableModel table) get Table Content int rowCount = table.getRowCount(); LinkedHashMap<String, String> result = new LinkedHashMap<>(); for (int row = 0; row < rowCount; row++) { String key = (String) table.getValueAt(row, 0); if (TextUtils.isEmpty(key)) continue; String value = (String) table.getValueAt(row, 1); result.put(key, value); ... |
void | insertTableModelListener(TableModel model, TableModelListener l, int index) insert Table Model Listener if (!(model instanceof AbstractTableModel)) { model.addTableModelListener(l); return; TableModelListener[] listeners = ((AbstractTableModel) model).getTableModelListeners(); for (TableModelListener listener : listeners) { model.removeTableModelListener(listener); for (int i = 0; i < listeners.length; i++) { TableModelListener listener = listeners[i]; if (index == i) { model.addTableModelListener(l); model.addTableModelListener(listener); if ((index < 0) || (index > listeners.length - 1)) model.addTableModelListener(l); |