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