View Full Version : [JAVA] Domanda sui keyListener
alessia86
25-09-2009, 11:17
io do il focus ad un pannello in modo tale da ricevere gli eventi da tastiera..E fin qui va tutto bene..poi però ad un certo punto metto qst pannello che ha il focus
invisibile e rendo visibile un altro pannello..poi per far ripartire il pannello iniziale(quello con il focus) non creo un altra istanza..ma faccio partire un metodo di quel pannello
ed è tutto ok..in quanto mi visualizza il pannello..l'unico problema è che perdo il focus e non rileva più gli eventi da tastiera..Come mai?
Come dovrei fare?
N.B: I due pannelli sono aggiunti allo stesso frame
banryu79
25-09-2009, 12:35
Il pannello quando diventa invisibile perde il focus, quindi affinchè possa di nuovo diventare il "focus owner" è necessario:
1) rendere di nuovo visibile il pannello;
2) invocare il metodo requestFocusInWindows sul pannello.
Estratto dai Javadoc:
public boolean requestFocusInWindow()
Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window. This component must be displayable, focusable, visible and all of its ancestors (with the exception of the top-level Window) must be visible for the request to be granted. Every effort will be made to honor the request; however, in some cases it may be impossible to do so. Developers must never assume that this Component is the focus owner until this Component receives a FOCUS_GAINED event.
alessia86
26-09-2009, 09:07
ma io lo rende prima visibile e dopo gli do il focus..Posto il codice in cui rendo visibile per un tot di tempo un pannello:
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==timer){
pl.setVisible(true); //pl è il pannello che viene reso visibile solo per un tot di tempo
}
pl.setVisible(false);
stopAnimation(); // in stop animation facciamo solo timer.stop()
update(); // richiamo un metodo del pannelloG
pg.requestFocusInWindow(); // do il focus al pannelloG
pg.setVisible(true); //rendo visibile il pannelloG
}
ho provato anche ad aggiungere KeyListener al pannelloG .. ma niente..
Questo metodo viene implementato in PannelloG..
:help:
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.