Example usage for javax.swing JTextField setDocument

List of usage examples for javax.swing JTextField setDocument

Introduction

In this page you can find the example usage for javax.swing JTextField setDocument.

Prototype

@BeanProperty(expert = true, description = "the text document model")
public void setDocument(Document doc) 

Source Link

Document

Associates the editor with a text document.

Usage

From source file:JTextFieldLimit.java

public static void main(String[] argv) {

    JTextField textfield1 = new JTextField(15);
    textfield1.setDocument(new JTextFieldLimit(10));
}

From source file:JTextFieldFilter.java

public Main() {
    JTextField tf1b;
    JLabel l1b;//from w  ww.  j av a  2  s. c om

    setLayout(new FlowLayout());

    l1b = new JLabel("only float");
    tf1b = new JTextField(10);
    getContentPane().add(l1b);
    getContentPane().add(tf1b);
    tf1b.setDocument(new JTextFieldFilter(JTextFieldFilter.FLOAT));
    setSize(300, 300);
    setVisible(true);
}

From source file:JTextFieldFilter.java

public Main() {
    JTextField tf1, tf1b, tf1c, tf2, tf3;
    JLabel l1, l1b, l1c, l2, l3;/* w  ww .ja v  a2  s  . com*/

    setLayout(new FlowLayout());
    //
    l1 = new JLabel("only numerics");
    tf1 = new JTextField(10);
    getContentPane().add(l1);
    getContentPane().add(tf1);
    tf1.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
    setSize(300, 300);
    setVisible(true);
}

From source file:JTextFieldFilter.java

public Main() {
    JTextField tf1c;
    JLabel l1c;/*from w  w w . j ava  2 s .c  o m*/

    setLayout(new FlowLayout());
    l1c = new JLabel("only float(can be negative)");
    tf1c = new JTextField(10);
    getContentPane().add(l1c);
    getContentPane().add(tf1c);
    JTextFieldFilter jtff = new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC);
    jtff.setNegativeAccepted(true);
    tf1c.setDocument(jtff);

    setSize(300, 300);
    setVisible(true);
}

From source file:ca.phon.app.project.ProjectWindow.java

private MultiActionButton createCorpusButton() {
    MultiActionButton retVal = new MultiActionButton();

    ImageIcon newIcn = IconManager.getInstance().getIcon("places/folder", IconSize.SMALL);

    String s1 = "Corpus";
    String s2 = "Enter corpus name and press enter.  Press escape to cancel.";

    retVal.getTopLabel().setText(WorkspaceTextStyler.toHeaderText(s1));
    retVal.getTopLabel().setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
    retVal.getTopLabel().setFont(FontPreferences.getTitleFont());
    retVal.getTopLabel().setIcon(newIcn);
    retVal.setAlwaysDisplayActions(true);

    retVal.setOpaque(false);//from  www . j a  va2  s  .co m

    ImageIcon cancelIcn = IconManager.getInstance().getIcon("actions/button_cancel", IconSize.SMALL);
    ImageIcon cancelIcnL = cancelIcn;

    PhonUIAction btnSwapAct = new PhonUIAction(this, "onSwapNewAndCreateCorpus", retVal);
    btnSwapAct.putValue(Action.ACTION_COMMAND_KEY, "CANCEL_CREATE_ITEM");
    btnSwapAct.putValue(Action.NAME, "Cancel create");
    btnSwapAct.putValue(Action.SHORT_DESCRIPTION, "Cancel create");
    btnSwapAct.putValue(Action.SMALL_ICON, cancelIcn);
    btnSwapAct.putValue(Action.LARGE_ICON_KEY, cancelIcnL);
    retVal.addAction(btnSwapAct);

    JPanel corpusNamePanel = new JPanel(new BorderLayout());
    corpusNamePanel.setOpaque(false);

    final JTextField corpusNameField = new JTextField();
    corpusNameField.setDocument(new NameDocument());
    corpusNameField.setText("Corpus Name");
    corpusNamePanel.add(corpusNameField, BorderLayout.CENTER);

    ActionMap actionMap = retVal.getActionMap();
    actionMap.put(btnSwapAct.getValue(Action.ACTION_COMMAND_KEY), btnSwapAct);
    InputMap inputMap = retVal.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);

    inputMap.put(ks, btnSwapAct.getValue(Action.ACTION_COMMAND_KEY));

    retVal.setActionMap(actionMap);
    retVal.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);

    PhonUIAction createNewCorpusAct = new PhonUIAction(this, "onCreateCorpus", corpusNameField);
    createNewCorpusAct.putValue(Action.SHORT_DESCRIPTION, "Create new corpus folder");
    createNewCorpusAct.putValue(Action.SMALL_ICON,
            IconManager.getInstance().getIcon("actions/list-add", IconSize.SMALL));

    JButton createBtn = new JButton(createNewCorpusAct);
    corpusNamePanel.add(createBtn, BorderLayout.EAST);

    corpusNameField.setAction(createNewCorpusAct);

    // swap bottom component in new project button
    retVal.setBottomLabelText(WorkspaceTextStyler.toDescText(s2));
    retVal.add(corpusNamePanel, BorderLayout.CENTER);

    retVal.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
        }

        @Override
        public void focusGained(FocusEvent e) {
            corpusNameField.requestFocus();
        }
    });

    return retVal;
}

From source file:ca.phon.app.project.ProjectWindow.java

private MultiActionButton createSessionButton() {
    MultiActionButton retVal = new MultiActionButton();

    ImageIcon newIcn = IconManager.getInstance().getIcon("mimetypes/text-xml", IconSize.SMALL);

    String s1 = "Session";
    String s2 = "Enter session name and press enter.  Press escape to cancel.";

    retVal.getTopLabel().setText(WorkspaceTextStyler.toHeaderText(s1));
    retVal.getTopLabel().setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
    retVal.getTopLabel().setFont(FontPreferences.getTitleFont());
    retVal.getTopLabel().setIcon(newIcn);
    retVal.setAlwaysDisplayActions(true);

    retVal.setOpaque(false);/*from w  w  w  .j a  v  a2 s.c o  m*/

    ImageIcon cancelIcn = IconManager.getInstance().getIcon("actions/button_cancel", IconSize.SMALL);
    ImageIcon cancelIcnL = cancelIcn;

    PhonUIAction btnSwapAct = new PhonUIAction(this, "onSwapNewAndCreateSession", retVal);
    btnSwapAct.putValue(Action.ACTION_COMMAND_KEY, "CANCEL_CREATE_ITEM");
    btnSwapAct.putValue(Action.NAME, "Cancel create");
    btnSwapAct.putValue(Action.SHORT_DESCRIPTION, "Cancel create");
    btnSwapAct.putValue(Action.SMALL_ICON, cancelIcn);
    btnSwapAct.putValue(Action.LARGE_ICON_KEY, cancelIcnL);
    retVal.addAction(btnSwapAct);

    JPanel sessionNamePanel = new JPanel(new BorderLayout());
    sessionNamePanel.setOpaque(false);

    final JTextField sessionNameField = new JTextField();
    sessionNameField.setDocument(new NameDocument());
    sessionNameField.setText("Session Name");
    sessionNamePanel.add(sessionNameField, BorderLayout.CENTER);

    ActionMap actionMap = retVal.getActionMap();
    actionMap.put(btnSwapAct.getValue(Action.ACTION_COMMAND_KEY), btnSwapAct);
    InputMap inputMap = retVal.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);

    inputMap.put(ks, btnSwapAct.getValue(Action.ACTION_COMMAND_KEY));

    retVal.setActionMap(actionMap);
    retVal.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);

    PhonUIAction createNewSessionAct = new PhonUIAction(this, "onCreateSession", sessionNameField);
    createNewSessionAct.putValue(Action.SHORT_DESCRIPTION, "Create new session in selected corpus");
    createNewSessionAct.putValue(Action.SMALL_ICON,
            IconManager.getInstance().getIcon("actions/list-add", IconSize.SMALL));

    JButton createBtn = new JButton(createNewSessionAct);
    sessionNamePanel.add(createBtn, BorderLayout.EAST);

    sessionNameField.setAction(createNewSessionAct);

    // swap bottom component in new project button
    retVal.setBottomLabelText(WorkspaceTextStyler.toDescText(s2));
    retVal.add(sessionNamePanel, BorderLayout.CENTER);

    retVal.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {

        }

        @Override
        public void focusGained(FocusEvent e) {
            sessionNameField.requestFocus();
        }
    });

    return retVal;
}

From source file:com.alvermont.terraj.planet.ui.MainFrame.java

/**
 * Set a combo box to have a numeric editor with validation
 *
 * @param box The <code>JComboBox</code> to be set up
 *///from  ww  w  .j a v  a 2  s .  co  m
protected void setComboNumericEditor(JComboBox<? extends String> box) {
    final ComboBoxEditor editor = box.getEditor();
    final Component editorComponent = editor.getEditorComponent();

    if (editorComponent instanceof JTextField) {
        final JTextField textField = (JTextField) editorComponent;
        textField.setDocument(new CustomDocument());
    }
}

From source file:edu.ku.brc.af.ui.forms.validation.ValFormattedTextField.java

/**
 * Creates the various UI Components for the formatter.
 *//*from ww  w . j ava  2 s  . com*/
protected void createUI() {
    CellConstraints cc = new CellConstraints();

    if (isViewOnly
            || (formatter != null && !formatter.isUserInputNeeded() && fields != null && fields.size() == 1)) {
        viewtextField = new JTextField();
        setControlSize(viewtextField);

        // Remove by rods 12/5/08 this messes thihngs up
        // values don't get inserted correctly, shouldn't be needed anyway

        //JFormattedDoc document = new JFormattedDoc(viewtextField, formatter, formatter.getFields().get(0));
        //viewtextField.setDocument(document);
        //document.addDocumentListener(this);
        //documents.add(document);

        ViewFactory.changeTextFieldUIForDisplay(viewtextField, false);
        PanelBuilder builder = new PanelBuilder(new FormLayout("1px,f:p:g,1px", "1px,f:p:g,1px"), this);
        builder.add(viewtextField, cc.xy(2, 2));
        bgColor = viewtextField.getBackground();

    } else {
        JTextField txt = new JTextField();

        Font txtFont = txt.getFont();

        Font font = new Font("Courier", Font.PLAIN, txtFont.getSize());

        BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics(font);
        g.dispose();

        Insets ins = txt.getBorder().getBorderInsets(txt);
        int baseWidth = ins.left + ins.right;

        bgColor = txt.getBackground();

        StringBuilder sb = new StringBuilder("1px");
        for (UIFieldFormatterField f : fields) {
            sb.append(",");
            if (f.getType() == FieldType.separator || f.getType() == FieldType.constant) {
                sb.append('p');
            } else {
                sb.append(((fm.getMaxAdvance() * f.getSize()) + baseWidth) + "px");
            }
        }
        sb.append(",1px");
        PanelBuilder builder = new PanelBuilder(new FormLayout(sb.toString(), "1px,P:G,1px"), this);

        comps = new JComponent[fields.size()];
        int inx = 0;
        for (UIFieldFormatterField f : fields) {
            JComponent comp = null;
            JComponent tfToAdd = null;

            if (f.getType() == FieldType.separator || f.getType() == FieldType.constant) {
                comp = createLabel(f.getValue());
                if (f.getType() == FieldType.constant) {
                    comp.setBackground(Color.WHITE);
                    comp.setOpaque(true);
                }
                tfToAdd = comp;

            } else {
                JTextField tf = new BGTextField(f.getSize(), isViewOnly ? "" : f.getValue());
                tfToAdd = tf;

                if (inx == 0) {
                    tf.addKeyListener(new KeyAdapter() {
                        @Override
                        public void keyPressed(KeyEvent e) {
                            checkForPaste(e);
                        }
                    });
                }

                JFormattedDoc document = new JFormattedDoc(tf, formatter, f);
                tf.setDocument(document);
                document.addDocumentListener(new DocumentAdaptor() {
                    @Override
                    protected void changed(DocumentEvent e) {
                        isChanged = true;
                        if (!shouldIgnoreNotifyDoc) {
                            String fldStr = getText();
                            int len = StringUtils.isNotEmpty(fldStr) ? fldStr.length() : 0;
                            if (formatter != null && len > 0 && formatter.isLengthOK(len)) {
                                setState(formatter.isValid(fldStr) ? UIValidatable.ErrorType.Valid
                                        : UIValidatable.ErrorType.Error);
                                repaint();
                            }

                            //validateState();
                            if (changeListener != null) {
                                changeListener.stateChanged(new ChangeEvent(this));
                            }

                            if (documentListeners != null) {
                                for (DocumentListener dl : documentListeners) {
                                    dl.changedUpdate(null);
                                }
                            }
                        }
                        currCachedValue = null;
                    }
                });
                documents.add(document);

                addFocusAdapter(tf);

                comp = tf;
                comp.setFont(font);

                if (f.isIncrementer()) {
                    editTF = tf;
                    cardLayout = new CardLayout();
                    cardPanel = new JPanel(cardLayout);
                    cardPanel.add("edit", tf);

                    viewTF = new BGTextField(f.getSize(), isViewOnly ? "" : f.getValue());
                    viewTF.setDocument(document);
                    cardPanel.add("view", viewTF);

                    cardLayout.show(cardPanel, "view");
                    comp = cardPanel;
                    tfToAdd = cardPanel;
                }
            }

            setControlSize(tfToAdd);
            builder.add(comp, cc.xy(inx + 2, 2));
            comps[inx] = tfToAdd;
            inx++;
        }
    }
}

From source file:nz.govt.natlib.ndha.manualdeposit.StructMapFileDescMgmtPresenter.java

StructMapFileDescMgmtPresenter(final JList lstDescription, final JTextField textfldDescription,
        final JTextField textfldFilePrefix, final JCheckBox checkAllowMultiples, final JCheckBox checkMandatory,
        final JComboBox cmbPosition, final JTextField textfldDescriptionMain, final JCheckBox checkExtraLayers,
        final JTextField textfldDescriptionL2, final JTextField textfldFilePrefixL2,
        final JCheckBox checkAllowMultiplesL2, final JTextField textfldDescriptionL3,
        final JTextField textfldFilePrefixL3, final JCheckBox checkAllowMultiplesL3,
        final JTextField textfldDescriptionL4, final JTextField textfldFilePrefixL4,
        final JCheckBox checkAllowMultiplesL4, final JButton btnAddNew, final JButton btnDelete,
        final JButton btnSave, final JButton btnCancel, final JButton btnClose, final JButton btnMoveUp,
        final JButton btnMoveDown, final JButton btnGenMainDesc, final String xmlFileName,
        final IStructMapFileDescManagement theForm) {
    jlstDescription = lstDescription;//w  w w . ja  va 2  s  .  com
    this.textfldDescription = textfldDescription;
    this.textfldFilePrefix = textfldFilePrefix;
    this.checkAllowMultiples = checkAllowMultiples;
    this.checkMandatory = checkMandatory;
    cmboPosition = cmbPosition;
    this.checkExtraLayers = checkExtraLayers;
    this.textfldDescriptionMain = textfldDescriptionMain;

    // Extra Layers
    extraLayers = new HashMap<String, JComponent>();
    extraLayers.put(textfldDescriptionL2.getName(), textfldDescriptionL2);
    extraLayers.put(textfldFilePrefixL2.getName(), textfldFilePrefixL2);
    extraLayers.put(checkAllowMultiplesL2.getName(), checkAllowMultiplesL2);
    extraLayers.put(textfldDescriptionL3.getName(), textfldDescriptionL3);
    extraLayers.put(textfldFilePrefixL3.getName(), textfldFilePrefixL3);
    extraLayers.put(checkAllowMultiplesL3.getName(), checkAllowMultiplesL3);
    extraLayers.put(textfldDescriptionL4.getName(), textfldDescriptionL4);
    extraLayers.put(textfldFilePrefixL4.getName(), textfldFilePrefixL4);
    extraLayers.put(checkAllowMultiplesL4.getName(), checkAllowMultiplesL4);

    bttnAddNew = btnAddNew;
    bttnDelete = btnDelete;
    bttnSave = btnSave;
    bttnCancel = btnCancel;
    bttnClose = btnClose;
    bttnMoveUp = btnMoveUp;
    bttnMoveDown = btnMoveDown;
    bttnGenMainDesc = btnGenMainDesc;
    theXmlFileName = xmlFileName;
    theStructForm = theForm;
    loadData();

    // Restrict the values of this combo box to that of possible "Position"
    // enums
    cmboPosition.setModel(new DefaultComboBoxModel(FileType.Position.values()));
    // Restrict the contents of the description and file prefix to not allow
    // special characters
    textfldDescription.setDocument(new SpecialCharFilterDocument());
    textfldFilePrefix.setDocument(new SpecialCharFilterDocument());

    loadListData();
    addEventHandlers();
    checkButtons();
}