Search in sources :

Example 71 with JTextComponent

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

the class TextFieldPopupMenu 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());
        for (MenuElement menuElement : getSubElements()) {
            Component m = menuElement.getComponent();
            Action a = m instanceof JMenuItem ? ((JMenuItem) m).getAction() : null;
            if (a instanceof DefaultEditorKit.PasteAction) {
                continue;
            }
            m.setEnabled(hasSelectedText);
        }
        super.show(c, x, y);
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent) JTextComponent(javax.swing.text.JTextComponent)

Example 72 with JTextComponent

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

the class PlaceholderLayerUI method focusLost.

@Override
public void focusLost(FocusEvent e) {
    JTextComponent tf = (JTextComponent) e.getComponent();
    if ("".equals(tf.getText().trim())) {
        tf.setForeground(INACTIVE);
        tf.setText(hintMessage);
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

Example 73 with JTextComponent

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

the class PlaceholderLayerUI method focusGained.

@Override
public void focusGained(FocusEvent e) {
    JTextComponent tf = (JTextComponent) e.getComponent();
    if (hintMessage.equals(tf.getText()) && INACTIVE.equals(tf.getForeground())) {
        tf.setForeground(UIManager.getColor("TextField.foreground"));
        tf.setText("");
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

Example 74 with JTextComponent

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

the class TextComponentPopupMenu method makePopupMenu.

private static JPopupMenu makePopupMenu() {
    Action cutAction = new DefaultEditorKit.CutAction();
    Action copyAction = new DefaultEditorKit.CopyAction();
    Action pasteAction = new DefaultEditorKit.PasteAction();
    JPopupMenu popup1 = new JPopupMenu();
    popup1.add(cutAction);
    popup1.add(copyAction);
    popup1.add(pasteAction);
    popup1.addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
        /* not needed */
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        /* not needed */
        }

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            JPopupMenu pop = (JPopupMenu) e.getSource();
            JTextComponent tc = (JTextComponent) pop.getInvoker();
            System.out.println(tc.getClass().getName() + ": " + tc.getName());
            // TEST:
            // tc.requestFocusInWindow();
            // tc.selectAll();
            boolean hasSelectedText = Objects.nonNull(tc.getSelectedText());
            cutAction.setEnabled(hasSelectedText);
            copyAction.setEnabled(hasSelectedText);
        }
    });
    return popup1;
}
Also used : PopupMenuListener(javax.swing.event.PopupMenuListener) JTextComponent(javax.swing.text.JTextComponent) PopupMenuEvent(javax.swing.event.PopupMenuEvent)

Example 75 with JTextComponent

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

the class RedoAction 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());
        cutAction.setEnabled(hasSelectedText);
        copyAction.setEnabled(hasSelectedText);
        deleteAction.setEnabled(hasSelectedText);
        super.show(c, x, y);
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent)

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