• Post Reply Bookmark Topic Watch Topic
  • New Topic

Focus on JPanel

 
Sebastiaan Kortleven
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I've got a JFrame with several JPanels on it. These JPanels each hold several textfields, tables, labels,...
Now I would like to know which of the JPanels the user is currently working on so I can Highlight it (changing the border).
Add a focuslistener to the JPanel itself did nothing, should I add a focuslistener to EACH component on that JPanel (which will create a lot of overhead) or is there another way around this problem?
Thanks!
Sebastiaan Kortleven
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you would have to add a FocusListener to each component in the JPanel, but this is neither hard, nor does it create a lot of overhead. All you'd have to do is make a subclass of JPanel that implements FocusListener, and override the add() and remove() methods to add or remove itself as a FocusListener to the component that is being added or removed. If you have nested panels this still isn't a problem... you'll just have to create another subclass that is a FocusListener that propegates the event to it's parent.
 
Sebastiaan Kortleven
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. It's been really helpfull.
I've written a subclass of JPanel and wrote a new add method for it, adding the focuslistener to the component.
But now, switching between components on the same panel, will first call the focusLost on the first component, and then the focusGained on the second component. On each focusLost, I reset the panels border and repaint it and on each focusGained I do the same with a different border.
So now switching between components repaints the panel twice. Although this is not visible, it seems to me like a waste of CPU resources.. is this the way it should happen or am I'm doing something wrong?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's not visible (i.e. if it really doesn't do a repaint()) it's not really wasting resources... repaint() requests are queued up, and aren't like normal method calls. If the repaint does become visible, or there is a problem with wasting resources, here's one way to fix it -

Use some kind of manager class that each of the panels calls when they gain and lose focus. The manager only makes the decision to change the border when it receives the focus gained. If the focus gained and focus lost panels are the same panels, no border is changed. If they are different, then the border of each of the panels is changed to what it should be.
 
Sebastiaan Kortleven
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! You helped me a lot..
The problems are solved and if I ever get a resource problem due to the repaint issue, I'll build a some sort of border manageger.
Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!