Java Code Examples for javax.swing.JComboBox#isEditable()

The following examples show how to use javax.swing.JComboBox#isEditable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RComboBox.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
@Override
public void focusLost(RComponent next) {
    JComboBox comboBox = (JComboBox) component;
    Object selectedItem = comboBox.getSelectedItem();
    if (selectedItem != null && selectedItem.equals(prevSelectedItem)) {
        return;
    }
    if (!comboBox.isEditable()) {
        recorder.recordSelect(this, getText(comboBox, true));
    } else {
        String editorText = ((JTextField) comboBox.getEditor().getEditorComponent()).getText();
        String selectedItemText = getText(comboBox, false);
        if (editorText.equals(selectedItemText)) {
            recorder.recordSelect(this, getText(comboBox, true));
        } else {
            recorder.recordSelect(this, editorText);
        }
    }
}
 
Example 2
Source File: MiscSettingsDialog.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
public void storeSetting() {
	if ( component instanceof JSpinner )
		Settings.set( settingKey, ( (JSpinner) component ).getValue() );
	else if ( component instanceof JSlider )
		Settings.set( settingKey, ( (JSlider) component ).getValue() );
	else if ( component instanceof JTextField )
		Settings.set( settingKey, ( (JTextField) component ).getText() );
	else if ( component instanceof JCheckBox )
		Settings.set( settingKey, ( (JCheckBox) component ).isSelected() );
	else if ( component instanceof JComboBox ) {
		Settings.set( settingKey, ( (JComboBox< ? >) component ).getSelectedIndex() );
		final JComboBox< ? > comboBox = (JComboBox< ? >) component;
		if ( comboBox.isEditable() ) // It's a pre-defined list combo box
			Settings.set( settingKey, comboBox.getSelectedItem() );				
		else                         // Normal combo box
			Settings.set( settingKey, comboBox.getSelectedIndex() );				
	}
}
 
Example 3
Source File: ReturnTypeUIHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void setSelectedItem(final JComboBox combo, final Object item) {
    combo.setSelectedItem(item);
    if (combo.isEditable() && combo.getEditor() != null) {
        // item must be set in the editor in case of editable combobox
        combo.configureEditor(combo.getEditor(), combo.getSelectedItem()); 
    }
}
 
Example 4
Source File: DatasourceUIHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void setSelectedItem(final JComboBox combo, final Object item) {
    combo.setSelectedItem(item);
    if (combo.isEditable() && combo.getEditor() != null) {
        // item must be set in the editor in case of editable combobox
        combo.configureEditor(combo.getEditor(), combo.getSelectedItem()); 
    }
}
 
Example 5
Source File: DemoUtils.java    From java-ocr-api with GNU Affero General Public License v3.0 5 votes vote down vote up
public static JFileChooser registerBrowseButtonListener(final JComboBox comboBox, final JButton button, final boolean chooseFile, final boolean isOpen, final FileFilter fileFilter, final File initialDirectory) {
    if(! comboBox.isEditable()) {
        throw new IllegalArgumentException("The combo box must be editable.");
    }

    final JFileChooser fileChooser = new JFileChooser();

    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            fileChooser.setCurrentDirectory(comboBox.getSelectedItem() == null || comboBox.getSelectedItem().toString().trim().length() == 0 ?
                    initialDirectory : new File(comboBox.getSelectedItem().toString()));

            fileChooser.setFileSelectionMode(chooseFile ? JFileChooser.FILES_ONLY : JFileChooser.DIRECTORIES_ONLY);

            if(fileFilter != null) {
                fileChooser.addChoosableFileFilter(fileFilter);
            }

            int ret = isOpen ? fileChooser.showOpenDialog(comboBox) :
                    fileChooser.showSaveDialog(comboBox);

            if(ret == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                comboBox.getEditor().setItem(file.getAbsolutePath());
            }
        }
    };

    button.addActionListener(listener);

    return fileChooser;
}
 
Example 6
Source File: SeaGlassComboBoxUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
private int getComponentState(JComponent c) {
    // currently we have a broken situation where if a developer
    // takes the border from a JComboBox and sets it on a JTextField
    // then the codepath will eventually lead back to this method
    // but pass in a JTextField instead of JComboBox! In case this
    // happens, we just return the normal synth state for the component
    // instead of doing anything special
    if (!(c instanceof JComboBox)) return SeaGlassLookAndFeel.getComponentState(c);

    JComboBox box = (JComboBox) c;
    if (shouldActLikeButton()) {
        int state = ENABLED;
        if ((!c.isEnabled())) {
            state = DISABLED;
        }
        if (buttonHandler.isPressed()) {
            state |= PRESSED;
        }
        if (buttonHandler.isRollover()) {
            state |= MOUSE_OVER;
        }
        if (box.isFocusOwner()) {
            state |= FOCUSED;
        }
        return state;
    } else {
        // for editable combos the editor component has the focus not the
        // combo box its self, so we should make the combo paint focused
        // when its editor has focus
        int basicState = SeaGlassLookAndFeel.getComponentState(c);
        if (box.isEditable() && box.getEditor().getEditorComponent().isFocusOwner()) {
            basicState |= FOCUSED;
        }
        return basicState;
    }
}
 
Example 7
Source File: DefaultGridCellEditor.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
public Component getEditorComponent(int row, int column, Object value,
			boolean isSelected, JGrid grid, boolean recreate) {
		if (editorComponent instanceof JComboBox) {
			final JComboBox comboBox = new JComboBox();
			editorComponent = comboBox;
			comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
		        delegate = new EditorDelegate() {
		        	public void setValue(Object value) {
		        		comboBox.setSelectedItem(value);
		            }

		        	public Object getCellEditorValue() {
		        		return comboBox.getSelectedItem();
		        	}
		                
		            public boolean shouldSelectCell(EventObject anEvent) { 
		                if (anEvent instanceof MouseEvent) { 
		                    MouseEvent e = (MouseEvent)anEvent;
		                    return e.getID() != MouseEvent.MOUSE_DRAGGED;
		                }
		                return true;
		            }
		            
		            public boolean stopCellEditing() {
		            	if (comboBox.isEditable()) {
		            		// 	Commit edited value.
		            		comboBox.actionPerformed(new ActionEvent(DefaultGridCellEditor.this, 0, ""));
		            	}
		            	return super.stopCellEditing();
		            }
		        };
			comboBox.addActionListener(delegate);
		}
        this.grid = grid;
//		editorComponent.setBorder(new LineBorder(Color.black));
		delegate.setValue(value);
		
		return editorComponent;
	}
 
Example 8
Source File: UiUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static String getValue(JComboBox<String> combo) {
    if (combo.isEditable()) {
        return getValue((String) combo.getEditor().getItem());
    }
    return getValue((String) combo.getSelectedItem());
}
 
Example 9
Source File: ComboBoxCellEditor.java    From screenstudio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a new ComboBoxCellEditor.
 * 
 * @param comboBox the comboBox that should be used as the cell editor.
 */
public ComboBoxCellEditor(final JComboBox comboBox) {
    super(comboBox);

    comboBox.removeActionListener(this.delegate);

    this.delegate = new EditorDelegate() {
        @Override
        public void setValue(final Object value) {
            comboBox.setSelectedItem(value);
        }

        @Override
        public Object getCellEditorValue() {
            return comboBox.getSelectedItem();
        }

        @Override
        public boolean shouldSelectCell(final EventObject anEvent) {
            if (anEvent instanceof MouseEvent) {
                final MouseEvent e = (MouseEvent) anEvent;
                return e.getID() != MouseEvent.MOUSE_DRAGGED;
            }
            return true;
        }

        @Override
        public boolean stopCellEditing() {
            if (comboBox.isEditable()) {
                // Commit edited value.
                comboBox.actionPerformed(new ActionEvent(ComboBoxCellEditor.this, 0, ""));
            }
            return super.stopCellEditing();
        }

        @Override
        public void actionPerformed(final ActionEvent e) {
            ComboBoxCellEditor.this.stopCellEditing();
        }
    };
    comboBox.addActionListener(this.delegate);
}
 
Example 10
Source File: AutoCompleteDecorator.java    From cropplanning with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Enables automatic completion for the given JComboBox. The automatic
 * completion will be strict (only items from the combo box can be selected)
 * if the combo box is not editable.
 * @param comboBox a combo box
 * @param stringConverter the converter used to transform items to strings
 */
public static void decorate(final JComboBox comboBox, final ObjectToStringConverter stringConverter) {
    boolean strictMatching = !comboBox.isEditable();
    // has to be editable
    comboBox.setEditable(true);
    // fix the popup location
    AquaLnFPopupLocationFix.install(comboBox);

    // configure the text component=editor component
    JTextComponent editorComponent = (JTextComponent) comboBox.getEditor().getEditorComponent();
    final AbstractAutoCompleteAdaptor adaptor = new ComboBoxAdaptor(comboBox);
    final AutoCompleteDocument document = new AutoCompleteDocument(adaptor, strictMatching, stringConverter);
    decorate(editorComponent, document, adaptor);
    
    // show the popup list when the user presses a key
    final KeyListener keyListener = new KeyAdapter() {
        public void keyPressed(KeyEvent keyEvent) {
            // don't popup on action keys (cursor movements, etc...)
            if (keyEvent.isActionKey()) return;
            // don't popup if the combobox isn't visible anyway
            if (comboBox.isDisplayable() && !comboBox.isPopupVisible()) {
                int keyCode = keyEvent.getKeyCode();
                // don't popup when the user hits shift,ctrl or alt
                if (keyCode==keyEvent.VK_SHIFT || keyCode==keyEvent.VK_CONTROL || keyCode==keyEvent.VK_ALT) return;
                // don't popup when the user hits escape (see issue #311)
                if (keyCode==keyEvent.VK_ESCAPE) return;
                comboBox.setPopupVisible(true);
            }
        }
    };
    editorComponent.addKeyListener(keyListener);
    
    if (stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) {
        comboBox.setEditor(new AutoCompleteComboBoxEditor(comboBox.getEditor(), stringConverter));
    }
    
    // Changing the l&f can change the combobox' editor which in turn
    // would not be autocompletion-enabled. The new editor needs to be set-up.
    comboBox.addPropertyChangeListener("editor", new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
          	ComboBoxEditor editor = (ComboBoxEditor) e.getNewValue();
          	if (editor!=null && editor.getEditorComponent()!=null) {
                if (!(editor instanceof AutoCompleteComboBoxEditor) 
                    && stringConverter!=ObjectToStringConverter.DEFAULT_IMPLEMENTATION) {
                    comboBox.setEditor(new AutoCompleteComboBoxEditor(editor, stringConverter));
                    // Don't do the decorate step here because calling setEditor will trigger
                    // the propertychange listener a second time, which will do the decorate
                    // and addKeyListener step.
                } else {
                    decorate((JTextComponent) editor.getEditorComponent(), document, adaptor);
                    editor.getEditorComponent().addKeyListener(keyListener);
                }
          	}
        }
    });
}