List of usage examples for javax.swing.table TableColumn setCellEditor
@BeanProperty(description = "The editor to use for cell values.") public void setCellEditor(TableCellEditor cellEditor)
From source file:com.mirth.connect.client.ui.browsers.message.MessageBrowserAdvancedFilter.java
private void initMetaDataSearchTable() { metaDataSearchTable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "Metadata", "Operator", "Value", "Ignore Case" }) { public boolean isCellEditable(int rowIndex, int columnIndex) { if (columnIndex == 3 && cachedMetaDataColumns.get(getValueAt(rowIndex, 0)) .getType() != MetaDataColumnType.STRING) { return false; }/*from w ww .j a va 2 s . co m*/ return true; } @Override public void setValueAt(Object value, int row, int column) { int metaDataColumnIndex = findColumn("Metadata"); int operatorColumnIndex = findColumn("Operator"); int valueColumnIndex = findColumn("Value"); int ignoreCaseColumnIndex = findColumn("Ignore Case"); if (column == valueColumnIndex) { MetaDataColumn metaDataColumn = cachedMetaDataColumns.get(getValueAt(row, metaDataColumnIndex)); if (StringUtils.isNotEmpty((String) value)) { try { metaDataColumn.getType().castValue(value); } catch (MetaDataColumnException e) { parent.alertError(parent, "Invalid value for column type " + metaDataColumn.getType().toString()); return; } } } else if (column == metaDataColumnIndex) { if (!value.equals(getValueAt(row, metaDataColumnIndex))) { MetaDataSearchOperator operator = MetaDataSearchOperator.EQUAL; super.setValueAt(operator, row, operatorColumnIndex); MetaDataColumn metaDataColumn = cachedMetaDataColumns.get(value); if (metaDataColumn.getType() != MetaDataColumnType.STRING) { super.setValueAt(Boolean.FALSE, row, ignoreCaseColumnIndex); } } super.setValueAt("", row, valueColumnIndex); } super.setValueAt(value, row, column); } }); metaDataSearchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); metaDataSearchTable.setDragEnabled(false); metaDataSearchTable.setSortable(false); metaDataSearchTable.getTableHeader().setReorderingAllowed(false); addMetaDataSearchButton.setEnabled(!messageBrowser.getMetaDataColumns().isEmpty()); metaDataSearchTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { deleteMetaDataSearchButton.setEnabled(getSelectedRow(metaDataSearchTable) != -1); } }); if (Preferences.userNodeForPackage(Mirth.class).getBoolean("highlightRows", true)) { metaDataSearchTable.setHighlighters(HighlighterFactory .createAlternateStriping(UIConstants.HIGHLIGHTER_COLOR, UIConstants.BACKGROUND_COLOR)); } List<MetaDataColumn> metaDataColumns = messageBrowser.getMetaDataColumns(); cachedMetaDataColumns.clear(); String[] metaDataNames = new String[metaDataColumns.size()]; for (int i = 0; i < metaDataColumns.size(); i++) { String columnName = metaDataColumns.get(i).getName(); metaDataNames[i] = columnName; cachedMetaDataColumns.put(columnName, metaDataColumns.get(i)); } MirthComboBoxTableCellEditor metaDataEditor = new MirthComboBoxTableCellEditor(metaDataSearchTable, metaDataNames, 1, false, null); metaDataEditor.getComboBox().setAutoResizeDropdown(true); TableColumn metaDataColumn = metaDataSearchTable.getColumnModel().getColumn(0); metaDataColumn.setCellRenderer(new MirthComboBoxTableCellRenderer(metaDataNames)); metaDataColumn.setCellEditor(metaDataEditor); metaDataColumn.setMinWidth(METADATA_NAME_COLUMN_WIDTH); metaDataColumn.setMaxWidth(METADATA_NAME_COLUMN_WIDTH * 2); metaDataColumn.setPreferredWidth(METADATA_NAME_COLUMN_WIDTH); // Need to create this custom editor since the combo box values are dynamic based on metadata column type. MirthComboBoxTableCellEditor operatorEditor = new MirthComboBoxTableCellEditor(metaDataSearchTable, MetaDataSearchOperator.values(), 1, false, null) { public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { MetaDataColumn metaDataColumn = cachedMetaDataColumns.get(table.getValueAt(row, 0)); comboBox.setModel(new DefaultComboBoxModel( MetaDataSearchOperator.valuesForColumnType(metaDataColumn.getType()))); return super.getTableCellEditorComponent(table, value, isSelected, row, column); } }; TableColumn operatorColumn = metaDataSearchTable.getColumnModel().getColumn(1); operatorColumn.setCellRenderer(new MirthComboBoxTableCellRenderer(MetaDataSearchOperator.values())); operatorColumn.setCellEditor(operatorEditor); operatorColumn.setMinWidth(METADATA_OPERATOR_COLUMN_WIDTH); operatorColumn.setMaxWidth(METADATA_OPERATOR_COLUMN_WIDTH); TableColumn caseColumn = metaDataSearchTable.getColumnModel().getColumn(3); caseColumn.setMinWidth(METADATA_CASE_COLUMN_WIDTH); caseColumn.setMaxWidth(METADATA_CASE_COLUMN_WIDTH); deleteMetaDataSearchButton.setEnabled(false); }
From source file:org.jdal.swing.ListTableModel.java
/** * Create a TableColumnModel for JTable. * Try to use sizes and cell renderers from property descriptors. * @return a new TableColumnModel based on PropertyDescriptors *//*from w w w . j a v a 2 s .com*/ public TableColumnModel getTableColumnModel() { TableColumnModel tcm = new DefaultTableColumnModel(); int baseIndex = 0; if (usingChecks) { TableColumn tableColumn = new TableColumn(0); tableColumn.setMaxWidth(50); tcm.addColumn(tableColumn); baseIndex++; } for (int i = 0; i < columnNames.size(); i++) { String name = this.columnNames.get(i); TableColumn tableColumn = new TableColumn(baseIndex + i); tableColumn.setHeaderValue(displayNames.get(i)); if (pds != null && pds.size() > 0) { PropertyDescriptor descriptor = pds.get(i); // property values for TableColumns if (descriptor != null) { Integer maxWidth = getColumnWidth(name); if (maxWidth != null) tableColumn.setMaxWidth(maxWidth.intValue()); tableColumn.setCellRenderer(getColumnRenderer(name)); tableColumn.setCellEditor(getColumnEditor(name)); } } tcm.addColumn(tableColumn); } if (usingActions) { baseIndex += columnNames.size(); for (int i = 0; i < actions.size(); i++) { TableColumn tableColumn = new TableColumn(baseIndex + i); tableColumn.setCellRenderer(new ActionCellRenderer()); tableColumn.setMaxWidth(50); // tableColumn.setCellEditor(new ActionCellEditor()) tcm.addColumn(tableColumn); } } return tcm; }
From source file:com.sec.ose.osi.ui.frm.main.manage.ManagedProjectTableModel.java
public void setColumnType(JTable table) { table.setShowVerticalLines(false);/*from w ww . j a va 2s. c om*/ table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setColumnSelectionAllowed(false); table.setRowSelectionAllowed(false); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setRowHeight(30); JTableHeader header = table.getTableHeader(); header.setPreferredSize(new java.awt.Dimension(table.getTableHeader().getWidth(), 30)); header.setFont(new Font("Arial", Font.BOLD, 12)); header.setReorderingAllowed(false); TableColumnModel cm = table.getColumnModel(); TableColumn col = null; analyzeHeader = new CheckBoxHeader(new CheckboxHeaderItemListener(table, COL_ANALYZE_TARGET), "Analyze Target"); col = cm.getColumn(COL_ANALYZE_TARGET); col.setHeaderRenderer(analyzeHeader); JCheckBox chkbox = new JCheckBox(); chkbox.setBackground(Color.white); chkbox.setHorizontalAlignment(JLabel.CENTER); col.setCellRenderer(new DefaultTableCellRenderer() { private static final long serialVersionUID = 1L; public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { ManagedProjectTableModel model = (ManagedProjectTableModel) table.getModel(); String sPrjName = (String) table.getValueAt(row, ManagedProjectTableModel.COL_PROJECT_NAME); OSIProjectInfo item = model.getProjectInfo(sPrjName); JCheckBox chkbox = new JCheckBox(); chkbox.setSelected(((Boolean) value).booleanValue()); if (item != null) { chkbox.setEnabled(item.isLocationValid()); } chkbox.setHorizontalAlignment(JLabel.CENTER); chkbox.setBackground(Color.white); return (Component) chkbox; } }); col.setCellEditor(new DefaultCellEditor(chkbox)); col = cm.getColumn(COL_ANALYZE_STATUS); col.setCellRenderer(new StatusIconCellRenderer()); col = cm.getColumn(COL_SOURCE_LOCATION); col.setCellRenderer(new FileBrowseCellRenderer()); col.setCellEditor(new FileBrowseCellEditor()); }
From source file:grupob.TipoProceso.java
private void cargarDatosLocal() { tableModel = new LocalTableModel(); tblLocal.setModel(tableModel);/* www . j av a 2s . c om*/ /* DefaultTableModel modelo = (DefaultTableModel)jTable8.getModel(); listaLocales = Manager.queryAllLocales(); modelo.setRowCount(0); String datos[] = new String[4]; for (int i = 0; i < listaLocales.size(); i++) { datos[0] = listaLocales.get(i).getNombre(); if(listaLocales.get(i).getCantidadVotantesRegistrados() == 0){ datos[1] =""; }else{ datos[1] = Long.toString(listaLocales.get(i).getCantidadVotantesRegistrados()); } datos[2] = Integer.toString(listaLocales.get(i).getIdDistrito()); // datos[3] = Integer.toString(listaLocales.get(i).getIdTipo()); modelo.addRow(datos); } TableColumn colum1 = null; colum1 = jTable8.getColumnModel().getColumn(0); colum1.setPreferredWidth(40); TableColumn colum2 = null; colum2 = jTable8.getColumnModel().getColumn(1); colum2.setPreferredWidth(5); TableColumn colum3 = null; colum3 = jTable8.getColumnModel().getColumn(2); colum3.setPreferredWidth(40); TableColumn colum4 = null; colum4 = jTable8.getColumnModel().getColumn(3); colum4.setPreferredWidth(40); colum3.setPreferredWidth(10); */ // jTable8.getColumn("Region").setCellEditor(new ComboBoxEditor()); TipoProcesoVotacion tipoLocal = Manager.queryProcesoById(4); Calendar cal = Calendar.getInstance(); Date dateActual = cal.getTime(); if (tipoLocal != null && tipoLocal.getId() != 0) { if (!tipoLocal.getFechaInicio2().after(dateActual)) { dpFechaInicio1Local.setDate(tipoLocal.getFechaInicio1().getTime()); dpFechaInicio2Local.setDate(tipoLocal.getFechaInicio2().getTime()); dpFechaFin1Local.setDate(tipoLocal.getFechaFin1().getTime()); dpFechaFin2Local.setDate(tipoLocal.getFechaFin2().getTime()); txtPorcentajeLocal.setText("" + tipoLocal.getPorcentajeMinimo() * 100); } if ((tipoLocal.getFechaInicio1().before(dateActual)) && (cal.before(tipoLocal.getFechaFin2()))) { btnGuardarProcLocal.setEnabled(false); } if (tipoLocal.getFechaFin2().before(dateActual)) { btnGuardarProcLocal.setEnabled(false); } } TableColumn column = tblLocal.getColumnModel().getColumn(2); DistritoComboBox dist = new DistritoComboBox(); column.setCellEditor(dist); tblLocal.getColumnModel().getColumn(3).setCellRenderer(new ButtonEliminarLocales()); tblLocal.getColumnModel().getColumn(3).setCellEditor(new ButtonEliminarLocales()); if (listaLocales != null) { // TableColumn column = jTable8.getColumnModel().getColumn(2); // column.setCellEditor(new DistritoComboBox()); /* jTable8.getColumn("Eliminar").setCellRenderer(new ButtonRenderer()); jTable8.getColumn("Eliminar").setCellEditor(new ButtonEliminarLocales(new JCheckBox()));*/ } }
From source file:grupob.TipoProceso.java
public TipoProceso() { initComponents();//from w ww. j a v a 2s . c o m initInstitucional(); // listaRegiones.add(new Region(1,"Lima",15000)); // listaRegiones.add(new Region(1,"Arequipa",10000)); // listaRegiones.add(new Region(1,"Junin",12000)); TipoProcesoVotacion tipoNacional = Manager.queryProcesoById(1); TipoProcesoVotacion tipoRegional = Manager.queryProcesoById(2); TipoProcesoVotacion tipoDistrital = Manager.queryProcesoById(3); Calendar cal = Calendar.getInstance(); Date dateActual = cal.getTime(); textConfGeneral.setText(Recorte.rutaGeneral); textConfHuellas.setText(Recorte.rutaHuella); textConfFirmas.setText(Recorte.rutaFirma); /* Recorte.rutaGeneral = FramePrincipal.rutaGeneral; Recorte.rutaHuella = FramePrincipal.rutaHuella; Recorte.rutaFirma = FramePrincipal.rutaFirma;*/ if (tipoNacional != null && tipoNacional.getId() != 0) { if (!tipoNacional.getFechaInicio2().after(dateActual)) { fechai1Nacional.setDate(tipoNacional.getFechaInicio1().getTime()); fechai2Nacional.setDate(tipoNacional.getFechaInicio2().getTime()); fechaf1Nacional.setDate(tipoNacional.getFechaFin1().getTime()); fechaf2Nacional.setDate(tipoNacional.getFechaFin2().getTime()); } if ((tipoNacional.getFechaInicio1().before(dateActual)) && (cal.before(tipoNacional.getFechaFin2()))) { botonGuardarNacional.setEnabled(false); } if (tipoNacional.getFechaFin2().before(dateActual)) { botonGuardarNacional.setEnabled(true); } } if (tipoRegional != null && tipoRegional.getId() != 0) { if (!tipoRegional.getFechaInicio2().after(dateActual)) { fechai1Regiones.setDate(tipoRegional.getFechaInicio1().getTime()); fechai2Regiones.setDate(tipoRegional.getFechaInicio2().getTime()); fechaf1Regiones.setDate(tipoRegional.getFechaFin1().getTime()); fechaf2Regiones.setDate(tipoRegional.getFechaFin2().getTime()); porcentajeRegional.setText("" + tipoRegional.getPorcentajeMinimo() * 100); } if ((tipoRegional.getFechaInicio1().before(dateActual)) && (cal.before(tipoRegional.getFechaFin2()))) { botonGuardarRegional.setEnabled(false); } if (tipoRegional.getFechaFin2().before(dateActual)) { botonGuardarRegional.setEnabled(true); } } if (tipoDistrital != null && tipoDistrital.getId() != 0) { if (!tipoRegional.getFechaInicio2().after(dateActual)) { fechai1Distritos.setDate(tipoDistrital.getFechaInicio1().getTime()); fechai2Distritos.setDate(tipoDistrital.getFechaInicio2().getTime()); fechaf1Distritos.setDate(tipoDistrital.getFechaFin1().getTime()); fechaf2Distritos.setDate(tipoDistrital.getFechaFin2().getTime()); porcentajeDistrital.setText("" + tipoDistrital.getPorcentajeMinimo() * 100); } if ((tipoDistrital.getFechaInicio1().before(dateActual)) && (cal.before(tipoDistrital.getFechaFin2()))) { botonGuardarDistrital.setEnabled(false); // addRowRegional.setEnabled(false); // jTable6.setEnabled(false); } if (tipoDistrital.getFechaFin2().before(dateActual)) { botonGuardarDistrital.setEnabled(true); // addRowRegional.setEnabled(true); } } agregarDatos(); agregarDatosDistritos(); if (listaRegiones != null) { jTableRegiones.getColumn("Eliminar").setCellRenderer(new ButtonRenderer()); jTableRegiones.getColumn("Eliminar").setCellEditor(new botonEliminarRegiones()); } if (listaDistritos != null) { jTableDistritos.getColumn("Eliminar").setCellRenderer(new ButtonRenderer()); jTableDistritos.getColumn("Eliminar").setCellEditor(new botonEliminarDistritos()); } TableColumn sColumn = jTableDistritos.getColumnModel().getColumn(2); ArrayList<Region> lReg = Manager.queryAllRegion(); JComboBox comboBox = new JComboBox(); for (int i = 0; i < lReg.size(); i++) { comboBox.addItem(lReg.get(i).getNombre()); } sColumn.setCellEditor(new DefaultCellEditor(comboBox)); TableColumn instColumn = tblInstitucional.getColumnModel().getColumn(2); ArrayList<Local> lLoc = Manager.queryAllLocales(); JComboBox comboBoxLocal = new JComboBox(); for (int i = 0; i < lLoc.size(); i++) { comboBoxLocal.addItem(lLoc.get(i).getNombre()); } instColumn.setCellEditor(new DefaultCellEditor(comboBoxLocal)); ChangeListener changeListener = new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource(); int index = sourceTabbedPane.getSelectedIndex(); switch (index) { case 3: cargarDatosLocal(); return; default: return; } } }; jconfiguracion.addChangeListener(changeListener); }
From source file:com.aw.swing.mvp.binding.component.BndSJTable.java
private void configureEditableCell(TableColumn col, ColumnInfo columnInfo) { TableCellRenderer tableCellRenderer = new DefaultEditableCellRenderer(columnInfo); ((DefaultTableCellRenderer) tableCellRenderer).setHorizontalAlignment(columnInfo.getAlignment()); TableCellEditor tableCellEditor = null; Font font = ((DefaultEditableCellRenderer) tableCellRenderer).getFont(); if (columnInfo.getCellEditorValuesProvider() != null) { JComponentCellRenderer jComponentCellRenderer = new JComponentCellRenderer(); JComponentCellEditor jComponentCellEditor = new JComponentCellEditor(columnInfo, font); jComponentCellEditor.setCellRenderer(jComponentCellRenderer); jComponentCellEditor.setCellEditorProvider(columnInfo.getCellEditorValuesProvider()); col.setCellRenderer(jComponentCellRenderer); col.setCellEditor(jComponentCellEditor); columnInfo.setSpecialCellRenderer(jComponentCellRenderer); return;// w ww. j a v a 2 s. c om } if (columnInfo.getColumnEditorType() == ColumnInfo.TEXT_BOX) { ((DefaultEditableCellRenderer) tableCellRenderer).setCellColorChanger(cellColorChanger); tableCellEditor = CellEditorFactory.getJTextFielCellEditor(columnInfo, jTable, font); } else if (columnInfo.getColumnEditorType() == ColumnInfo.TEXT_AREA) { tableCellEditor = new JTextAreaCellEditor(getJTable(), columnInfo, font); } else if (columnInfo.getColumnEditorType() == ColumnInfo.CHECK_BOX) { tableCellRenderer = new JCheckBoxCellRenderer(columnInfo, columnInfo.getValueTrue()); ((JCheckBoxCellRenderer) tableCellRenderer).setBndSJTable(this); tableCellEditor = CellEditorFactory.getJCheckBoxEditor(columnInfo, jTable, getCellEditableFocusListener()); } else if (columnInfo.getColumnEditorType() == ColumnInfo.RADIO_BUTTON) { tableCellRenderer = new JRadioButtonCellRenderer(columnInfo.getValueTrue()); tableCellEditor = new JRadioButtonCellEditor(new JCheckBox(), columnInfo.getValueTrue()); } else if (columnInfo.getColumnEditorType() == ColumnInfo.COMBO_BOX) { if (columnInfo.hasParentCmps()) { CellDropDownProvider cellDropDownProvider = columnInfo.getCellDropDownProvider(this); tableCellRenderer = new JComboBoxDependentCellRenderer(columnInfo, new JComboBoxDependentCellRendererModel(cellDropDownProvider)); JComboBoxDependentCellEditorModel comboBoxModel = new JComboBoxDependentCellEditorModel( cellDropDownProvider); final JComboBox jComboBox = getJComboBox(comboBoxModel); JComboBoxDependentCellEditor jComboBoxCellEditor = new JComboBoxDependentCellEditor(jComboBox); tableCellEditor = jComboBoxCellEditor; } else { JComboBoxCellRenderer comboBoxCellRenderer = new JComboBoxCellRenderer(columnInfo, new JComboBoxCellRendererModel(columnInfo.getMetaLoader())); comboBoxCellRenderer.setEditingRowPolicy(editingRowPolicy); tableCellRenderer = comboBoxCellRenderer; JComboBoxCellEditorModel comboBoxModel = new JComboBoxCellEditorModel(); comboBoxModel.setMetaLoader(columnInfo.getMetaLoader()); final JComboBox jComboBox = getJComboBox(comboBoxModel); JComboBoxCellEditor jComboBoxCellEditor = new JComboBoxCellEditor(jComboBox); jComboBoxCellEditor.setCol(columnInfo.getIdx()); tableCellEditor = jComboBoxCellEditor; } } else if (columnInfo.getColumnEditorType() == ColumnInfo.FILE_CHOOSER) { tableCellEditor = new JFileChooserCellEditor(); } else if (columnInfo.getColumnEditorType() == ColumnInfo.BUTTON) { tableCellEditor = new JButtonCellEditor(columnInfo); tableCellRenderer = new JButtonCellRenderer(); } else if (columnInfo.getColumnEditorType() == ColumnInfo.LINK) { tableCellEditor = new JLabelLinkCellEditor(columnInfo); //tableCellEditor = new JLabelLinkCellRenderer(); JLabelLinkCellRenderer linkCellRenderer = new JLabelLinkCellRenderer(); linkCellRenderer.setColumnInfo(columnInfo); linkCellRenderer.setCellColorChanger(cellColorChanger); linkCellRenderer.setHorizontalAlignment(columnInfo.getAlignment()); linkCellRenderer.setTableEditable(isEditable()); if (isAuditingColumn(columnInfo.getFieldName())) { linkCellRenderer.setCustomBackground(AUDITING_COLUMN_COLOR); } tableCellRenderer = linkCellRenderer; } col.setCellRenderer(tableCellRenderer); col.setCellEditor(tableCellEditor); }
From source file:net.jradius.client.gui.JRadiusSimulator.java
/** * This method initializes attributesTable * // ww w . j a va 2 s . c o m * @return javax.swing.JTable */ private JTable getAttributesTable() { if (attributesTable == null) { attributesTable = new JTable(attributesTableModel); TableColumn col = attributesTable.getColumnModel().getColumn(7); col.setCellEditor(new ValueTableCellEditor()); for (int i = 0; i < attributesTableModel.getColumnCount(); i++) { col = attributesTable.getColumnModel().getColumn(i); if (i == 0 || i == 7) { col.setPreferredWidth(120); } else { col.setPreferredWidth(40); } } } return attributesTable; }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java
protected void addGeneratedColumnInternal(String columnId, ColumnGenerator generator, Class<? extends com.haulmont.cuba.gui.components.Component> componentClass) { Column col = getColumn(columnId);// ww w. jav a 2 s .c o m Column associatedRuntimeColumn = null; if (col == null) { col = addRuntimeGeneratedColumn(columnId); associatedRuntimeColumn = col; } tableModel.addGeneratedColumn(col); TableColumn tableColumn = getColumn(col); DesktopTableCellEditor cellEditor = new DesktopTableCellEditor(this, generator, componentClass); tableColumn.setCellEditor(cellEditor); tableColumn.setCellRenderer(cellEditor); cellEditor.setAssociatedRuntimeColumn(associatedRuntimeColumn); generatedColumnsCount++; packRows(); repaintImplIfNeeded(); }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java
protected Column addRuntimeGeneratedColumn(String columnId) { // store old cell editors / renderers TableCellEditor[] cellEditors = new TableCellEditor[tableModel.getColumnCount() + 1]; TableCellRenderer[] cellRenderers = new TableCellRenderer[tableModel.getColumnCount() + 1]; for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn tableColumn = getColumn(tableModelColumn); cellEditors[i] = tableColumn.getCellEditor(); cellRenderers[i] = tableColumn.getCellRenderer(); }// w w w .j a va 2s . c o m } // if column with columnId does not exists then add new to model Column col = new Column(columnId, columnId); col.setEditable(false); columns.put(col.getId(), col); // do not touch columnsOrder, it will be synced from table model if (tableModel != null) { tableModel.addColumn(col); } // reassign column identifiers setColumnIdentifiers(); // reattach old generated columns for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn tableColumn = getColumn(tableModelColumn); if (cellEditors[i] != null) { tableColumn.setCellEditor(cellEditors[i]); } if (cellRenderers[i] != null) { tableColumn.setCellRenderer(cellRenderers[i]); } } } return col; }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java
@Override public void removeColumn(Column column) { if (column == null) { return;// ww w. j a v a 2 s . c o m } String name; if (column.getId() instanceof MetaPropertyPath) { MetaPropertyPath metaPropertyPath = (MetaPropertyPath) column.getId(); name = metaPropertyPath.getMetaProperty().getName(); editableColumns.remove(metaPropertyPath); } else { name = column.getId().toString(); } TableColumn tableColumn = null; Iterator<TableColumn> columnIterator = getAllColumns().iterator(); while (columnIterator.hasNext() && (tableColumn == null)) { TableColumn xColumn = columnIterator.next(); Object identifier = xColumn.getIdentifier(); if (identifier instanceof String && identifier.equals(name)) { tableColumn = xColumn; } else if (column.equals(identifier)) { tableColumn = xColumn; } } if (tableColumn != null) { // store old cell editors / renderers Map<Object, TableCellEditor> cellEditors = new HashMap<>(); Map<Object, TableCellRenderer> cellRenderers = new HashMap<>(); for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn oldColumn = getColumn(tableModelColumn); cellEditors.put(tableModelColumn.getId(), oldColumn.getCellEditor()); cellRenderers.put(tableModelColumn.getId(), oldColumn.getCellRenderer()); } } impl.getColumnModel().removeColumn(tableColumn); impl.removeColumn(tableColumn); columns.remove(column.getId()); columnsOrder.remove(column); if (tableModel != null) { tableModel.removeColumn(column); } // reassign column identifiers setColumnIdentifiers(); // reattach old generated columns for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn oldColumn = getColumn(tableModelColumn); if (cellEditors.containsKey(tableModelColumn.getId())) { oldColumn.setCellEditor(cellEditors.get(tableModelColumn.getId())); } if (cellRenderers.containsKey(tableModelColumn.getId())) { oldColumn.setCellRenderer(cellRenderers.get(tableModelColumn.getId())); } } } packRows(); repaintImplIfNeeded(); } column.setOwner(null); }