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);
}
}
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);
}
}
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("");
}
}
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;
}
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);
}
}
Aggregations