• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I keep focus on the component pressed until I pressed other component????

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends:
I met an urgent problem in my project,How can I keep focus on the component pressed until I pressed other component?? ie. here in following code, I pressed jlabel jl1, then jl1 was highlighted and showed red border line, when i move out of this jl1 to other place, but I pressed nothing, then the focus on jl1 was lost, and its red border line disappear,
I really hope once I pressed jl1, its borderline becomes red, then I move to other place but I did not press any place or components on this JPanel, this jl1's focus will not be lost and its rede borderline keeps red until I press any place or any component,
How can I keep jl1's current focus??
Thanks so much



[ August 07, 2007: Message edited by: Michelle Wang ]
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you might be getting confused between a component having focus and what colour the border is. The clicked on component has a red border because your mousePressed() method colours it red. Once the mouse moves outside of the component the border's colour is being changed by your mouseMoved() method. This does not mean it has lost focus, it's just that your code has changed the colour of the border.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Tony:
I tested your idea, I add actionListener to trace Focus, When I press jlabel1, then press Focus Button, jlabel1's focus is lost, you can see it in println, see code below.
I hope when I press Button, still get same jlabel I pressed before, not others.
Thanks
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some guru help solve problem in my last post??
Thanks a lot
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michelle Wang:
I tested your idea, I add actionListener to trace Focus, When I press jlabel1, then press Focus Button, jlabel1's focus is lost,

Well if you click on another component then the focus will transfer to that component and the focus will be lost from the original component that's how the focus system works, but this is not the problem you described in your initial post where you said you weren't clicking on any other components. Have I misunderstood your problem?
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that at begining I mis-present my question because I am not very clear the focus' meaning, but the last post is what I want, if possibEl, can you help solve this problEm in my last post instead of initial onE??
Thanks
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK What is Focus?
First of all when talking of Focus we generally mean keyboard focus. A component is said to have keyboard focus when keyboard key presses are sent to that component. Of course not all key presses make sense to all components, for example pressing the letter 'A' will cause a letter 'A' to be added to a JTextField if it has focus but probably won't do anything if a JButton has focus.

As for solving your problem there are a number of ways of doing it but before you do anything I suggest you read the Sun Focus System Tutorial.

One approach is as follows: Each component has a requestFocusInWindow() method so this can be called to request the focus is set to this component (Note: the request is only a request and is not guaranteed to succeed), so if you know which component has just lost focus when the Focus Button is pressed you can request it is set back to the original component. The FocusEvent sent to the new focus owner has a getOppositeComponent() method so you can find out which component (if any) has lost focus. I'm not 100% sure if you can call requestFocus() on this component from within the FocusListener (I think you can so try it and see). If it doesn't work use EventQueue.invokeLater() to perform the change outside of the current event chain.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Tony,
I carefully read your comments and tried several times, I think success, see code below, but only small problem,
when I click on jl2, for example, then
[1]. if I carefully move around and did not move across any other jlabels such as jl and jl2, then I press "Focus" button, I can successfully get jl2's background color changed
[2]. if I carefully move stright and DO move across any other jlabels such as jl and jl2, then I press "Focus" button, I CANNOT get jl2's background color changed, ie, jl2 still keeps pink color.

Can you help solve this problem, i mean even I move my mouse cursor at my will, never mind across or not across any other components such as jl and jl1 etc, after I press "Focus" button, I can also change jl2's background color to blue.

Thanks a lot!!

see code below


[ August 16, 2007: Message edited by: Michelle Wang ]
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem that the selectedComponent variable is being set by the mouseMoved() method rather than the mousePressed() method so the focus button is setting the background colour of the last button that was passed over rather than the last button that was clicked on.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony, I got success, really appreciate
[ August 16, 2007: Message edited by: Michelle Wang ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic