List of usage examples for javax.swing JTextField JTextField
public JTextField()
TextField
. From source file:DefaultFormatterFactoryDemo.java
public static void main(String args[]) { JFrame frame = new JFrame("Mask Input"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Date"); DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd"); DateFormatter displayFormatter = new DateFormatter(displayFormat); DateFormat editFormat = new SimpleDateFormat("MM/dd/yy"); DateFormatter editFormatter = new DateFormatter(editFormat); DateFormat nullFormat = new SimpleDateFormat("'null'"); DateFormatter nullFormatter = new DateFormatter(nullFormat); DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter, nullFormatter); JFormattedTextField input = new JFormattedTextField(factory); input.setColumns(30);/*from w w w . j a va 2 s . co m*/ JPanel panel = new JPanel(); panel.add(label); panel.add(input); frame.add(panel, "North"); frame.add(new JTextField(), "Center"); frame.pack(); frame.setVisible(true); }
From source file:JTextFieldSample.java
public static void main(String args[]) { String title = (args.length == 0 ? "TextField Listener Example" : args[0]); JFrame frame = new JFrame(title); Container content = frame.getContentPane(); JPanel namePanel = new JPanel(new BorderLayout()); JLabel nameLabel = new JLabel("Name: "); nameLabel.setDisplayedMnemonic(KeyEvent.VK_N); JTextField nameTextField = new JTextField(); nameLabel.setLabelFor(nameTextField); namePanel.add(nameLabel, BorderLayout.WEST); namePanel.add(nameTextField, BorderLayout.CENTER); content.add(namePanel, BorderLayout.NORTH); JPanel cityPanel = new JPanel(new BorderLayout()); JLabel cityLabel = new JLabel("City: "); cityLabel.setDisplayedMnemonic(KeyEvent.VK_C); JTextField cityTextField = new JTextField(); cityLabel.setLabelFor(cityTextField); cityPanel.add(cityLabel, BorderLayout.WEST); cityPanel.add(cityTextField, BorderLayout.CENTER); content.add(cityPanel, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Command: " + actionEvent.getActionCommand()); }//from w w w . j av a 2 s .c om }; nameTextField.setActionCommand("Yo"); nameTextField.addActionListener(actionListener); cityTextField.addActionListener(actionListener); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent keyEvent) { printIt("Pressed", keyEvent); } public void keyReleased(KeyEvent keyEvent) { printIt("Released", keyEvent); } public void keyTyped(KeyEvent keyEvent) { printIt("Typed", keyEvent); } private void printIt(String title, KeyEvent keyEvent) { int keyCode = keyEvent.getKeyCode(); String keyText = KeyEvent.getKeyText(keyCode); System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar()); } }; nameTextField.addKeyListener(keyListener); cityTextField.addKeyListener(keyListener); InputVerifier verifier = new InputVerifier() { public boolean verify(JComponent input) { final JTextComponent source = (JTextComponent) input; String text = source.getText(); if ((text.length() != 0) && !(text.equals("Exit"))) { Runnable runnable = new Runnable() { public void run() { JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE); } }; SwingUtilities.invokeLater(runnable); return false; } else { return true; } } }; nameTextField.setInputVerifier(verifier); cityTextField.setInputVerifier(verifier); DocumentListener documentListener = new DocumentListener() { public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } private void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); String typeString = null; if (type.equals(DocumentEvent.EventType.CHANGE)) { typeString = "Change"; } else if (type.equals(DocumentEvent.EventType.INSERT)) { typeString = "Insert"; } else if (type.equals(DocumentEvent.EventType.REMOVE)) { typeString = "Remove"; } System.out.print("Type : " + typeString + " / "); Document source = documentEvent.getDocument(); int length = source.getLength(); try { System.out.println("Contents: " + source.getText(0, length)); } catch (BadLocationException badLocationException) { System.out.println("Contents: Unknown"); } } }; nameTextField.getDocument().addDocumentListener(documentListener); cityTextField.getDocument().addDocumentListener(documentListener); frame.setSize(250, 150); frame.setVisible(true); }
From source file:FormattedSample.java
public static void main(final String args[]) { JFrame frame = new JFrame("Formatted Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd"); DateFormatter displayFormatter = new DateFormatter(displayFormat); DateFormat editFormat = new SimpleDateFormat("MM/dd/yy"); DateFormatter editFormatter = new DateFormatter(editFormat); DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter);//ww w . j av a 2 s .c o m JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date()); frame.add(date2TextField, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JFormattedTextField source = (JFormattedTextField) actionEvent.getSource(); Object value = source.getValue(); System.out.println("Class: " + value.getClass()); System.out.println("Value: " + value); } }; date2TextField.addActionListener(actionListener); frame.add(new JTextField(), BorderLayout.SOUTH); frame.setSize(250, 100); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame frame = new JFrame("Formatted Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd"); DateFormatter displayFormatter = new DateFormatter(displayFormat); DateFormat editFormat = new SimpleDateFormat("MM/dd/yy"); DateFormatter editFormatter = new DateFormatter(editFormat); DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter, editFormatter);/*from ww w .j a v a 2 s. c o m*/ JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date()); frame.add(date2TextField, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JFormattedTextField source = (JFormattedTextField) actionEvent.getSource(); Object value = source.getValue(); System.out.println("Class: " + value.getClass()); System.out.println("Value: " + value); } }; date2TextField.addActionListener(actionListener); frame.add(new JTextField(), BorderLayout.SOUTH); frame.setSize(250, 100); frame.setVisible(true); }
From source file:ActionsMenuBar.java
public static void main(String args[]) { final JFrame frame = new JFrame("TextAction Usage"); Container contentPane = frame.getContentPane(); final JScrollPane scrollPane = new JScrollPane(); contentPane.add(scrollPane, BorderLayout.CENTER); final JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar);/*from w w w. j a v a 2s. co m*/ ActionListener actionListener = new ActionListener() { JTextComponent component; public void actionPerformed(ActionEvent actionEvent) { // Determine which component selected String command = actionEvent.getActionCommand(); if (command.equals("JTextField")) { component = new JTextField(); } else if (command.equals("JPasswordField")) { component = new JPasswordField(); } else if (command.equals("JTextArea")) { component = new JTextArea(); } else if (command.equals("JTextPane")) { component = new JTextPane(); } else { component = new JEditorPane(); } scrollPane.setViewportView(component); // Process action list Action actions[] = component.getActions(); menuBar.removeAll(); menuBar.revalidate(); JMenu menu = null; for (int i = 0, n = actions.length; i < n; i++) { if ((i % 10) == 0) { menu = new JMenu("From " + i); menuBar.add(menu); } menu.add(actions[i]); } menuBar.revalidate(); } }; String components[] = { "JTextField", "JPasswordField", "JTextArea", "JTextPane", "JEditorPane" }; final Container componentsContainer = RadioButtonUtils.createRadioButtonGrouping(components, "Pick to List Actions", actionListener); contentPane.add(componentsContainer, BorderLayout.WEST); frame.setSize(400, 300); frame.setVisible(true); }
From source file:KeyTest.java
public static void main(String args[]) { JFrame frame = new JFrame("Key Listener"); Container contentPane = frame.getContentPane(); KeyListener listener = new KeyListener() { public void keyPressed(KeyEvent e) { dumpInfo("Pressed", e); }/*from w w w.j a v a 2s.c om*/ public void keyReleased(KeyEvent e) { dumpInfo("Released", e); } public void keyTyped(KeyEvent e) { dumpInfo("Typed", e); } private void dumpInfo(String s, KeyEvent e) { System.out.println(s); int code = e.getKeyCode(); System.out.println("\tCode: " + KeyEvent.getKeyText(code)); System.out.println("\tChar: " + e.getKeyChar()); int mods = e.getModifiersEx(); System.out.println("\tMods: " + KeyEvent.getModifiersExText(mods)); System.out.println("\tLocation: " + location(e.getKeyLocation())); System.out.println("\tAction? " + e.isActionKey()); } private String location(int location) { switch (location) { case KeyEvent.KEY_LOCATION_LEFT: return "Left"; case KeyEvent.KEY_LOCATION_RIGHT: return "Right"; case KeyEvent.KEY_LOCATION_NUMPAD: return "NumPad"; case KeyEvent.KEY_LOCATION_STANDARD: return "Standard"; case KeyEvent.KEY_LOCATION_UNKNOWN: default: return "Unknown"; } } }; JTextField text = new JTextField(); text.addKeyListener(listener); contentPane.add(text, BorderLayout.NORTH); frame.pack(); frame.show(); }
From source file:KeyTextComponent.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new KeyTextComponent(), BorderLayout.CENTER); JTextField field = new JTextField(); field.setText("Click above blank area to request focus"); frame.add(field, BorderLayout.SOUTH); frame.setSize(300, 100);/*from ww w . j a va 2 s.c o m*/ frame.setVisible(true); field.requestFocus(); }
From source file:KeyTextComponent.java
public static void main(String[] a) { JFrame frame = new JFrame("Key Text Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); KeyTextComponent keyTextComponent = new KeyTextComponent(); final JTextField textField = new JTextField(); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String keyText = actionEvent.getActionCommand(); textField.setText(keyText);//from www. j a v a 2 s . c o m } }; keyTextComponent.addActionListener(actionListener); frame.add(keyTextComponent, BorderLayout.CENTER); textField.setText("Press keyboard after clicking the above blank area"); frame.add(textField, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:de.codesourcery.eve.skills.ui.spreadsheet.SpreadSheetTableModel.java
public static void main(String[] args) { final ITableFactory cellFactory = new ITableFactory() { @Override/* w w w . j av a 2 s.c om*/ public ITableCell createEmptyCell(int row, int column) { return new SimpleCell(); } @Override public TableRow createRow(SpreadSheetTableModel tableModel) { return new TableRow(tableModel); } }; final SpreadSheetTableModel model = new SpreadSheetTableModel(cellFactory); final JTable table = new JTable(model); table.setFillsViewportHeight(true); final JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // table.setPreferredSize( new Dimension(400,200 ) ); table.setBorder(BorderFactory.createLineBorder(Color.black)); frame.getContentPane().add(new JScrollPane(table)); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); model.addRow(new SimpleCell("First row") { public boolean isEditable() { return true; } public void setValue(Object value) { System.out.println("new value: " + value); } }); model.addRow(new SimpleCell("Second row #1"), new SimpleCell("Second row #2")); model.addRow(new SimpleCell("Third row #1"), new SimpleCell("Third row #2"), new SimpleCell("Third row #3")); JTextField tf = new JTextField(); table.setModel(model); table.setDefaultEditor(ITableCell.class, new DefaultCellEditor(tf)); } }); }
From source file:DragImage.java
public static void main(String args[]) { JFrame frame = new JFrame("Clip Image"); Container contentPane = frame.getContentPane(); final Clipboard clipboard = frame.getToolkit().getSystemClipboard(); Icon icon = new ImageIcon("jaeger.jpg"); final JLabel label = new JLabel(icon); label.setTransferHandler(new ImageSelection()); MouseListener mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent e) { JComponent comp = (JComponent) e.getSource(); TransferHandler handler = comp.getTransferHandler(); handler.exportAsDrag(comp, e, TransferHandler.COPY); }//from ww w.java 2s . c om }; label.addMouseListener(mouseListener); JScrollPane pane = new JScrollPane(label); contentPane.add(pane, BorderLayout.CENTER); JButton copy = new JButton("Copy"); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TransferHandler handler = label.getTransferHandler(); handler.exportToClipboard(label, clipboard, TransferHandler.COPY); } }); JButton clear = new JButton("Clear"); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { label.setIcon(null); } }); clear.setTransferHandler(new TransferHandler("text")); mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent e) { JComponent comp = (JComponent) e.getSource(); TransferHandler handler = comp.getTransferHandler(); handler.exportAsDrag(comp, e, TransferHandler.COPY); } }; clear.addMouseListener(mouseListener); JButton paste = new JButton("Paste"); paste.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Transferable clipData = clipboard.getContents(clipboard); if (clipData != null) { if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) { TransferHandler handler = label.getTransferHandler(); handler.importData(label, clipData); } } } }); JPanel p = new JPanel(); p.add(copy); p.add(clear); p.add(paste); contentPane.add(p, BorderLayout.SOUTH); JTextField tf = new JTextField(); tf.setDragEnabled(true); contentPane.add(tf, BorderLayout.NORTH); frame.setSize(300, 300); frame.show(); }