Results 1 to 11 of 11
  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default [SOLVED] Two swing questions

    1) How can I make it so that pressing "Enter" automatically "clicks" a button?
    2) I have a JList, but I want an event to take place only when it is double clicked; how can I do that?

    Thanks
    Last edited by carderne; 12-26-2008 at 01:26 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Thanks, got the thing sorted I think. Any idea about the double click?

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Speaking of buttons, here's something I just found that saves a lot of grief:
    [code]
    /*
    * If a button owns the focus, it becomes the default button
    */
    UIManager.put("Button.defaultButtonFollowsFocus",
    Boolean.TRUE);
    [/code}
    Without this, *any* Enter will activate your button, even when another button has the focus.

    As for part two, that's not easy. You could override the JList class and its method that actually fires the event. The method will still be called, but simply save the event. Then listen for mouse events on the control and fire the event if a click that generates the list event correlates to a double click. Pretty ugly...

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by carderne View Post
    Thanks, got the thing sorted I think. Any idea about the double click?
    Add MouseListner to the JList. Then using MouseEvent class, count number of clicks through getClickCount().

  6. #6
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Thanks, got double click sorted using getClickCount().

    For the pressing Enter thing. It doesn't actually need to 'click' a button; just do some simple things.

    I did this quite easily by using a key typed event and then using
    Java Code:
    if(evt.getKeyCode() == KeyEvent.VK_ENTER)
    This works fine, unless there are some text fields or such... Then the key typed event is never fired...

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The way to bind key press events you have to use getKeyCode() from the event, that's right. But what you are talking about text fields? Whatever the components you have in UI, if you get the correct code for the corresponding component, it should work.

  8. #8
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Ok, I have a main form, where I don't really want Enter to do anything. Then I have a JDialog with text input. I was adding a key typed event to the whole JDialog, but this event was never fired. I've just realised that perhaps I should rather be adding the key typed event to each text field? That isn't ideal, but unless someone suggests an alternative...

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have multiple components on a form. You can key press event as mentioned above. On that event you have to process the correct event. So I don't see any conflict there. Did you do that correctly?

  10. #10
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Yup, I've added the event to each component on the Dialog and that works perfectly now. Double clicking also good, so SOLVED.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 01:13 AM
  2. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  3. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM
  4. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM
  5. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-27-2007, 11:18 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
  •