Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: SWING keybindings

  1. #1
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default SWING keybindings

    Hey, I have a JTextArea and I want it to submit text when I press Enter on my keyboard.
    On default the JTextArea puts a new line in the JTextarea but I want to disable this.
    I have to overwrite the default keybindings in JTextArea but I can't get it to work..

    Ive read the docs on oracle with the inputmap and actionmap and I understand the basics.
    My code below works but I know SWING is designed for keybindings, not keylisteners. I could use a simple example of the JTextArea overwriting the default keybindings.
    Hope somebody can help me

    Java Code:
      sendwindow.addKeyListener(new KeyListener(){
            	  @Override
            	public void keyPressed(KeyEvent e) {
            		  if(e.getKeyCode() == KeyEvent.VK_ENTER)
            		    {
            				try{
            					writeLine("PRIVMSG #asdf :"+sendwindow.getText());
            					chatwindow.append("<"+nick+">  "+sendwindow.getText()+"\n");
            					sendwindow.setText("");
            				}catch(Exception ex){
            					ex.printStackTrace();
            				}
            		        
            		    }
            	}
            	  @Override
            	public void keyReleased(KeyEvent e) {	
            	}
            	  @Override
            	public void keyTyped(KeyEvent e) {
    
            	}
              });

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: SWING keybindings

    I can't tell you how to do this by overriding the key bindings wthout some research on my part. But I know from experience you can do it via a document listener which can be used to monitor the keystrokes.

    Regards,
    Jim
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: SWING keybindings

    Well, I did the document listener, the key listener and the key bindings. They all work as advertised. The bindings are probably the best way to go since it intercepts the enter key and doesn't put it in the text area. Check out key bindings in the tutorials in my signature (that's how I figured it out).

    Regards,
    Jim
    Last edited by jim829; 11-25-2013 at 06:53 PM. Reason: s/figure/figured
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: SWING keybindings

    I understand the basics.
    Then post your Short, Self Contained, Correct Example that shows what you have attempted to do and we can help out with the problems. All you need to do is use examples from the tutorial or search the forum for examples to get you started.

  5. #5
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: SWING keybindings

    Quote Originally Posted by jim829 View Post
    Well, I did the document listener, the key listener and the key bindings. They all work as advertised. The bindings are probably the best way to go since it intercepts the enter key and doesn't put it in the text area. Check out key bindings in the tutorials in my signature (that's how I figured it out).

    Regards,
    Jim
    Thanks jim, could you link me a sample code were you link a JTextArea (not textfield) to an inputmap, outputmap and one of the listeners since actionlistener is not working with JTextAreas

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: SWING keybindings

    I suggest first you try it yourself by reading this How to Use Key Bindings. It gives an example of how to setup the inputMap, actionMap, and instantiate an action. The string you want to use is "ENTER" for the enter command. That is derived from the writeup in the KeyStroke (Java Platform SE 7 ) documentation.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default Re: SWING keybindings

    Quote Originally Posted by JavaJimme View Post
    Thanks jim, could you link me a sample code were you link a JTextArea (not textfield) to an inputmap, outputmap and one of the listeners since actionlistener is not working with JTextAreas
    Google: java jtextarea trap enter
    second result: java - JTextArea new line on shift + enter - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Unresponsive keybindings
    By Insomniac.. in forum AWT / Swing
    Replies: 5
    Last Post: 07-21-2013, 06:50 PM
  2. Swing vs AWT
    By Army in forum New To Java
    Replies: 26
    Last Post: 06-20-2012, 01:58 PM
  3. keybindings
    By jsobel in forum New To Java
    Replies: 8
    Last Post: 04-03-2012, 04:41 PM
  4. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 07:28 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Tags for this Thread

Posting Permissions

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