Results 1 to 3 of 3
  1. #1
    kc3
    kc3 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Swing VK_X and VK_C Issue

    I have an issue where for some reason the VK_X and VK_C KeyEvents do not work at all, I can change them to anything else and it works, I've tried this on two other PCs as well... It just acts like Ctrl-C and X are in use or something, the combination is displayed in the menu so I imagine I typed it in right. This is my code

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class ExProject implements ActionListener {
    	JLabel selectedItem;
    	JTextArea textContents;
    	
    	ExProject() {
    		JFrame jfrm = new JFrame("Experimental GUI");
    		jfrm.setSize(215, 350);
    		jfrm.setLocation(350, 350);
    		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		/*** Start Menus ***/
    		
    		JMenuBar jmb = new JMenuBar();
    		
    		JMenu fileMenu = new JMenu("File");
    		JMenu editMenu = new JMenu("Edit");
    		
    		JMenuItem newFile = new JMenuItem("New");
    		JMenuItem openFile = new JMenuItem("Open");
    		JMenuItem exitProg = new JMenuItem("Exit");
    		
    		newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
    		openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
    		exitProg.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
    		
    		newFile.addActionListener(this);
    		openFile.addActionListener(this);
    		exitProg.addActionListener(this);
    		
    		fileMenu.add(newFile);
    		fileMenu.add(openFile);
    		fileMenu.addSeparator();
    		fileMenu.add(exitProg);
    		
    		JMenuItem cutFrom = new JMenuItem("Cut");
    		JMenuItem copyFrom = new JMenuItem("Copy");
    		JMenuItem pasteTo = new JMenuItem("Paste");
    		
    		cutFrom.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));      /***** PROBLEM LINE!!! *****/
    		copyFrom.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));     /***** PROBLEM LINE!!! *****/
    		pasteTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));
    		
    		cutFrom.addActionListener(this);
    		copyFrom.addActionListener(this);
    		pasteTo.addActionListener(this);
    		
    		editMenu.add(cutFrom);
    		editMenu.add(copyFrom);
    		editMenu.add(pasteTo);
    		
    		jmb.add(fileMenu);
    		jmb.add(editMenu);
    		
    		/*** End Menus ***/
    		
    		/*** Start Main Contents ***/
     
    		selectedItem = new JLabel("Selected Items Log");
    		jfrm.add(selectedItem, BorderLayout.NORTH);
    		
    		textContents = new JTextArea();
    		textContents.setEditable(false);
    		JScrollPane windowContents = new JScrollPane(textContents);
    		jfrm.add(windowContents, BorderLayout.CENTER);
    		
    		/*** End Main Contents ***/
    		
    		jfrm.setJMenuBar(jmb);
    		jfrm.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent ae) {
    		String cmd = ae.getActionCommand();
    		String contents = textContents.getText();
    		if (cmd.equals("Exit")) {
    			System.exit(0);
    		}  else {
    			textContents.setText(contents +
    					"Selected Item - " + cmd + "\n");
    		}
    	}
    	
    	public static void main(String args[]) {
    		new ExProject();
    	}
    }


    I've tried changing the keys to other ones and it works, also keeping the KeyEvent but changing the InputEvent to ALT_MASK works as well, all other accelerators work in this code, am I just missing something?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Swing VK_X and VK_C Issue

    Are those events are being captured/consumed by a Text component, for instance the JTextArea? They have by default their own action map and accelerators for copy/cut/paste.

  3. #3
    kc3
    kc3 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Swing VK_X and VK_C Issue

    [edit] nvm :) that helped, didn't realize that the accelerators were default for JTextArea, thanks!
    Last edited by kc3; 04-11-2012 at 10:11 PM.

Similar Threads

  1. Drawing issue with Applet/Swing
    By The_Capn in forum Java Applets
    Replies: 2
    Last Post: 06-18-2011, 05:30 AM
  2. AWT/Swing Mixing Still and issue on MAC OSX?
    By Confused in forum AWT / Swing
    Replies: 2
    Last Post: 02-21-2010, 08:34 PM
  3. Swing Timers Issue.
    By killpoppop in forum AWT / Swing
    Replies: 4
    Last Post: 03-09-2009, 11:17 PM
  4. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 12:25 PM
  5. Java1.5 Swing Issue
    By naveenbk in forum AWT / Swing
    Replies: 1
    Last Post: 11-23-2008, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •