List of usage examples for javax.swing DefaultCellEditor DefaultCellEditor
public DefaultCellEditor(final JComboBox<?> comboBox)
DefaultCellEditor
object that uses a combo box. From source file:org.pentaho.ui.xul.swing.tags.SwingTree.java
private TableCellEditor getCellEditor(final SwingTreeCol col) { return new DefaultCellEditor(new JComboBox()) { JComponent control;/*from ww w. jav a 2 s.co m*/ @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, final int column) { Component comp; ColumnType colType = col.getColumnType(); if (colType == ColumnType.DYNAMIC) { colType = ColumnType.valueOf(extractDynamicColType(elements.toArray()[row], column)); } final XulTreeCell cell = getRootChildren().getItem(row).getRow().getCell(column); switch (colType) { case CHECKBOX: final JCheckBox checkbox = new JCheckBox(); final JTable tbl = table; checkbox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { SwingTree.this.table.setValueAt(checkbox.isSelected(), row, column); tbl.getCellEditor().stopCellEditing(); } }); control = checkbox; if (value instanceof String) { checkbox.setSelected(((String) value).equalsIgnoreCase("true")); //$NON-NLS-1$ } else if (value instanceof Boolean) { checkbox.setSelected((Boolean) value); } else if (value == null) { checkbox.setSelected(false); } if (isSelected) { checkbox.setBackground(Color.LIGHT_GRAY); } comp = checkbox; checkbox.setEnabled(!cell.isDisabled()); break; case EDITABLECOMBOBOX: case COMBOBOX: Vector val = (value != null && value instanceof Vector) ? (Vector) value : new Vector(); final JComboBox comboBox = new JComboBox(val); if (isSelected) { comboBox.setBackground(Color.LIGHT_GRAY); } if (colType == ColumnType.EDITABLECOMBOBOX) { comboBox.setEditable(true); final JTextComponent textComp = (JTextComponent) comboBox.getEditor().getEditorComponent(); textComp.addKeyListener(new KeyListener() { private String oldValue = ""; //$NON-NLS-1$ public void keyPressed(KeyEvent e) { oldValue = textComp.getText(); } public void keyReleased(KeyEvent e) { if (oldValue != null && !oldValue.equals(textComp.getText())) { SwingTree.this.table.setValueAt(textComp.getText(), row, column); oldValue = textComp.getText(); } else if (oldValue == null) { // AWT error where sometimes the keyReleased is fired before keyPressed. oldValue = textComp.getText(); } else { logger.debug("Special key pressed, ignoring"); //$NON-NLS-1$ } } public void keyTyped(KeyEvent e) { } }); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // if(textComp.hasFocus() == false && comboBox.hasFocus()){ SwingTree.logger.debug("Setting ComboBox value from editor: " //$NON-NLS-1$ + comboBox.getSelectedItem() + ", " + row + ", " + column); //$NON-NLS-1$ //$NON-NLS-2$ SwingTree.this.table.setValueAt(comboBox.getSelectedIndex(), row, column); // } } }); } else { comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { SwingTree.logger.debug("Setting ComboBox value from editor: " //$NON-NLS-1$ + comboBox.getSelectedItem() + ", " + row + ", " + column); //$NON-NLS-1$ //$NON-NLS-2$ SwingTree.this.table.setValueAt(comboBox.getSelectedIndex(), row, column); } }); } control = comboBox; comboBox.setEnabled(!cell.isDisabled()); comp = comboBox; break; case LABEL: JLabel lbl = new JLabel(cell.getLabel()); comp = lbl; control = lbl; break; case CUSTOM: return new CustomCellEditorWrapper(cell, customEditors.get(col.getType())); default: final JTextField label = new JTextField((String) value); label.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent arg0) { SwingTree.this.table.setValueAt(label.getText(), row, column); } public void insertUpdate(DocumentEvent arg0) { SwingTree.this.table.setValueAt(label.getText(), row, column); } public void removeUpdate(DocumentEvent arg0) { SwingTree.this.table.setValueAt(label.getText(), row, column); } }); if (isSelected) { label.setOpaque(true); // label.setBackground(Color.LIGHT_GRAY); } control = label; comp = label; label.setEnabled(!cell.isDisabled()); label.setDisabledTextColor(Color.DARK_GRAY); break; } return comp; } @Override public Object getCellEditorValue() { if (control instanceof JCheckBox) { return ((JCheckBox) control).isSelected(); } else if (control instanceof JComboBox) { JComboBox box = (JComboBox) control; if (box.isEditable()) { return ((JTextComponent) box.getEditor().getEditorComponent()).getText(); } else { return box.getSelectedIndex(); } } else if (control instanceof JTextField) { return ((JTextField) control).getText(); } else { return ((JLabel) control).getText(); } } }; }
From source file:org.richie.codeGen.ui.configui.ConstantConfigWin.java
public JScrollPane getCenterPanel() { if (centerPanel == null) { constantConfigModel = new ConstantConfigModel(); table = new JTable(constantConfigModel); table.setBackground(Color.white); table.setSelectionBackground(Color.white); table.setSelectionForeground(Color.black); JTableHeader tableHeader = table.getTableHeader(); tableHeader.setReorderingAllowed(false);// ?? table.setFont(new Font("Dialog", 0, 13)); table.setRowHeight(23);/*from ww w . j av a2 s . c om*/ TemplateConfigWin.hideColumn(table, 3); JComboBox cm = new JComboBox(GlobalData.costantType); TableColumnModel tcm = table.getColumnModel(); tcm.getColumn(4).setCellRenderer(new ButtonRenderer()); tcm.getColumn(4).setCellEditor(new ButtonEditor()); tcm.getColumn(1).setCellEditor(new DefaultCellEditor(cm)); tcm.getColumn(0).setPreferredWidth(50); tcm.getColumn(1).setPreferredWidth(30); tcm.getColumn(2).setPreferredWidth(240); tcm.getColumn(4).setPreferredWidth(20); centerPanel = new JScrollPane(table); // table addTableListener(); } return centerPanel; }
From source file:org.richie.codeGen.ui.GenAndPreviewUI.java
public JScrollPane getTemplatePanel() { complateModel = new CodeTemplateModel(false); table = new JTable(complateModel); table.setBackground(Color.white); table.setSelectionBackground(Color.white); table.setSelectionForeground(Color.black); JTableHeader tableHeader = table.getTableHeader(); tableHeader.setReorderingAllowed(false);// ?? table.setFont(new Font("Dialog", 0, 13)); table.setRowHeight(23);//from w w w . jav a 2s .c om try { templateNames = GlobalData.getTemplateNames(); rootPathNames = GlobalData.getOutFileRootNames(); } catch (Exception e) { log.error("??", e); } templateNameCom = new JComboBox(templateNames); rootPathCom = new JComboBox(rootPathNames); // ?? TemplateConfigWin.hideColumn(table, 8); TableColumnModel tcm = table.getColumnModel(); tcm.getColumn(1).setCellEditor(new DefaultCellEditor(templateNameCom)); tcm.getColumn(3).setCellEditor(new DefaultCellEditor(rootPathCom)); tcm.getColumn(7).setCellRenderer(new ButtonRenderer()); tcm.getColumn(7).setCellEditor(new ButtonEditor()); tcm.getColumn(9).setCellRenderer(new ButtonRenderer()); tcm.getColumn(9).setCellEditor(new ButtonEditor()); tcm.getColumn(0).setMaxWidth(40); tcm.getColumn(1).setPreferredWidth(50); tcm.getColumn(2).setPreferredWidth(30); tcm.getColumn(3).setPreferredWidth(40); tcm.getColumn(4).setPreferredWidth(120); tcm.getColumn(5).setMinWidth(90); tcm.getColumn(5).setMaxWidth(90); tcm.getColumn(6).setPreferredWidth(10); tcm.getColumn(7).setMaxWidth(60); tcm.getColumn(7).setMinWidth(60); tcm.getColumn(9).setMaxWidth(60); tcm.getColumn(9).setMinWidth(60); JScrollPane tablePanel = new JScrollPane(table); // table addTableListener(); return tablePanel; }
From source file:org.richie.codeGen.ui.GenAndPreviewUI.java
public void refreshComBoBox() { try {/*from ww w . j a va2s . c om*/ templateNames = GlobalData.getTemplateNames(); rootPathNames = GlobalData.getOutFileRootNames(); } catch (Exception ex) { ex.printStackTrace(); } templateNameCom = new JComboBox(templateNames); TableColumnModel tcm = table.getColumnModel(); tcm.getColumn(1).setCellEditor(new DefaultCellEditor(templateNameCom)); templateNameCom.updateUI(); rootPathCom = new JComboBox(rootPathNames); TableColumnModel tcm1 = table.getColumnModel(); tcm1.getColumn(3).setCellEditor(new DefaultCellEditor(rootPathCom)); rootPathCom.updateUI(); }
From source file:org.rimudb.editor.DescriptorEditor.java
/** * Build the panel/*from ww w .j a v a2s.c o m*/ */ private JPanel createColumnTablePanel() { JPanel columnPanel = new JPanel(); columnPanel.setLayout(new BoxLayout(columnPanel, BoxLayout.Y_AXIS)); // Create the property table panel propertyModel = new PropertyTableModel(); // Add a listener to set the changed state propertyModel.addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e) { markChanged(); if (e instanceof PropertyTableModelEvent) { PropertyTableModelEvent ptme = (PropertyTableModelEvent) e; // If the columnName column was changed then check it isn't // a PK if (ptme.getColumn() == 1) { String beforeColumnName = (String) ptme.getBeforeValue(); String afterColumnName = (String) ptme.getAfterValue(); // Is the field entry in the list of primary keys? for (int i = 0; i < pkListModel.getSize(); i++) { String pkColumnName = (String) pkListModel.get(i); // If it's found then remove it if (beforeColumnName.equals(pkColumnName)) { pkListModel.set(i, afterColumnName); break; } } } } } }); table = new ATable(getPropertyModel()); table.setName("ColumnTable"); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { int selectedRowCount = table.getSelectedRowCount(); removeColumnBtn.setEnabled(selectedRowCount > 0); moveUpBtn.setEnabled(selectedRowCount > 0); moveDownBtn.setEnabled(selectedRowCount > 0); } }); table.setTransferHandler(new TransferHandler() { public int getSourceActions(JComponent c) { return COPY; } protected Transferable createTransferable(JComponent c) { ATable columnTable = (ATable) c; int row = columnTable.getSelectedRow(); String columnName = getPropertyModel().getRow(row).getColumnName(); return new StringSelection(columnName); } }); table.setDragEnabled(true); JScrollPane sp = new JScrollPane(table); sp.setMaximumSize(new Dimension(Short.MAX_VALUE, 325)); sp.setPreferredSize(new Dimension(Short.MAX_VALUE, 325)); sp.setMinimumSize(new Dimension(Short.MAX_VALUE, 325)); JComboBox typeCB = new JComboBox(DatabaseTypes.getAllTypes()); typeCB.setEditable(false); javax.swing.table.TableColumn typeColumn = table.getColumnModel().getColumn(2); typeColumn.setCellEditor(new DefaultCellEditor(typeCB)); // Create the popup menu and set it on the table propertyPopup = new TablePopupMenu(this, table); table.addMouseListener(propertyPopup); sp.addMouseListener(propertyPopup); sp.setAlignmentX(LEFT_ALIGNMENT); columnPanel.add(sp); columnPanel.add(Box.createVerticalStrut(10)); JLabel pkLabel = new JLabel("Primary Key Columns", SwingConstants.LEFT); pkLabel.setAlignmentX(LEFT_ALIGNMENT); columnPanel.add(pkLabel); pkListModel = new DefaultListModel(); pkListModel.addListDataListener(new ListDataListener() { public void intervalRemoved(ListDataEvent e) { markChanged(); } public void intervalAdded(ListDataEvent e) { markChanged(); } public void contentsChanged(ListDataEvent e) { markChanged(); } }); pkList = new JList(pkListModel); pkList.setName("pkList"); pkList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); pkList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { int selectedRowCount = pkList.getSelectedIndex(); removePkBtn.setEnabled(selectedRowCount > -1); } }); pkList.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport info) { // we only import Strings if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); if (dl.getIndex() == -1) { return false; } return true; } public boolean importData(TransferHandler.TransferSupport info) { if (!info.isDrop()) { return false; } // Check for String flavor if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) { displayDropLocation("List doesn't accept a drop of this type."); return false; } JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); DefaultListModel listModel = (DefaultListModel) pkList.getModel(); int index = dl.getIndex(); // Get the string that is being dropped. Transferable t = info.getTransferable(); String data; try { data = (String) t.getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { return false; } // If this is a copy action then check we don't already have that String if (info.getDropAction() == COPY && listModel.indexOf(data) > -1) { displayDropLocation("The column " + data + " is already a primary key"); return false; } // Perform the actual import. if (dl.isInsert()) { int oldIndex = listModel.indexOf(data); if (oldIndex < index) { listModel.add(index, data); listModel.remove(oldIndex); } else { listModel.remove(oldIndex); listModel.add(index, data); } } else { // Don't handle replacements } return true; } public int getSourceActions(JComponent c) { return MOVE; } protected Transferable createTransferable(JComponent c) { JList list = (JList) c; Object[] values = list.getSelectedValues(); StringBuffer buff = new StringBuffer(); for (int i = 0; i < values.length; i++) { Object val = values[i]; buff.append(val == null ? "" : val.toString()); if (i != values.length - 1) { buff.append("\n"); } } return new StringSelection(buff.toString()); } }); pkList.setDropMode(DropMode.INSERT); pkList.setDragEnabled(true); JScrollPane pkScrollPanel = new JScrollPane(pkList); pkScrollPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, 100)); pkScrollPanel.setAlignmentX(LEFT_ALIGNMENT); columnPanel.add(pkScrollPanel); return columnPanel; }
From source file:org.smart.migrate.ui.MappingDialog.java
private void initFieldMappings() { if (StringUtils.isBlank((String) cbxSourceTables.getSelectedItem()) || StringUtils.isBlank((String) cbxTargetTables.getSelectedItem())) { return;/*from ww w .j av a2 s .c o m*/ } DefaultTableModel model = (DefaultTableModel) tblFieldMapping.getModel(); model.setRowCount(0); TableColumn srcColumn = tblFieldMapping.getColumnModel().getColumn(0); TableColumn tgtColumn = tblFieldMapping.getColumnModel().getColumn(1); JComboBox srcComboBox = new JComboBox(); srcComboBox.setMaximumRowCount(20); List<Field> srcFields = sourceMetaDao.getFieldsOfTable(sourceConnection, cbxSourceTables.getSelectedItem().toString()); List<Field> tgtFields = targetMetaDao.getFieldsOfTable(targetConnection, cbxTargetTables.getSelectedItem().toString()); srcComboBox.addItem(""); for (Field field : srcFields) { srcComboBox.addItem(field.getName()); } srcColumn.setCellEditor(new DefaultCellEditor(srcComboBox)); int k = 0; for (Field tgtField : tgtFields) { k++; model.addRow(new Object[] { null, tgtField.getName(), false, null, null, k }); } if (tableSetting != null && !CollectionUtils.isEmpty(tableSetting.getFieldSettings()) && tableSetting.getSourceTable().equals(cbxSourceTables.getSelectedItem()) && tableSetting.getTargetTable().equals(cbxTargetTables.getSelectedItem())) { for (int i = 0; i < tblFieldMapping.getRowCount(); i++) { String tgtField = (String) tblFieldMapping.getValueAt(i, 1); FieldSetting fieldSetting = tableSetting.getFieldSettingByTargetField(tgtField); if (fieldSetting != null) { tblFieldMapping.setValueAt(fieldSetting.getSourceField(), i, 0); tblFieldMapping.setValueAt(fieldSetting.isPrimaryKey(), i, 2); tblFieldMapping.setValueAt(fieldSetting.getDefaultValue(), i, 3); tblFieldMapping.setValueAt(fieldSetting.getDictText(), i, 4); } } } }
From source file:org.tinymediamanager.ui.components.MediaIdTable.java
public MediaIdTable(EventList<MediaId> ids, ScraperType type) { this.idMap = null; this.editable = true; this.idList = ids; setModel(new DefaultEventTableModel<>(idList, new MediaIdTableFormat())); setTableHeader(null);//from w w w. j ava 2 s. c om putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); TableColumn column = getColumnModel().getColumn(0); Set<String> providerIds = new HashSet<>(); for (MediaId id : ids) { providerIds.add(id.key); } for (MediaScraper scraper : MediaScraper.getMediaScrapers(type)) { providerIds.add(scraper.getId()); } JComboBox<String> comboBox = new JComboBox<>(providerIds.toArray(new String[0])); column.setCellEditor(new DefaultCellEditor(comboBox)); }
From source file:org.zaproxy.zap.extension.ascan.PolicyAllCategoryPanel.java
/** * This method initializes tableTest/*w w w .j a va2 s. c o m*/ * * @return javax.swing.JTable */ private JTable getTableTest() { if (tableTest == null) { tableTest = new JTable(); tableTest.setModel(getAllCategoryTableModel()); tableTest.setRowHeight(DisplayUtils.getScaledSize(18)); tableTest.setIntercellSpacing(new java.awt.Dimension(1, 1)); tableTest.setAutoCreateRowSorter(true); //Default sort by name (column 0) List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>(1); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); tableTest.getRowSorter().setSortKeys(sortKeys); for (int i = 0; i < tableTest.getColumnCount() - 1; i++) { TableColumn column = tableTest.getColumnModel().getColumn(i); column.setPreferredWidth(width[i]); } JComboBox<String> jcb1 = new JComboBox<>(); jcb1.addItem(""); // Always show a blank one for where they are not all the same for (AlertThreshold level : AlertThreshold.values()) { jcb1.addItem(Constant.messages.getString("ascan.policy.level." + level.name().toLowerCase())); } tableTest.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(jcb1)); JComboBox<String> jcb2 = new JComboBox<>(); jcb2.addItem(""); // Always show a blank one for where they are not all the same for (AttackStrength level : AttackStrength.values()) { jcb2.addItem(Constant.messages.getString("ascan.policy.level." + level.name().toLowerCase())); } tableTest.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(jcb2)); } return tableTest; }
From source file:org.zaproxy.zap.extension.customFire.PolicyAllCategoryPanel.java
/** * This method initializes tableTest//from w w w. j a v a 2 s .c o m * * @return javax.swing.JTable */ private JTable getTableTest() { if (tableTest == null) { tableTest = new JTable(); tableTest.setModel(getAllCategoryTableModel()); tableTest.setRowHeight(DisplayUtils.getScaledSize(18)); tableTest.setIntercellSpacing(new java.awt.Dimension(1, 1)); tableTest.setAutoCreateRowSorter(true); //Default sort by name (column 0) List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>(1); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); tableTest.getRowSorter().setSortKeys(sortKeys); for (int i = 0; i < tableTest.getColumnCount() - 1; i++) { TableColumn column = tableTest.getColumnModel().getColumn(i); column.setPreferredWidth(width[i]); } JComboBox<String> jcb1 = new JComboBox<>(); jcb1.addItem(""); // Always show a blank one for where they are not all the same for (AlertThreshold level : AlertThreshold.values()) { jcb1.addItem(Constant.messages .getString("customFire.custom.policy.level." + level.name().toLowerCase())); } tableTest.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(jcb1)); JComboBox<String> jcb2 = new JComboBox<>(); jcb2.addItem(""); // Always show a blank one for where they are not all the same for (AttackStrength level : AttackStrength.values()) { jcb2.addItem(Constant.messages .getString("customFire.custom.policy.level." + level.name().toLowerCase())); } tableTest.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(jcb2)); } return tableTest; }
From source file:org.zaproxy.zap.extension.httppanel.view.paramtable.HttpPanelParamTableView.java
private void initAddins() { // Get all addins addins = new LinkedList<>(); addins.add(new ParamAddinMagic()); addins.add(new ParamAddinUrlencode()); comboBoxAddIns = new JComboBox<>(); comboBoxAddIns.addItem(ADD_INS);//from w w w . j av a 2 s .c om for (ParamAddinInterface addin : addins) { comboBoxAddIns.addItem(addin); } comboBoxAddIns.addActionListener(new ComboBoxAddinsActionListener()); table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(getComboBoxTypes())); table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN); if (table.getColumnCount() != 4) { return; } table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBoxAddIns)); table.getColumnModel().getColumn(3).setCellRenderer(new ComboBoxCellRenderer(comboBoxAddIns)); }