Example usage for javax.swing JComponent addFocusListener

List of usage examples for javax.swing JComponent addFocusListener

Introduction

In this page you can find the example usage for javax.swing JComponent addFocusListener.

Prototype

public synchronized void addFocusListener(FocusListener l) 

Source Link

Document

Adds the specified focus listener to receive focus events from this component when this component gains input focus.

Usage

From source file:hr.fer.zemris.vhdllab.platform.ui.wizard.AbstractMultiValidationForm.java

@Override
protected JComponent createFormControl() {
    TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
    doBuildForm(builder);// w  w  w . j ava 2s .c  om

    JComponent control = builder.getForm();
    control.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            if (componentToGiveFocusTo != null) {
                componentToGiveFocusTo.requestFocusInWindow();
            }
        }
    });
    return control;
}

From source file:cool.pandora.modeller.ui.jpanel.base.BagInfoForm.java

private void createFormFieldsFromMap(final BagTableFormBuilder formBuilder) {
    int rowCount = 0;
    final int index = 2;

    final Set<String> keys = fieldMap.keySet();
    for (final BagInfoField field : fieldMap.values()) {
        formBuilder.row();//w  w w .ja  v  a 2  s. co  m
        rowCount++;
        final ImageIcon imageIcon = bagView.getPropertyImage("bag.delete.image");
        JButton removeButton = new JButton(imageIcon);
        final Dimension dimension = removeButton.getPreferredSize();
        dimension.width = imageIcon.getIconWidth();
        removeButton.setMaximumSize(dimension);
        removeButton.setOpaque(false);
        removeButton.setBorderPainted(false);
        removeButton.setContentAreaFilled(false);
        removeButton.addActionListener(new RemoveFieldHandler());
        logger.debug("OrganizationInfoForm add: " + field);
        if (field.getValue() != null && field.getValue().length() > 60) {
            field.setComponentType(BagInfoField.TEXTAREA_COMPONENT);
        }
        if (field.isRequired()) {
            removeButton = new JButton();
            removeButton.setOpaque(false);
            removeButton.setBorderPainted(false);
            removeButton.setContentAreaFilled(false);
        }
        switch (field.getComponentType()) {
        case BagInfoField.TEXTAREA_COMPONENT:
            final JComponent[] tlist = formBuilder.addTextArea(field.isRequired(), field.getLabel(),
                    removeButton);
            final JComponent textarea = tlist[index];
            textarea.setEnabled(field.isEnabled());
            textarea.addFocusListener(this);
            ((NoTabTextArea) textarea).setText(field.getValue());
            textarea.setBorder(new EmptyBorder(1, 1, 1, 1));
            ((NoTabTextArea) textarea).setLineWrap(true);
            if (rowCount == 1) {
                focusField = textarea;
            }
            break;
        case BagInfoField.TEXTFIELD_COMPONENT:
            final JComponent[] flist = formBuilder.add(field.isRequired(), field.getLabel(), removeButton);
            final JComponent comp = flist[index];
            comp.setEnabled(field.isEnabled());
            comp.addFocusListener(this);
            ((JTextField) comp).setText(field.getValue());
            if (rowCount == 1) {
                focusField = comp;
            }
            break;
        case BagInfoField.LIST_COMPONENT:
            final List<String> elements = field.getElements();
            final JComponent[] llist = formBuilder.addList(field.isRequired(), field.getLabel(), elements,
                    field.getValue(), removeButton);
            final JComponent lcomp = llist[index];
            lcomp.setEnabled(field.isEnabled());
            lcomp.addFocusListener(this);
            if (field.getValue() != null) {
                ((JComboBox<?>) lcomp).setSelectedItem(field.getValue().trim());
            }
            if (rowCount == 1) {
                focusField = lcomp;
            }
            break;
        default:
        }
    }
    if (focusField != null) {
        focusField.requestFocus();
    }

}

From source file:cool.pandora.modeller.ui.jpanel.base.OrganizationProfileForm.java

/**
 * createFormFields./* w  w w  . j  a v  a 2  s.c  om*/
 *
 * @return fieldForm
 */
private JComponent createFormFields() {
    final JComponent fieldForm;
    final BagTableFormBuilder formBuilder = new BagTableFormBuilder(getBindingFactory());
    formBuilder.row();
    formBuilder.addSeparator("Send from Organization");
    formBuilder.row();
    JComponent field = formBuilder.add("sourceOrganization")[1];
    final Organization organization = bagView.getBag().getProfile().getOrganization();
    if (organization != null && organization.getName().isReadOnly()) {
        field.setEnabled(false);
    }
    field.addFocusListener(this);
    formBuilder.row();
    final JComponent orgAddress = formBuilder.add("organizationAddress")[1];
    if (organization != null && organization.getAddress().isReadOnly()) {
        field.setEnabled(false);
    }

    orgAddress.addFocusListener(this);

    final Contact fromContact = bagView.getBag().getProfile().getSendFromContact();

    formBuilder.row();
    formBuilder.addSeparator("Send from Contact");
    formBuilder.row();

    this.contactName = formBuilder.add("srcContactName")[1];

    if (fromContact != null && fromContact.getContactName().isReadOnly()) {
        field.setEnabled(false);
    }

    this.contactName.addFocusListener(this);
    formBuilder.row();
    field = formBuilder.add("srcContactPhone")[1];
    if (fromContact != null && fromContact.getTelephone().isReadOnly()) {
        field.setEnabled(false);
    }
    field.addFocusListener(this);
    formBuilder.row();
    field = formBuilder.add("srcContactEmail")[1];

    if (fromContact != null && fromContact.getEmail().isReadOnly()) {
        field.setEnabled(false);
    }

    field.addFocusListener(this);
    formBuilder.row();
    formBuilder.addSeparator("Send to Contact");
    formBuilder.row();
    field = formBuilder.add("toContactName")[1];
    final Contact contact = bagView.getBag().getProfile().getSendToContact();
    if (contact != null && contact.getContactName().isReadOnly()) {
        field.setEnabled(false);
    }
    field.addFocusListener(this);
    formBuilder.row();
    field = formBuilder.add("toContactPhone")[1];

    if (contact != null && contact.getTelephone().isReadOnly()) {
        field.setEnabled(false);
    }
    field.addFocusListener(this);
    formBuilder.row();
    field = formBuilder.add("toContactEmail")[1];
    if (contact != null && contact.getEmail().isReadOnly()) {
        field.setEnabled(false);
    }
    field.addFocusListener(this);
    formBuilder.row();
    this.contactName.requestFocus();
    fieldForm = formBuilder.getForm();
    return fieldForm;
}

From source file:com.t3.macro.api.functions.input.ColumnPanel.java

/** Adjusts the runtime behavior of controls. Called when displayed. */
public void runtimeFixup() {
    // When first displayed, the focus will go to the first field.
    lastFocus = findFirstFocusable();//from  www. jav a 2  s .c o  m

    // When a field gains the focus, save it in lastFocus.
    FocusListener listener = new FocusListener() {
        @Override
        public void focusGained(FocusEvent fe) {
            JComponent src = (JComponent) fe.getSource();
            lastFocus = src;
            if (src instanceof JTextField)
                ((JTextField) src).selectAll();
            //               // debugging
            //               String s = (src instanceof JTextField) ?
            //                  " (" + ((JTextField)src).getText() + ")" : "";
            //               System.out.println("  Got focus " + src.getClass().getName() + s);
        }

        @Override
        public void focusLost(FocusEvent arg0) {
        }
    };

    for (JComponent c : inputFields) {
        // Each control saves itself to lastFocus when it gains focus.
        c.addFocusListener(listener);

        // Implement control-specific adjustments
        if (c instanceof ColumnPanel) {
            ColumnPanel cp = (ColumnPanel) c;
            cp.runtimeFixup();
        }
    }
    if (lastFocus != null)
        scrollRectToVisible(lastFocus.getBounds());
}

From source file:org.esa.snap.ui.tooladapter.dialogs.ToolParameterEditorDialog.java

public JPanel createMainPanel() {
    GridBagLayout layout = new GridBagLayout();
    layout.columnWidths = new int[] { 100, 390 };

    mainPanel = new JPanel(layout);

    addTextPropertyEditor(mainPanel, "Name: ", "name", parameter.getName(), 0, true);
    addTextPropertyEditor(mainPanel, "Alias: ", "alias", parameter.getAlias(), 1, true);

    //dataType//from   w ww.  j a va 2  s.  co m
    mainPanel.add(new JLabel("Type"), getConstraints(2, 0, 1));
    JComboBox comboEditor = new JComboBox(typesMap.keySet().toArray());
    comboEditor.setSelectedItem(typesMap.getKey(parameter.getDataType()));
    comboEditor.addActionListener(ev -> {
        JComboBox cb = (JComboBox) ev.getSource();
        String typeName = (String) cb.getSelectedItem();
        if (!parameter.getDataType().equals(typesMap.get(typeName))) {
            parameter.setDataType((Class<?>) typesMap.get(typeName));
            //reset value set
            parameter.setValueSet(null);
            paramContext.getPropertySet().getProperty(parameter.getName()).getDescriptor().setValueSet(null);
            try {
                valuesContext.getPropertySet().getProperty("valueSet").setValue(null);
            } catch (ValidationException e) {
                logger.warning(e.getMessage());
            }
            //editor must updated
            try {
                if (editorComponent != null) {
                    mainPanel.remove(editorComponent);
                }
                editorComponent = uiWrapper.reloadUIComponent((Class<?>) typesMap.get(typeName));
                if (!("File".equals(typeName) || "List".equals(typeName))) {
                    editorComponent.setInputVerifier(new TypedValueValidator(
                            "The value entered is not of the specified data type", parameter.getDataType()));
                }
                mainPanel.add(editorComponent, getConstraints(3, 1, 1));
                mainPanel.revalidate();
            } catch (Exception e) {
                logger.warning(e.getMessage());
                Dialogs.showError(e.getMessage());
            }
        }
    });
    mainPanel.add(comboEditor, getConstraints(2, 1, 1));

    //defaultValue
    mainPanel.add(new JLabel("Default value"), getConstraints(3, 0, 1));
    try {
        editorComponent = uiWrapper.getUIComponent();
        mainPanel.add(editorComponent, getConstraints(3, 1, 1));
    } catch (Exception e) {
        e.printStackTrace();
    }

    addTextPropertyEditor(mainPanel, "Description: ", "description", parameter.getDescription(), 4, false);
    addTextPropertyEditor(mainPanel, "Label: ", "label", parameter.getLabel(), 5, false);
    addTextPropertyEditor(mainPanel, "Unit: ", "unit", parameter.getUnit(), 6, false);
    addTextPropertyEditor(mainPanel, "Interval: ", "interval", parameter.getInterval(), 7, false);
    JComponent valueSetEditor = addTextPropertyEditor(mainPanel, "Value set: ", "valueSet",
            StringUtils.join(parameter.getValueSet(), ArrayConverter.SEPARATOR), 8, false);
    valueSetEditor.addFocusListener(new FocusListener() {
        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent ev) {
            //the value set may impact the editor
            try {
                String newValueSet = ((JTextField) valueSetEditor).getText();
                if (newValueSet.isEmpty()) {
                    parameter.setValueSet(null);
                    valuesContext.getPropertySet().getProperty("valueSet").setValue(null);
                } else {
                    parameter.setValueSet(newValueSet.split(ArrayConverter.SEPARATOR));
                    valuesContext.getPropertySet().getProperty("valueSet")
                            .setValue(newValueSet.split(ArrayConverter.SEPARATOR));
                }
                if (editorComponent != null) {
                    mainPanel.remove(editorComponent);
                }
                createContextForValueEditor();
                if (!(File.class.equals(parameter.getDataType()) || parameter.getDataType().isArray())) {
                    editorComponent.setInputVerifier(new TypedValueValidator(
                            "The value entered is not of the specified data type", parameter.getDataType()));
                }
                mainPanel.add(editorComponent, getConstraints(3, 1, 1));
                mainPanel.revalidate();
            } catch (Exception e) {
                logger.warning(e.getMessage());
                Dialogs.showError(e.getMessage());
            }
        }
    });
    addTextPropertyEditor(mainPanel, "Condition: ", "condition", parameter.getCondition(), 9, false);
    addTextPropertyEditor(mainPanel, "Pattern: ", "pattern", parameter.getPattern(), 10, false);
    addTextPropertyEditor(mainPanel, "Format: ", "format", parameter.getFormat(), 11, false);
    addBoolPropertyEditor(mainPanel, "Not null", "notNull", parameter.isNotNull(), 12);
    addBoolPropertyEditor(mainPanel, "Not empty", "notEmpty", parameter.isNotEmpty(), 13);
    addTextPropertyEditor(mainPanel, "ItemAlias: ", "itemAlias", parameter.getItemAlias(), 14, false);
    addBoolPropertyEditor(mainPanel, "Deprecated", "deprecated", parameter.isDeprecated(), 15);

    return mainPanel;
}

From source file:se.cambio.cds.gdl.editor.view.panels.DescriptionPanel.java

private void connect(JXPathContext context, String xPath, JComponent component) {
    component.addFocusListener(new ComponentFocusAdapter(context, xPath));
    String value = null;//from   w  ww . j a  v a  2  s  . c  o  m
    try {
        value = (String) context.getValue(xPath);
    } catch (JXPathNotFoundException e) {
        //Value not found
    }
    if (value != null) {
        if (component instanceof JTextComponent) {
            value = value.replace("\\\"", "\"");
            ((JTextComponent) component).setText(value);
        } else if (component instanceof JComboBox) {
            ((JComboBox) component).setSelectedItem(value);
        }
    }
}