List of usage examples for javax.swing JTextField addFocusListener
public synchronized void addFocusListener(FocusListener l)
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// w ww . j a v a 2 s . com 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 ww.j av a2 s. c o 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); } }
From source file:Main.java
public Main() { JTextField textField = new JTextField("A TextField"); textField.addFocusListener(this); JLabel label = new JLabel("A Label"); label.addFocusListener(this); add(label);//from w w w.j a va 2 s. c o m String comboPrefix = "Item #"; final int numItems = 15; Vector vector = new Vector(numItems); for (int i = 0; i < numItems; i++) { vector.addElement(comboPrefix + i); } JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); add(comboBox); JButton button = new JButton("A Button"); button.addFocusListener(this); add(button); JList list = new JList(vector); list.setSelectedIndex(1); list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); listScrollPane.getVerticalScrollBar().setFocusable(false); listScrollPane.getHorizontalScrollBar().setFocusable(false); add(listScrollPane); setPreferredSize(new Dimension(450, 450)); }
From source file:Main.java
public Main() { JTextField textField = new JTextField("A TextField"); textField.addFocusListener(this); JLabel label = new JLabel("A Label"); label.addFocusListener(this); add(label);//from ww w. j ava2 s . co m String comboPrefix = "ComboBox Item #"; final int numItems = 15; Vector vector = new Vector(numItems); for (int i = 0; i < numItems; i++) { vector.addElement(comboPrefix + i); } JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); add(comboBox); JButton button = new JButton("A Button"); button.addFocusListener(this); add(button); String listPrefix = "List Item #"; Vector listVector = new Vector(numItems); for (int i = 0; i < numItems; i++) { listVector.addElement(listPrefix + i); } JList list = new JList(listVector); list.setSelectedIndex(1); list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); listScrollPane.getVerticalScrollBar().setFocusable(false); listScrollPane.getHorizontalScrollBar().setFocusable(false); add(listScrollPane); setPreferredSize(new Dimension(450, 450)); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); }
From source file:ru.codemine.pos.ui.salespanel.QuantitySetupWindow.java
public void showWindow() { if (!actionListenersInit) setupActionListeners();/* w w w . j a v a2 s . c o m*/ mainWindow.blockBarcodeInput(); Product product = salesPanel.getChequeSetupPanel().getSelectedProduct(); if (product == null) { mainWindow.unblockBarcodeInput(); return; } int maxQuantity = storeService.getAvaibleStocksOnRetail(product); onStoresLabel.setText("?: " + String.valueOf(maxQuantity) + " ."); setTitle("? - " + product.getName()); spinner.setModel(new SpinnerNumberModel(1, 1, maxQuantity, 1)); JTextField ft = ((WebSpinner.DefaultEditor) spinner.getEditor()).getTextField(); ft.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { final JTextComponent txtcomp = (JTextComponent) e.getSource(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { txtcomp.selectAll(); } }); } }); ft.requestFocus(); setVisible(true); }
From source file:events.FocusEventDemo.java
public void addComponentsToPane(final Container pane) { GridBagLayout gridbag = new GridBagLayout(); pane.setLayout(gridbag);//from ww w. j a v a2s .com GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; //Make column as wide as possible. JTextField textField = new JTextField("A TextField"); textField.setMargin(new Insets(0, 2, 0, 2)); textField.addFocusListener(this); gridbag.setConstraints(textField, c); add(textField); c.weightx = 0.1; //Widen every other column a bit, when possible. c.fill = GridBagConstraints.NONE; JLabel label = new JLabel("A Label"); label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); label.addFocusListener(this); gridbag.setConstraints(label, c); add(label); String comboPrefix = "ComboBox Item #"; final int numItems = 15; Vector<String> vector = new Vector<String>(numItems); for (int i = 0; i < numItems; i++) { vector.addElement(comboPrefix + i); } JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); gridbag.setConstraints(comboBox, c); add(comboBox); c.gridwidth = GridBagConstraints.REMAINDER; JButton button = new JButton("A Button"); button.addFocusListener(this); gridbag.setConstraints(button, c); add(button); c.weightx = 0.0; c.weighty = 0.1; c.fill = GridBagConstraints.BOTH; String listPrefix = "List Item #"; Vector<String> listVector = new Vector<String>(numItems); for (int i = 0; i < numItems; i++) { listVector.addElement(listPrefix + i); } JList list = new JList(listVector); list.setSelectedIndex(1); //It's easier to see the focus change //if an item is selected. list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); gridbag.setConstraints(listScrollPane, c); add(listScrollPane); c.weighty = 1.0; //Make this row as tall as possible. c.gridheight = GridBagConstraints.REMAINDER; //Set up the area that reports focus-gained and focus-lost events. display = new JTextArea(); display.setEditable(false); //The setRequestFocusEnabled method prevents a //component from being clickable, but it can still //get the focus through the keyboard - this ensures //user accessibility. display.setRequestFocusEnabled(false); display.addFocusListener(this); JScrollPane displayScrollPane = new JScrollPane(display); gridbag.setConstraints(displayScrollPane, c); add(displayScrollPane); setPreferredSize(new Dimension(450, 450)); ((JPanel) pane).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); }
From source file:FocusEventDemo.java
public void addComponentsToPane(final Container pane) { GridBagLayout gridbag = new GridBagLayout(); pane.setLayout(gridbag);/*from w w w . j av a 2 s . c om*/ GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; // Make column as wide as possible. JTextField textField = new JTextField("A TextField"); textField.setMargin(new Insets(0, 2, 0, 2)); textField.addFocusListener(this); gridbag.setConstraints(textField, c); add(textField); c.weightx = 0.1; // Widen every other column a bit, when possible. c.fill = GridBagConstraints.NONE; JLabel label = new JLabel("A Label"); label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); label.addFocusListener(this); gridbag.setConstraints(label, c); add(label); String comboPrefix = "ComboBox Item #"; final int numItems = 15; Vector<String> vector = new Vector<String>(numItems); for (int i = 0; i < numItems; i++) { vector.addElement(comboPrefix + i); } JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); gridbag.setConstraints(comboBox, c); add(comboBox); c.gridwidth = GridBagConstraints.REMAINDER; JButton button = new JButton("A Button"); button.addFocusListener(this); gridbag.setConstraints(button, c); add(button); c.weightx = 0.0; c.weighty = 0.1; c.fill = GridBagConstraints.BOTH; String listPrefix = "List Item #"; Vector<String> listVector = new Vector<String>(numItems); for (int i = 0; i < numItems; i++) { listVector.addElement(listPrefix + i); } JList list = new JList(listVector); list.setSelectedIndex(1); // It's easier to see the focus change // if an item is selected. list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); gridbag.setConstraints(listScrollPane, c); add(listScrollPane); c.weighty = 1.0; // Make this row as tall as possible. c.gridheight = GridBagConstraints.REMAINDER; // Set up the area that reports focus-gained and focus-lost events. display = new JTextArea(); display.setEditable(false); // The setRequestFocusEnabled method prevents a // component from being clickable, but it can still // get the focus through the keyboard - this ensures // user accessibility. display.setRequestFocusEnabled(false); display.addFocusListener(this); JScrollPane displayScrollPane = new JScrollPane(display); gridbag.setConstraints(displayScrollPane, c); add(displayScrollPane); setPreferredSize(new Dimension(450, 450)); ((JPanel) pane).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); }
From source file:FocusEventDemo.java
public FocusEventDemo() { super(new GridBagLayout()); GridBagLayout gridbag = (GridBagLayout) getLayout(); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; //Make column as wide as possible. JTextField textField = new JTextField("A TextField"); textField.setMargin(new Insets(0, 2, 0, 2)); textField.addFocusListener(this); gridbag.setConstraints(textField, c); add(textField);/* ww w . ja v a2s.c o m*/ c.weightx = 0.1; //Widen every other column a bit, when possible. c.fill = GridBagConstraints.NONE; JLabel label = new JLabel("A Label"); label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); label.addFocusListener(this); gridbag.setConstraints(label, c); add(label); String comboPrefix = "ComboBox Item #"; final int numItems = 15; Vector vector = new Vector(numItems); for (int i = 0; i < numItems; i++) { vector.addElement(comboPrefix + i); } JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); gridbag.setConstraints(comboBox, c); add(comboBox); c.gridwidth = GridBagConstraints.REMAINDER; JButton button = new JButton("A Button"); button.addFocusListener(this); gridbag.setConstraints(button, c); add(button); c.weightx = 0.0; c.weighty = 0.1; c.fill = GridBagConstraints.BOTH; String listPrefix = "List Item #"; Vector listVector = new Vector(numItems); for (int i = 0; i < numItems; i++) { listVector.addElement(listPrefix + i); } JList list = new JList(listVector); list.setSelectedIndex(1); //It's easier to see the focus change //if an item is selected. list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); //We want to prevent the list's scroll bars //from getting the focus - even with the keyboard. //Note that in general we prefer setRequestFocusable //over setFocusable for reasons of accessibility, //but this is to work around bug #4866958. listScrollPane.getVerticalScrollBar().setFocusable(false); listScrollPane.getHorizontalScrollBar().setFocusable(false); gridbag.setConstraints(listScrollPane, c); add(listScrollPane); c.weighty = 1.0; //Make this row as tall as possible. c.gridheight = GridBagConstraints.REMAINDER; //Set up the area that reports focus-gained and focus-lost events. display = new JTextArea(); display.setEditable(false); //The method setRequestFocusEnabled prevents a //component from being clickable, but it can still //get the focus through the keyboard - this ensures //user accessibility. display.setRequestFocusEnabled(false); display.addFocusListener(this); JScrollPane displayScrollPane = new JScrollPane(display); //Work around for bug #4866958. displayScrollPane.getHorizontalScrollBar().setFocusable(false); displayScrollPane.getVerticalScrollBar().setFocusable(false); gridbag.setConstraints(displayScrollPane, c); add(displayScrollPane); setPreferredSize(new Dimension(450, 450)); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); }
From source file:com.github.cric.app.ui.SettingPanel.java
private JTextField textField(String defaultValue, boolean isInt) { JTextField field = new JTextField(defaultValue); addBorder(field, Color.GRAY); field.addFocusListener(new FocusListener() { @Override/*ww w.ja v a 2s. c o m*/ public void focusLost(FocusEvent e) { handleFocusLost(field, isInt); } @Override public void focusGained(FocusEvent e) { addBorder(field, Color.GRAY); field.selectAll(); } }); return field; }
From source file:de.fhg.iais.asc.ui.parts.HarvesterPanel.java
private JTextField createSetsTextField() { final JTextField textField = new JTextField(30); textField.addFocusListener(new FocusAdapter() { @Override//from ww w. j ava2s.co m public void focusGained(FocusEvent e) { e.getOppositeComponent().requestFocus(); // get the frame the panel is embedded in Component currentComponent = HarvesterPanel.this; while (currentComponent.getParent() != null && !(currentComponent instanceof JFrame)) { currentComponent = currentComponent.getParent(); } final JFrame parentFrame = currentComponent instanceof JFrame ? (JFrame) currentComponent : null; if (HarvesterPanel.this.availableSets == null) { LocalizedOptionPane.showMessageDialog(parentFrame, "No_setnames_retrieved", JOptionPane.ERROR_MESSAGE); return; } String[] selectedSets = textField.getText().split(","); //$NON-NLS-1$ for (int i = 0; i < selectedSets.length; i++) { selectedSets[i] = selectedSets[i].trim(); } SetSelectionWindow ssw = new SetSelectionWindow(parentFrame, HarvesterPanel.this.availableSets, selectedSets); ssw.setVisible(true); textField.setText(StringUtils.join(ssw.getSelectedSets(), ", ")); } }); return textField; }