List of usage examples for javax.swing KeyStroke getKeyStroke
public static KeyStroke getKeyStroke(int keyCode, int modifiers)
From source file:InsertAction.java
public static void main(String[] argv) { JTextField component = new JTextField(10); InsertAction insertSpaceAction = new InsertAction(); component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(new Character(' '), 0), "none"); component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("pressed SPACE"), insertSpaceAction.getValue(Action.NAME)); component.getActionMap().put(insertSpaceAction.getValue(Action.NAME), insertSpaceAction); JFrame f = new JFrame(); f.add(component);//from w ww . ja v a2s. co m f.setSize(300, 300); f.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { JFrame frame = new JFrame("Mnemonic/Accelerator Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Action printAction = new PrintHelloAction(); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu);// ww w. ja va 2s .c om JMenuItem menuItem = new JMenuItem("Print"); KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK); menuItem.setAccelerator(ctrlP); menuItem.addActionListener(printAction); menu.add(menuItem); JButton fileButton = new JButton("About"); fileButton.setMnemonic(KeyEvent.VK_A); fileButton.addActionListener(printAction); frame.setJMenuBar(menuBar); frame.add(fileButton, BorderLayout.SOUTH); frame.setSize(300, 100); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("Frame"); frame.add(Box.createRigidArea(new Dimension(400, 300))); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack();//from w w w. java 2 s.co m frame.setVisible(true); JDialog dialog = new JDialog(frame, "Dialog", true); int condition = JPanel.WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition); ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap(); String enter = "enter"; inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter); actionMap.put(enter, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { dialog.dispose(); } }); dialog.add(Box.createRigidArea(new Dimension(200, 200))); dialog.pack(); dialog.setLocationRelativeTo(frame); dialog.setVisible(true); }
From source file:PassiveTextField1.java
public static void main(String[] args) { JFrame f = new JFrame("Passive Text Field"); f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS)); final JTextField ptf = new JTextField(32); JTextField tf = new JTextField(32); JPanel p = new JPanel(); JButton b = new JButton("OK"); p.add(b);//w w w. ja v a 2s . co m f.getContentPane().add(ptf); f.getContentPane().add(tf); f.getContentPane().add(p); Keymap map = ptf.getKeymap(); // Gets the shared map KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); map.removeKeyStrokeBinding(key); ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Action event from a text field"); } }; ptf.addActionListener(l); tf.addActionListener(l); // Make the button the default button f.getRootPane().setDefaultButton(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Content of text field: <" + ptf.getText() + ">"); } }); f.pack(); f.setVisible(true); }
From source file:Submenu.java
public static void main(String[] args) { JFrame f = new JFrame(); JMenuBar menubar = new JMenuBar(); ImageIcon iconNew = new ImageIcon("new.png"); ImageIcon iconOpen = new ImageIcon("open.png"); ImageIcon iconSave = new ImageIcon("save.png"); ImageIcon iconClose = new ImageIcon("exit.png"); JMenu file = new JMenu("File"); JMenu imp = new JMenu("Import"); JMenuItem fileNew = new JMenuItem("New", iconNew); JMenuItem fileOpen = new JMenuItem("Open", iconOpen); JMenuItem fileSave = new JMenuItem("Save", iconSave); JMenuItem fileClose = new JMenuItem("Close", iconClose); file.setMnemonic(KeyEvent.VK_F); imp.setMnemonic(KeyEvent.VK_M); JMenuItem newsf = new JMenuItem("Import newsfeed list..."); JMenuItem bookm = new JMenuItem("Import bookmarks..."); JMenuItem mail = new JMenuItem("Import mail..."); imp.add(newsf);//from w w w .java2 s .com imp.add(bookm); imp.add(mail); fileNew.setMnemonic(KeyEvent.VK_N); fileNew.setMnemonic(KeyEvent.VK_O); fileSave.setMnemonic(KeyEvent.VK_S); fileClose.setMnemonic(KeyEvent.VK_C); fileClose.setToolTipText("Exit application"); fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK)); fileClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); file.add(fileNew); file.add(fileOpen); file.add(fileSave); file.addSeparator(); file.add(imp); file.addSeparator(); file.add(fileClose); menubar.add(file); f.setJMenuBar(menubar); f.setSize(360, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JDialog dialog;/*from w ww . j av a2 s. c o m*/ JList jlist; ActionListener otherListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("current"); } }; JButton okButton = new JButton("OK"); okButton.addActionListener(e -> close(true)); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(e -> close(false)); jlist = new JList(new String[] { "A", "B", "C", "D", "E", "F", "G" }); jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jlist.setVisibleRowCount(5); JScrollPane scroll = new JScrollPane(jlist); JPanel buttonsPanel = new JPanel(new FlowLayout()); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); JPanel content = new JPanel(new BorderLayout()); content.add(scroll, BorderLayout.CENTER); content.add(buttonsPanel, BorderLayout.SOUTH); dialog = new JDialog((Frame) null, true); dialog.setContentPane(content); dialog.pack(); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doSomething"); dialog.getRootPane().getActionMap().put("doSomething", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); dialog.setVisible(true); }
From source file:ColorAction.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("SeparateGUITest"); frame.setSize(300, 200);// w ww . j a va 2s .c om frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JPanel panel = new JPanel(); Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.blue, panel); Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), Color.yellow, panel); Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.red, panel); panel.add(new JButton(yellowAction)); panel.add(new JButton(blueAction)); panel.add(new JButton(redAction)); panel.registerKeyboardAction(yellowAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(blueAction, KeyStroke.getKeyStroke(KeyEvent.VK_B, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(redAction, KeyStroke.getKeyStroke(KeyEvent.VK_R, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); Container contentPane = frame.getContentPane(); contentPane.add(panel); JMenu m = new JMenu("Color"); m.add(yellowAction); m.add(blueAction); m.add(redAction); JMenuBar mbar = new JMenuBar(); mbar.add(m); frame.setJMenuBar(mbar); frame.show(); }
From source file:ToggleSample.java
public static void main(String args[]) { JFrame frame = new JFrame("JToggleButtonMenuItem Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar bar = new JMenuBar(); JMenu file = new JMenu("File"); file.setMnemonic('f'); JMenuItem newItem = new JMenuItem("New", 'N'); file.add(newItem);//from w w w. j a va 2s .c om JMenuItem openItem = new JMenuItem("Open", 'O'); file.add(openItem); JMenuItem closeItem = new JMenuItem("Close", 'C'); file.add(closeItem); file.addSeparator(); JMenuItem saveItem = new JMenuItem("Save", 'S'); file.add(saveItem); file.addSeparator(); JMenuItem exitItem = new JMenuItem("Exit", 'X'); file.add(exitItem); bar.add(file); JMenu edit = new JMenu("Edit"); JMenuItem cutItem = new JMenuItem("Cut", 'T'); cutItem.setAccelerator(KeyStroke.getKeyStroke('X', Event.CTRL_MASK)); edit.add(cutItem); JMenuItem copyItem = new JMenuItem("Copy", 'C'); copyItem.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK)); edit.add(copyItem); JMenuItem pasteItem = new JMenuItem("Paste", 'P'); pasteItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK)); pasteItem.setEnabled(false); edit.add(pasteItem); edit.addSeparator(); JMenuItem findItem = new JMenuItem("Find", 'F'); findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0)); edit.add(findItem); edit.setMnemonic('e'); Icon atIcon = new ImageIcon("at.gif"); JMenu findOptions = new JMenu("Options"); findOptions.setIcon(atIcon); findOptions.setMnemonic('O'); ButtonGroup directionGroup = new ButtonGroup(); JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true); findOptions.add(forward); directionGroup.add(forward); JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward"); findOptions.add(backward); directionGroup.add(backward); findOptions.addSeparator(); JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Insensitive"); findOptions.add(caseItem); edit.add(findOptions); JToggleButtonMenuItem toggleItem = new JToggleButtonMenuItem("Ballon Help"); toggleItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Selected"); } }); edit.add(toggleItem); bar.add(edit); frame.setJMenuBar(bar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:SwingToolBarSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Action Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Action showAction = new ShowAction(frame); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('f'); JMenuItem newMenuItem = new JMenuItem("New", 'N'); fileMenu.add(newMenuItem);/*from w w w.j a va 2 s . c om*/ JMenuItem openMenuItem = new JMenuItem("Open", 'O'); fileMenu.add(openMenuItem); JMenuItem closeMenuItem = new JMenuItem("Close", 'C'); fileMenu.add(closeMenuItem); fileMenu.addSeparator(); JMenuItem saveMenuItem = new JMenuItem("Save", 'S'); fileMenu.add(saveMenuItem); fileMenu.add(showAction); fileMenu.addSeparator(); JMenuItem exitMenuItem = new JMenuItem("Exit", 'X'); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); JMenu editMenu = new JMenu("Edit"); JMenuItem cutMenuItem = new JMenuItem("Cut", 'T'); KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK); cutMenuItem.setAccelerator(ctrlXKeyStroke); editMenu.add(cutMenuItem); JMenuItem copyMenuItem = new JMenuItem("Copy", 'C'); KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK); copyMenuItem.setAccelerator(ctrlCKeyStroke); editMenu.add(copyMenuItem); JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P'); KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK); pasteMenuItem.setAccelerator(ctrlVKeyStroke); pasteMenuItem.setEnabled(false); editMenu.add(pasteMenuItem); editMenu.addSeparator(); JMenuItem findMenuItem = new JMenuItem("Find", 'F'); KeyStroke f3KeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0); findMenuItem.setAccelerator(f3KeyStroke); editMenu.add(findMenuItem); editMenu.setMnemonic('e'); editMenu.add(showAction); menuBar.add(editMenu); frame.setJMenuBar(menuBar); frame.setSize(350, 250); frame.setVisible(true); }
From source file:MenuX.java
public static void main(String args[]) { ActionListener actionListener = new MenuActionListener(); MenuKeyListener menuKeyListener = new MyMenuKeyListener(); ChangeListener cListener = new MyChangeListener(); MenuListener menuListener = new MyMenuListener(); MenuSelectionManager manager = MenuSelectionManager.defaultManager(); manager.addChangeListener(cListener); JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar bar = new VerticalMenuBar(); // JMenuBar bar = new JMenuBar(); // File Menu, F - Mnemonic JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); file.addChangeListener(cListener);/*ww w .java 2s . c o m*/ file.addMenuListener(menuListener); file.addMenuKeyListener(menuKeyListener); JPopupMenu popupMenu = file.getPopupMenu(); popupMenu.setLayout(new GridLayout(3, 3)); bar.add(file); // File->New, N - Mnemonic JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N); newItem.addActionListener(actionListener); newItem.addChangeListener(cListener); newItem.addMenuKeyListener(menuKeyListener); file.add(newItem); // File->Open, O - Mnemonic JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O); openItem.addActionListener(actionListener); openItem.addChangeListener(cListener); openItem.addMenuKeyListener(menuKeyListener); file.add(openItem); // File->Close, C - Mnemonic JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C); closeItem.addActionListener(actionListener); closeItem.addChangeListener(cListener); closeItem.addMenuKeyListener(menuKeyListener); file.add(closeItem); // Separator file.addSeparator(); // File->Save, S - Mnemonic JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S); saveItem.addActionListener(actionListener); saveItem.addChangeListener(cListener); saveItem.addMenuKeyListener(menuKeyListener); file.add(saveItem); // Separator file.addSeparator(); // File->Exit, X - Mnemonic JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X); exitItem.addActionListener(actionListener); exitItem.addChangeListener(cListener); exitItem.addMenuKeyListener(menuKeyListener); file.add(exitItem); // Edit Menu, E - Mnemonic JMenu edit = new JMenu("Edit"); edit.setMnemonic(KeyEvent.VK_E); edit.addChangeListener(cListener); edit.addMenuListener(menuListener); edit.addMenuKeyListener(menuKeyListener); bar.add(edit); // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T); cutItem.addActionListener(actionListener); cutItem.addChangeListener(cListener); cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK)); cutItem.addMenuKeyListener(menuKeyListener); edit.add(cutItem); // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C); copyItem.addActionListener(actionListener); copyItem.addChangeListener(cListener); copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK)); copyItem.addMenuKeyListener(menuKeyListener); copyItem.setEnabled(false); edit.add(copyItem); // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P); pasteItem.addActionListener(actionListener); pasteItem.addChangeListener(cListener); pasteItem.addMenuKeyListener(menuKeyListener); pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK)); pasteItem.setEnabled(false); edit.add(pasteItem); // Separator edit.addSeparator(); // Edit->Find, F - Mnemonic, F3 - Accelerator JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F); findItem.addActionListener(actionListener); findItem.addChangeListener(cListener); findItem.addMenuKeyListener(menuKeyListener); findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0)); edit.add(findItem); // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File JMenu findOptions = new JMenu("Options"); findOptions.addChangeListener(cListener); findOptions.addMenuListener(menuListener); findOptions.addMenuKeyListener(menuKeyListener); Icon atIcon = new ImageIcon("at.gif"); findOptions.setIcon(atIcon); findOptions.setMnemonic(KeyEvent.VK_O); // ButtonGrou for radio buttons ButtonGroup directionGroup = new ButtonGroup(); // Edit->Options->Forward, F - Mnemonic, in group JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true); forward.addActionListener(actionListener); forward.addChangeListener(cListener); forward.addMenuKeyListener(menuKeyListener); forward.setMnemonic(KeyEvent.VK_F); findOptions.add(forward); directionGroup.add(forward); // Edit->Options->Backward, B - Mnemonic, in group JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward"); backward.addActionListener(actionListener); backward.addChangeListener(cListener); backward.addMenuKeyListener(menuKeyListener); backward.setMnemonic(KeyEvent.VK_B); findOptions.add(backward); directionGroup.add(backward); // Separator findOptions.addSeparator(); // Edit->Options->Case Sensitive, C - Mnemonic JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive"); caseItem.addActionListener(actionListener); caseItem.addChangeListener(cListener); caseItem.addMenuKeyListener(menuKeyListener); caseItem.setMnemonic(KeyEvent.VK_C); findOptions.add(caseItem); edit.add(findOptions); frame.setJMenuBar(bar); // frame.getContentPane().add(bar, BorderLayout.EAST); frame.setSize(350, 250); frame.setVisible(true); }