Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Remove focus from JSpinner?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED] Remove focus from JSpinner?

    I'm working on program that has the arrow keys traverse a JList via a KeyboardEventDispatcher, and it works on a global level like I want. But I also have a spinner and it keeps changing value when I hit the up or down arrow. How do I get it to ignore keystrokes? I've tried setting focusable to false with no success.
    Last edited by hafunui; June 16th, 2011 at 04:17 PM.


  2. #2
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Remove focus from JSpinner?

    what platform are you using? I use Netbeans and sometimes use forms, on forms there is a properties that you can remove focus. other wise I think the code would go:
    jSpinner.setFocus(false);

    some platforms you can use ctrl + space bar to bring up a list. I have noticed that when using keystrokes that you need to remove all the focus on most components to get the desired results needed.

    hope this helps.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 601 Times in 516 Posts

    Default Re: Remove focus from JSpinner?

    Can you provide an SSCCE that demonstrates the problem?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Remove focus from JSpinner?

    Assuming you mean you're using a KeyEventDispatcher (not a KeyboardEventDispatcher), you need to be very careful how you use these. If you don't want the event dispatched to other components, return false from dispatchKeyEvent(). I would recommend controlling focus via setting the focus cycle root and using a custom FocusTraversalPolicy to set the default focus component appropriately. YMMV.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Remove focus from JSpinner?

    Ok, I've solved this one. Apparently setting focasable to false on the spinner alone isn't enough; I have to setFocusable(false) on every component of the JSpinner editor object too.

    Component[] comps = mySpinner.getEditor().getComponents();
    for (Component component : comps) {
        component.setFocusable(false);
    }

    Now it works as expected

Similar Threads

  1. Listening to JMenuItem keyboard focus
    By nik_meback in forum AWT / Java Swing
    Replies: 0
    Last Post: December 31st, 2010, 03:02 AM
  2. Help with JSpinner Problem
    By leoeroz in forum AWT / Java Swing
    Replies: 0
    Last Post: December 25th, 2010, 11:42 AM
  3. Maintain focus on JFrame
    By nik_meback in forum AWT / Java Swing
    Replies: 1
    Last Post: December 15th, 2010, 07:49 AM
  4. [SOLVED] JTextPane focus problem
    By LeonLanford in forum AWT / Java Swing
    Replies: 3
    Last Post: June 21st, 2010, 11:50 PM
  5. switch focus to another window
    By tuansoibk in forum AWT / Java Swing
    Replies: 1
    Last Post: November 13th, 2009, 01:02 PM