List of usage examples for javax.swing JLabel setDisplayedMnemonic
public void setDisplayedMnemonic(char aChar)
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame("LabelFor Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Username"); JTextField textField = new JTextField(); label.setDisplayedMnemonic(KeyEvent.VK_U); label.setLabelFor(textField);//from w ww . ja v a2 s.c o m Container box = Box.createHorizontalBox(); box.add(label); box.add(textField); frame.add(box, BorderLayout.NORTH); frame.add(new JButton("Submit"), BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:LabelFor.java
public static void main(String args[]) { JFrame f = new JFrame("LabelFor Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); JLabel label = new JLabel("Username"); JTextField textField = new JTextField(); label.setDisplayedMnemonic(KeyEvent.VK_U); label.setLabelFor(textField);/*from w w w .j av a2 s . c o m*/ Container box = Box.createHorizontalBox(); box.add(label); box.add(textField); content.add(box, BorderLayout.NORTH); content.add(new JButton("Submit"), BorderLayout.SOUTH); f.setSize(300, 200); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JTextField firstField = new JTextField(10); JTextField middleField = new JTextField(10); JTextField lastField = new JTextField(10); JLabel firstLabel = new JLabel("First Name", JLabel.RIGHT); firstLabel.setDisplayedMnemonic('F'); firstLabel.setLabelFor(firstField);// www. j a v a 2s . c om JLabel middleLabel = new JLabel("Middle Initial", JLabel.RIGHT); middleLabel.setDisplayedMnemonic('I'); middleLabel.setDisplayedMnemonicIndex(7); middleLabel.setLabelFor(middleField); JLabel lastLabel = new JLabel("Last Name", JLabel.RIGHT); lastLabel.setDisplayedMnemonic('L'); lastLabel.setLabelFor(lastField); JPanel p = new JPanel(); p.setLayout(new GridLayout(3, 2, 5, 5)); p.add(firstLabel); p.add(firstField); p.add(middleLabel); p.add(middleField); p.add(lastLabel); p.add(lastField); JFrame f = new JFrame("MnemonicLabels"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(p); f.pack(); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JLabel l; JTextField t;/*from www . j ava 2 s.c o m*/ JButton b; JFrame f = new JFrame("Text Accelerator"); f.add(l = new JLabel("Name:", SwingConstants.RIGHT)); l.setDisplayedMnemonic('n'); f.add(l = new JLabel("House/Street:", SwingConstants.RIGHT)); l.setDisplayedMnemonic('h'); f.add(l = new JLabel("City:", SwingConstants.RIGHT)); l.setDisplayedMnemonic('c'); f.add(l = new JLabel("State/County:", SwingConstants.RIGHT)); l.setDisplayedMnemonic('s'); f.add(l = new JLabel("Zip/Post code:", SwingConstants.RIGHT)); l.setDisplayedMnemonic('z'); f.add(l = new JLabel("Telephone:", SwingConstants.RIGHT)); l.setDisplayedMnemonic('t'); f.add(b = new JButton("Clear")); b.setMnemonic('l'); f.add(t = new JTextField(35)); t.setFocusAccelerator('n'); f.add(t = new JTextField(35)); t.setFocusAccelerator('h'); f.add(t = new JTextField(35)); t.setFocusAccelerator('c'); f.add(t = new JTextField(35)); t.setFocusAccelerator('s'); f.add(t = new JTextField(35)); t.setFocusAccelerator('z'); f.add(t = new JTextField(35)); t.setFocusAccelerator('t'); f.add(b = new JButton("OK")); b.setMnemonic('o'); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Command: " + e.getActionCommand()); int modifiers = e.getModifiers(); System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK)); System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK)); System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK)); System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK)); Object source = e.getSource(); if (source instanceof JComboBox) { JComboBox jb = (JComboBox) source; System.out.println("Combo: " + jb.getSelectedItem()); }//from w w w . ja v a 2 s. c o m } private boolean checkMod(int modifiers, int mask) { return ((modifiers & mask) == mask); } }; String flavors[] = { "Item 1", "Item 2", "Item 3" }; JComboBox jc = new JComboBox(flavors); jc.setMaximumRowCount(4); jc.setEditable(true); jc.addActionListener(listener); contentPane.add(jc, BorderLayout.NORTH); JButton b = new JButton("Button!"); b.addActionListener(listener); contentPane.add(b, BorderLayout.CENTER); JPanel panel = new JPanel(); JLabel label = new JLabel("Label 1: "); JTextField text = new JTextField("Type your text", 15); text.addActionListener(listener); label.setDisplayedMnemonic(KeyEvent.VK_1); label.setLabelFor(text); panel.add(label); panel.add(text); contentPane.add(panel, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); }
From source file:ActionTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Command: " + e.getActionCommand()); System.out.println("Modifiers: "); int modifiers = e.getModifiers(); System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK)); System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK)); System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK)); System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK)); Object source = e.getSource(); if (source instanceof JComboBox) { JComboBox jb = (JComboBox) source; System.out.println("Combo: " + jb.getSelectedItem()); }/* w w w .j av a 2s . c o m*/ } private boolean checkMod(int modifiers, int mask) { return ((modifiers & mask) == mask); } }; String flavors[] = { "Item 1", "Item 2", "Item 3" }; JComboBox jc = new JComboBox(flavors); jc.setMaximumRowCount(4); jc.setEditable(true); jc.addActionListener(listener); contentPane.add(jc, BorderLayout.NORTH); JButton b = new JButton("Button!"); b.addActionListener(listener); contentPane.add(b, BorderLayout.CENTER); JPanel panel = new JPanel(); JLabel label = new JLabel("Label 1: "); JTextField text = new JTextField("Type your text", 15); text.addActionListener(listener); label.setDisplayedMnemonic(KeyEvent.VK_1); label.setLabelFor(text); panel.add(label); panel.add(text); contentPane.add(panel, BorderLayout.SOUTH); frame.pack(); frame.show(); }
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);/*from w w w. ja v a 2 s . co 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: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. ja v a 2 s . c o m*/ }; 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:TextAcceleratorExample.java
public static void main(String[] args) { try {/* w ww.ja va2s . co m*/ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } JLabel l; JTextField t; JButton b; JFrame f = new JFrame("Text Accelerator Example"); Container cp = f.getContentPane(); cp.setLayout(new GridBagLayout()); cp.setBackground(UIManager.getColor("control")); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = GridBagConstraints.RELATIVE; c.gridwidth = 1; c.gridheight = 1; c.insets = new Insets(2, 2, 2, 2); c.anchor = GridBagConstraints.EAST; cp.add(l = new JLabel("Name:", SwingConstants.RIGHT), c); l.setDisplayedMnemonic('n'); cp.add(l = new JLabel("House/Street:", SwingConstants.RIGHT), c); l.setDisplayedMnemonic('h'); cp.add(l = new JLabel("City:", SwingConstants.RIGHT), c); l.setDisplayedMnemonic('c'); cp.add(l = new JLabel("State/County:", SwingConstants.RIGHT), c); l.setDisplayedMnemonic('s'); cp.add(l = new JLabel("Zip/Post code:", SwingConstants.RIGHT), c); l.setDisplayedMnemonic('z'); cp.add(l = new JLabel("Telephone:", SwingConstants.RIGHT), c); l.setDisplayedMnemonic('t'); cp.add(b = new JButton("Clear"), c); b.setMnemonic('l'); c.gridx = 1; c.gridy = 0; c.weightx = 1.0; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; cp.add(t = new JTextField(35), c); t.setFocusAccelerator('n'); c.gridx = 1; c.gridy = GridBagConstraints.RELATIVE; cp.add(t = new JTextField(35), c); t.setFocusAccelerator('h'); cp.add(t = new JTextField(35), c); t.setFocusAccelerator('c'); cp.add(t = new JTextField(35), c); t.setFocusAccelerator('s'); cp.add(t = new JTextField(35), c); t.setFocusAccelerator('z'); cp.add(t = new JTextField(35), c); t.setFocusAccelerator('t'); c.weightx = 0.0; c.fill = GridBagConstraints.NONE; cp.add(b = new JButton("OK"), c); b.setMnemonic('o'); f.pack(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); f.setVisible(true); }
From source file:LabelForComboBox.java
public LabelForComboBox() { JLabel lbl = new JLabel("Color:"); lbl.setDisplayedMnemonic('c'); add(lbl);// w w w. j av a 2 s . c o m add(Box.createHorizontalStrut(20)); JComboBox cbColor = new JComboBox(); cbColor.addItem("red"); cbColor.addItem("blue"); lbl.setLabelFor(cbColor); add(cbColor); }