Skip to content

Instantly share code, notes, and snippets.

@LuisFcoOrtiz
Last active February 9, 2017 11:16
Show Gist options
  • Save LuisFcoOrtiz/46ef1b2c20ff527bca21bb486e08f451 to your computer and use it in GitHub Desktop.
Save LuisFcoOrtiz/46ef1b2c20ff527bca21bb486e08f451 to your computer and use it in GitHub Desktop.
Ejemplo de Focus Listener (Visualización del foco en ventana Java) | Focus Listener Example (Show in a window where is the focus)
/*
* FocusEvent.java
*
* Copyright 2017 manrique <https:/github.com/luisFcoOrtiz>
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*border layout con zona norte y centro*/
public class FocusExample extends Frame {
//referencia de Variables
Label etiqueta;
TextField campoTexto;
TextArea textArea1, textArea2;
Button boton;
Choice seleccion;
FocusListener foco;
public FocusExample(String titulo) {
super(titulo);
setLayout(new BorderLayout());
addWindowListener(new ControlVentana()); //Controlador cerrar
Panel panelNorte = new Panel(); //Panel NORTE
Panel panelCenter = new Panel(); //Panel SUR
//objetos de ventana
campoTexto = new TextField(" ");
etiqueta = new Label("etiqueta");
seleccion = new Choice();
seleccion.add("item 1");
seleccion.add("item 2");
seleccion.add("item 3");
seleccion.add("item 4");
boton = new Button("boton");
//Controlar eventos
campoTexto.addFocusListener(new ControlFoco());
boton.addFocusListener(new ControlFoco());
seleccion.addFocusListener(new ControlFoco());
//Objetos del panel NORTE
panelNorte.add(campoTexto);
panelNorte.add(etiqueta);
panelNorte.add(seleccion);
panelNorte.add(boton);
add("North", panelNorte);
//Objetos del panel CENTRO
textArea1 = new TextArea();
textArea2 = new TextArea();
textArea2.setEditable(false); //No permite editar
panelCenter.add(textArea1);
panelCenter.add(textArea2);
add("Center", panelCenter);
}//Fin constructor
class ControlVentana extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}//Fin Extendida
//Controlador del boton
class ControlBoton implements ActionListener {
public void actionPerformed(ActionEvent action) {
System.out.println(action.getSource());
}
}//Fin control boton
class ControlFoco implements FocusListener {
public void focusGained(FocusEvent e) {
textArea2.append("Foco ganado " + e.getSource() + "\n");
}
public void focusLost(FocusEvent e) {
textArea2.append("Foco Perdido " + e.getSource() + "\n");
}
}//Fin de control de foco
}//Fin clase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment