List of usage examples for java.awt.event FocusListener FocusListener
FocusListener
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField("A TextField"); textField.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); }/*from w w w . ja va 2 s . c o m*/ public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { System.out.println(e.getID() == FocusEvent.FOCUS_GAINED); } }); frame.add(textField, "North"); frame.add(new JTextField(), "South"); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField("A TextField"); textField.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); }/*w w w . j ava 2s . com*/ public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { System.out.println(e.getID() == FocusEvent.FOCUS_LOST); } }); frame.add(textField, "North"); frame.add(new JTextField(), "South"); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField("A TextField"); textField.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); }/*from w ww. j av a 2 s . co m*/ public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { System.out.println(e.getID() == FocusEvent.FOCUS_LAST); } }); frame.add(textField, "North"); frame.add(new JTextField(), "South"); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField("A TextField"); textField.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); }//from ww w . j a va2 s . c o m public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { System.out.println(e.getID() == FocusEvent.FOCUS_FIRST); } }); frame.add(textField, "North"); frame.add(new JTextField(), "South"); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField("A TextField"); textField.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); }//from w w w.ja va 2s .c o m public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { System.out.println(e.paramString()); } }); frame.add(textField, "North"); frame.add(new JTextField(), "South"); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { final JTextField textField = new JTextField(5); textField.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { }//from w w w .jav a 2s .com public void focusLost(FocusEvent e) { if (!e.isTemporary()) { String content = textField.getText(); if (!content.equals("a")) { System.out.println("illegal value! " + content); SwingUtilities.invokeLater(new FocusGrabber(textField)); } } } }); JFrame frame = new JFrame(); frame.add(textField, BorderLayout.CENTER); frame.setSize(300, 300); frame.setVisible(true); }
From source file:UsingFocusListener.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField("A TextField"); textField.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); }/*from ww w .ja v a 2 s . com*/ public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { System.out.println(prefix + (e.isTemporary() ? " (temporary):" : ":") + e.getComponent().getClass().getName() + "; Opposite component: " + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName() : "null")); } }); frame.add(textField, "North"); frame.add(new JTextField(), "South"); frame.setSize(300, 200); frame.setVisible(true); }
From source file:FocusTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); FocusListener listener = new FocusListener() { public void focusGained(FocusEvent e) { dumpInfo(e);// w w w . j av a 2 s . c o m } public void focusLost(FocusEvent e) { dumpInfo(e); } private void dumpInfo(FocusEvent e) { System.out.println("Source : " + name(e.getComponent())); System.out.println("Opposite : " + name(e.getOppositeComponent())); System.out.println("Temporary: " + e.isTemporary()); } private String name(Component c) { return (c == null) ? null : c.getName(); } }; // First JPanel panel = new JPanel(); JLabel label = new JLabel("Label 1: "); JTextField text = new JTextField("Type your text", 15); text.setName("First"); text.addFocusListener(listener); label.setDisplayedMnemonic(KeyEvent.VK_1); label.setLabelFor(text); panel.add(label); panel.add(text); contentPane.add(panel, BorderLayout.NORTH); // Second panel = new JPanel(); label = new JLabel("Label 2: "); text = new JTextField("14.0", 10); text.setName("Second"); text.addFocusListener(listener); text.setHorizontalAlignment(JTextField.RIGHT); label.setDisplayedMnemonic(KeyEvent.VK_2); label.setLabelFor(text); panel.add(label); panel.add(text); contentPane.add(panel, BorderLayout.SOUTH); frame.pack(); frame.show(); }
From source file:Main.java
public static void main(String... args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); JTextField tfield1 = new JTextField(10); JTextField tfield2 = new JTextField(10); FocusListener tfieldListener = new FocusListener() { @Override/*from ww w.ja v a 2s. c o m*/ public void focusGained(FocusEvent fe) { } @Override public void focusLost(FocusEvent fe) { String num1 = tfield1.getText().trim(); String num2 = tfield2.getText().trim(); if (num1 == null || num1.equals("")) num1 = "0"; if (num2 == null || num2.equals("")) num2 = "0"; System.out.println(Integer.toString(Integer.parseInt(num1) + Integer.parseInt(num2))); } }; tfield1.addFocusListener(tfieldListener); tfield2.addFocusListener(tfieldListener); ((AbstractDocument) tfield1.getDocument()).setDocumentFilter(new MyDocumentFilter()); ((AbstractDocument) tfield2.getDocument()).setDocumentFilter(new MyDocumentFilter()); contentPane.add(tfield1); contentPane.add(tfield2); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); }
From source file:Main.java
public static void addPlaceHolder(final JTextField field, final String placeHolderText) { field.addFocusListener(new FocusListener() { private Color fontColor = field.getForeground(); // private String previousText=field.getText(); public void focusGained(FocusEvent arg0) { if (field.getText().equals(placeHolderText)) { field.setText(""); }/*w w w . j ava2 s . co m*/ field.setForeground(fontColor); } public void focusLost(FocusEvent arg0) { if (field.getText().trim().equals("")) { fontColor = field.getForeground(); field.setForeground(Color.GRAY); field.setText(placeHolderText); } } }); if (field.getText().trim().equals("")) { field.setText(placeHolderText); field.setForeground(Color.GRAY); } }