import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PruebaFocoVentana {
public static void main( String args[] ){
JFrame ventana = new FormaPrueba();
}
}
class FormaPrueba extends JFrame{
public FormaPrueba(){
super( "Asignación del foco a una ventana" );
setSize( 400, 300 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setResizable( false );
setVisible( true );
iniciarGUI();
}
private void iniciarGUI(){
int res;
elEscritorio = new JDesktopPane();
getContentPane().add( elEscritorio );
res = JOptionPane.showConfirmDialog( this, "Abrir forma interna", "Aviso", JOptionPane.YES_NO_OPTION );
FormaInterna formainterna=new FormaInterna();
if( res == JOptionPane.YES_OPTION )
elEscritorio.add(formainterna);
try
{
formainterna.setSelected(true);
}
catch (Exception e) { }
}
private JDesktopPane elEscritorio;
}
class FormaInterna extends JInternalFrame{
public FormaInterna(){
super( "Forma Interna", false, true, false, true );
iniciarGUI();
setVisible( true );
pack();
requestFocus();
}
private void iniciarGUI(){
contenedor = getContentPane();
contenedor.setLayout( new FlowLayout() );
campo = new JTextField( 10 );
contenedor.add( campo );
campo.requestFocus();
}
public Dimension getPreferredSize(){
return new Dimension( 350, 250 );
}
private Container contenedor;
private JTextField campo;
}