Search in sources :

Example 76 with JTextComponent

use of javax.swing.text.JTextComponent in project java-swing-tips by aterai.

the class MainPanel method makeTextField.

public JTextField makeTextField(JButton button) {
    JTextField textField = new JTextField(24);
    textField.setInputVerifier(new InputVerifier() {

        @Override
        public boolean verify(JComponent c) {
            if (c instanceof JTextComponent) {
                JTextComponent tc = (JTextComponent) c;
                String str = tc.getText().trim();
                return !str.isEmpty() && MAX_LEN - str.length() >= 0;
            }
            return false;
        }

        @Override
        public boolean shouldYieldFocus(JComponent input) {
            System.out.println("shouldYieldFocus");
            button.setEnabled(isAllValid());
            return super.shouldYieldFocus(input);
        }
    });
    textField.addFocusListener(new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent e) {
            if (e.isTemporary()) {
                return;
            }
            System.out.println("focusLost");
            button.setEnabled(isAllValid());
        }
    });
    // });
    return textField;
}
Also used : FocusAdapter(java.awt.event.FocusAdapter) JTextComponent(javax.swing.text.JTextComponent) FocusEvent(java.awt.event.FocusEvent)

Example 77 with JTextComponent

use of javax.swing.text.JTextComponent in project java-swing-tips by aterai.

the class GhostFocusListener method focusLost.

@Override
public void focusLost(FocusEvent e) {
    JTextComponent textField = (JTextComponent) e.getComponent();
    String str = textField.getText().trim();
    if ("".equals(str)) {
        textField.setForeground(INACTIVE_COLOR);
        textField.setText(ghostMessage);
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

Example 78 with JTextComponent

use of javax.swing.text.JTextComponent in project java-swing-tips by aterai.

the class SizeFilter method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    if (Objects.nonNull(target) && target.isEditable()) {
        Caret caret = target.getCaret();
        int dot = caret.getDot();
        int mark = caret.getMark();
        if (DefaultEditorKit.deletePrevCharAction.equals(getValue(Action.NAME))) {
            // @see javax/swing/text/DefaultEditorKit.java DeletePrevCharAction
            if (dot == 0 && mark == 0) {
                return;
            }
        } else {
            // @see javax/swing/text/DefaultEditorKit.java DeleteNextCharAction
            Document doc = target.getDocument();
            if (dot == mark && doc.getLength() == dot) {
                return;
            }
        }
    }
    deleteAction.actionPerformed(e);
}
Also used : JTextComponent(javax.swing.text.JTextComponent) AbstractDocument(javax.swing.text.AbstractDocument) Document(javax.swing.text.Document) Caret(javax.swing.text.Caret)

Example 79 with JTextComponent

use of javax.swing.text.JTextComponent in project java-swing-tips by aterai.

the class TextComponentPopupMenu method show.

@Override
public void show(Component c, int x, int y) {
    if (c instanceof JTextComponent) {
        JTextComponent tc = (JTextComponent) c;
        boolean hasSelectedText = Objects.nonNull(tc.getSelectedText());
        copyAction.setEnabled(hasSelectedText);
        super.show(c, x, y);
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

Example 80 with JTextComponent

use of javax.swing.text.JTextComponent in project zaproxy by zaproxy.

the class FindDialog method find.

private void find() {
    JTextComponent txtComp = lastInvoker;
    if (txtComp == null) {
        JFrame parent = (JFrame) (this.getParent());
        Component c = parent.getMostRecentFocusOwner();
        if (c instanceof JTextComponent) {
            txtComp = (JTextComponent) c;
        }
    }
    infoLabel.setVisible(false);
    // ZAP: Check if a JTextComponent was really found.
    if (txtComp == null) {
        return;
    }
    try {
        String findText = txtFind.getText().toLowerCase();
        String txt = txtComp.getText().toLowerCase();
        int startPos = txt.indexOf(findText, txtComp.getCaretPosition());
        // Enable Wrap Search
        if (startPos <= 0) {
            txtComp.setCaretPosition(0);
            startPos = txt.indexOf(findText, txtComp.getCaretPosition());
        }
        int length = findText.length();
        if (startPos > -1) {
            txtComp.setSelectionColor(DisplayUtils.getHighlightColor());
            txtComp.select(startPos, startPos + length);
            txtComp.requestFocusInWindow();
            txtFind.requestFocusInWindow();
        } else {
            infoLabel.setVisible(true);
        }
    } catch (Exception e) {
        LOGGER.error("An error occurred while finding:", e);
    }
}
Also used : JFrame(javax.swing.JFrame) JTextComponent(javax.swing.text.JTextComponent) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) HeadlessException(java.awt.HeadlessException)

Aggregations

JTextComponent (javax.swing.text.JTextComponent)181 Component (java.awt.Component)28 JComponent (javax.swing.JComponent)16 BadLocationException (javax.swing.text.BadLocationException)13 NotNull (org.jetbrains.annotations.NotNull)13 DocumentEvent (javax.swing.event.DocumentEvent)11 DocumentAdapter (com.intellij.ui.DocumentAdapter)8 Caret (javax.swing.text.Caret)8 Document (javax.swing.text.Document)8 Point (java.awt.Point)7 ActionEvent (java.awt.event.ActionEvent)7 ActionListener (java.awt.event.ActionListener)7 ArrayList (java.util.ArrayList)7 ComboBoxEditor (javax.swing.ComboBoxEditor)7 FocusEvent (java.awt.event.FocusEvent)6 Color (java.awt.Color)5 KeyEvent (java.awt.event.KeyEvent)5 MouseEvent (java.awt.event.MouseEvent)5 JButton (javax.swing.JButton)5 JComboBox (javax.swing.JComboBox)5