Search in sources :

Example 26 with DefaultCellEditor

use of javax.swing.DefaultCellEditor in project cayenne by apache.

the class CayenneTable method getSelectedTextComponent.

public JTextComponent getSelectedTextComponent() {
    int row = getSelectedRow();
    int column = getSelectedColumn();
    if (row < 0 || column < 0) {
        return null;
    }
    TableCellEditor editor = this.getCellEditor(row, column);
    if (editor instanceof DefaultCellEditor) {
        Component comp = ((DefaultCellEditor) editor).getComponent();
        if (comp instanceof JTextComponent) {
            return (JTextComponent) comp;
        }
    }
    return null;
}
Also used : JTextComponent(javax.swing.text.JTextComponent) TableCellEditor(javax.swing.table.TableCellEditor) JTextComponent(javax.swing.text.JTextComponent) Component(java.awt.Component) DefaultCellEditor(javax.swing.DefaultCellEditor)

Example 27 with DefaultCellEditor

use of javax.swing.DefaultCellEditor in project cayenne by apache.

the class CayenneTable method createDefaultEditors.

@Override
protected void createDefaultEditors() {
    super.createDefaultEditors();
    JTextField textField = new JTextField(20);
    final DefaultCellEditor textEditor = Application.getWidgetFactory().createCellEditor(textField);
    textEditor.setClickCountToStart(1);
    setDefaultEditor(Object.class, textEditor);
    setDefaultEditor(String.class, textEditor);
}
Also used : JTextField(javax.swing.JTextField) DefaultCellEditor(javax.swing.DefaultCellEditor)

Example 28 with DefaultCellEditor

use of javax.swing.DefaultCellEditor in project vcell by virtualcell.

the class DefaultScrollTableActionManager method constructPopupMenu.

protected void constructPopupMenu() {
    TableModel tableModel = ownerTable.getModel();
    int numColumns = tableModel.getColumnCount();
    boolean[] bEditable = new boolean[numColumns];
    boolean bTableEditable = false;
    for (int c = 0; c < numColumns; c++) {
        for (int r = 0; r < selectedRows.length; r++) {
            if (tableModel.isCellEditable(selectedRows[r], c)) {
                bEditable[c] = true;
                bTableEditable = true;
                break;
            }
        }
    }
    if (!bTableEditable) {
        return;
    }
    if (popupMenu == null) {
        popupMenu = new JPopupMenu();
        columnMenus = new JMenu[numColumns];
        popupLabel = new javax.swing.JLabel();
        popupLabel.setText(" Specify Column Value for Selected Row(s)");
    }
    popupMenu.removeAll();
    popupMenu.add(popupLabel);
    int[] uniqueColumns = getUniqueColumns();
    for (int c = 0; c < numColumns; c++) {
        String columnName = tableModel.getColumnName(c);
        if (disabledColumnPopups.contains(c)) {
            continue;
        }
        if (columnName.equalsIgnoreCase("name")) {
            continue;
        }
        boolean bUnique = false;
        if (uniqueColumns != null) {
            for (int uc : uniqueColumns) {
                if (uc == c) {
                    bUnique = true;
                    break;
                }
            }
        }
        if (bUnique) {
            continue;
        }
        if (bEditable[c]) {
            if (columnMenus[c] == null) {
                Class<?> columnClass = tableModel.getColumnClass(c);
                Component editorComponent = null;
                TableCellEditor cellEditor = ownerTable.getColumnModel().getColumn(c).getCellEditor();
                if (cellEditor == null) {
                    cellEditor = ownerTable.getDefaultEditor(columnClass);
                }
                if (cellEditor instanceof DefaultCellEditor) {
                    editorComponent = ((DefaultCellEditor) cellEditor).getComponent();
                }
                if (editorComponent == null || !(editorComponent instanceof JCheckBox) && !(editorComponent instanceof JTextField)) {
                    continue;
                }
                ScrollTableCellEditorType editorType = editorComponent instanceof JCheckBox ? ScrollTableCellEditorType.jcheckbox : ScrollTableCellEditorType.jtextfield;
                columnMenus[c] = new JMenu(columnName);
                switch(editorType) {
                    case jcheckbox:
                        JMenuItem menuItemCheckSelected = new JMenuItem("Checked");
                        menuItemCheckSelected.addActionListener(this);
                        columnActionComponentList.add(new ColumnActionComponent(c, menuItemCheckSelected, true));
                        JMenuItem menuItemUncheckSelected = new JMenuItem("Unchecked");
                        menuItemUncheckSelected.addActionListener(this);
                        columnActionComponentList.add(new ColumnActionComponent(c, menuItemUncheckSelected, false));
                        columnMenus[c].add(menuItemCheckSelected);
                        columnMenus[c].add(menuItemUncheckSelected);
                        break;
                    case jtextfield:
                        JTextField textField = new JTextField(5);
                        textField.addActionListener(this);
                        textField.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(2, 4, 2, 4), textField.getBorder()));
                        columnActionComponentList.add(new ColumnActionComponent(c, textField));
                        JLabel label = new JLabel(" (Press Enter to commit) ");
                        label.setFont(label.getFont().deriveFont(label.getFont().getSize2D() - 1));
                        columnMenus[c].add(textField);
                        columnMenus[c].add(label);
                        break;
                }
            }
            columnMenus[c].setEnabled(true);
        } else {
            if (columnMenus[c] != null) {
                columnMenus[c].setEnabled(false);
            }
        }
        if (columnMenus[c] != null) {
            popupMenu.add(columnMenus[c]);
        }
    }
}
Also used : JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) JPopupMenu(javax.swing.JPopupMenu) DefaultCellEditor(javax.swing.DefaultCellEditor) JCheckBox(javax.swing.JCheckBox) JLabel(javax.swing.JLabel) TableCellEditor(javax.swing.table.TableCellEditor) JComponent(javax.swing.JComponent) Component(java.awt.Component) JMenuItem(javax.swing.JMenuItem) EmptyBorder(javax.swing.border.EmptyBorder) TableModel(javax.swing.table.TableModel) JMenu(javax.swing.JMenu)

Example 29 with DefaultCellEditor

use of javax.swing.DefaultCellEditor in project CCDD by nasa.

the class CcddTableEditorHandler method setUpMsgNamesAndIDsColumn.

/**
 ********************************************************************************************
 * Set up or update the combo box containing the available message ID names and corresponding
 * message IDs for display in table column cells
 *
 * @param msgIDs
 *            list of message ID names and the associated ID values
 ********************************************************************************************
 */
protected void setUpMsgNamesAndIDsColumn(List<String[]> msgIDs) {
    // Check if a cell is currently being edited
    if (table.getCellEditor() != null) {
        // Incorporate any cell changes and terminate editing
        table.getCellEditor().stopCellEditing();
    }
    // Check if the table has a message name & ID column
    if (!msgIDNameIndex.isEmpty()) {
        // Check if no message name & ID list is provided
        if (msgIDs == null) {
            // Create a message ID handler and get the list of message ID names and associated
            // ID values
            CcddMessageIDHandler msgIDHandler = new CcddMessageIDHandler(ccddMain, false);
            msgIDs = msgIDHandler.getMessageIDsAndNames(MessageIDSortOrder.BY_NAME, true, editorDialog);
        }
        // Create a combo box for displaying message ID names & IDs
        PaddedComboBox comboBox = new PaddedComboBox(table.getFont());
        // Step through each message ID name & ID pair
        for (String[] msgID : msgIDs) {
            // Check if the message ID name isn't blank
            if (!msgID[MsgIDListColumnIndex.MESSAGE_ID_NAME.ordinal()].isEmpty()) {
                // Get the message name & ID to display in the list
                String item = msgID[MsgIDListColumnIndex.MESSAGE_ID_NAME.ordinal()] + " (" + msgID[MsgIDListColumnIndex.MESSAGE_ID.ordinal()] + ")";
                // Check if the message name & ID isn't already in the list
                if (((DefaultComboBoxModel<String>) comboBox.getModel()).getIndexOf(item) == -1) {
                    // Add the message ID name & ID to the combo box list
                    comboBox.addItem(item);
                }
            }
        }
        // Step through each message ID names column
        for (Integer column : msgIDNameIndex) {
            // Get the column reference for the message ID names & IDs column
            TableColumn msgNameIDColumn = table.getColumnModel().getColumn(table.convertColumnIndexToView(column));
            // Set the table column editor to the combo box
            msgNameIDColumn.setCellEditor(new DefaultCellEditor(comboBox));
        }
    }
}
Also used : PaddedComboBox(CCDD.CcddClassesComponent.PaddedComboBox) TableColumn(javax.swing.table.TableColumn) DefaultCellEditor(javax.swing.DefaultCellEditor)

Example 30 with DefaultCellEditor

use of javax.swing.DefaultCellEditor in project CCDD by nasa.

the class CcddJTableHandler method setCellEditors.

/**
 ********************************************************************************************
 * Set the editors for the editable cells based on the column type
 ********************************************************************************************
 */
private void setCellEditors() {
    // Create a focus listener to track the text cursor position when the cell loses focus
    FocusListener focusListener = new FocusListener() {

        /**
         ************************************************************************************
         * Handle loss of keyboard focus for the cell
         ************************************************************************************
         */
        @Override
        public void focusLost(FocusEvent fe) {
            // Check if editing is active in the cell
            if (table.isEditing()) {
                // Store the start and end positions of the selected text
                lastSelectionStart = ((JTextComponent) fe.getComponent()).getSelectionStart();
                lastSelectionEnd = ((JTextComponent) fe.getComponent()).getSelectionEnd();
            } else // Editing is inactive
            {
                // Reset the text selection positions
                lastSelectionStart = -1;
                lastSelectionEnd = -1;
            }
        }

        /**
         ************************************************************************************
         * Handle gain of keyboard focus for the cell
         ************************************************************************************
         */
        @Override
        public void focusGained(FocusEvent fe) {
        }
    };
    // Create a text area so that its properties can be set and then used to create a
    // multi-line editor for cells containing one or more lines of text
    JTextArea textFieldMulti = new JTextArea();
    textFieldMulti.setFont(cellFont);
    textFieldMulti.setBorder(editBorder);
    // Add a listener for cell focus changes
    textFieldMulti.addFocusListener(focusListener);
    // Create the cell editor for multi-line cells
    MultiLineCellEditor dceMulti = new MultiLineCellEditor(textFieldMulti);
    // Create a a cell editor for single line cells. The padding differs from the multi-line
    // cell; using this editor for single line cells prevents the text from changing vertical
    // alignment when editing is initiated
    JTextField textFieldSingle = new JTextField();
    textFieldSingle.setFont(cellFont);
    textFieldSingle.setBorder(editBorder);
    // Add a listener for cell focus changes
    textFieldSingle.addFocusListener(focusListener);
    // Create the cell editor
    DefaultCellEditor dceSingle = new DefaultCellEditor(textFieldSingle);
    // Step through each column in the table
    for (int column = 0; column < getColumnCount(); column++) {
        // Get the column index in model coordinates
        int columnModel = convertColumnIndexToModel(column);
        // Check if the column's contents is not displayed as a check box
        if (!isColumnBoolean(columnModel)) {
            // Set the editor so that the contents can be modified within the table cell. Use
            // the editor appropriate for the number of cell display lines
            getColumnModel().getColumn(column).setCellEditor(isColumnMultiLine(columnModel) ? dceMulti : dceSingle);
        }
    }
}
Also used : JTextArea(javax.swing.JTextArea) JTextField(javax.swing.JTextField) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent) Point(java.awt.Point) TableInsertionPoint(CCDD.CcddConstants.TableInsertionPoint) DefaultCellEditor(javax.swing.DefaultCellEditor)

Aggregations

DefaultCellEditor (javax.swing.DefaultCellEditor)183 JComboBox (javax.swing.JComboBox)111 TableColumn (javax.swing.table.TableColumn)66 JTable (javax.swing.JTable)59 ArrayList (java.util.ArrayList)47 TableCellEditor (javax.swing.table.TableCellEditor)35 JScrollPane (javax.swing.JScrollPane)34 JPanel (javax.swing.JPanel)29 ActionEvent (java.awt.event.ActionEvent)27 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)27 JTextField (javax.swing.JTextField)24 Component (java.awt.Component)23 JLabel (javax.swing.JLabel)23 ProxyColumn (us.mn.state.dot.tms.client.proxy.ProxyColumn)23 DefaultTableModel (javax.swing.table.DefaultTableModel)22 JCheckBox (javax.swing.JCheckBox)18 Dimension (java.awt.Dimension)17 ActionListener (java.awt.event.ActionListener)17 JButton (javax.swing.JButton)17 TableCellRenderer (javax.swing.table.TableCellRenderer)14