jueves, 6 de julio de 2017

Controles Swing Listas

CONTROLES SWING
Las listas y cajas "combo" en Swing funcionan del mismo modo que lo hacían en el AWT, aunque tienen incrementada la funcionalidad a través de algunas funciones de conveniencia que se han incorporado. Por ejemplo, JList tiene un constructor al que se puede pasar un array de objetos String para que los presente.

Controles jList.
Introducción:
El JList clase es un componente que muestra una lista de objetos y permite al usuario seleccionar uno o más elementos. Un modelo separado, ListModel, mantiene el contenido de la lista.
·         A continuación se presentan los campos de la clase javax.swing.JList -
o   static int HORIZONTAL_WRAP - Indica un diseño "estilo periodístico" con las células que fluye horizontalmente y luego verticalmente.
o   static int VERTICAL - Indica una disposición vertical de las células, en una sola columna; el diseño predeterminado.
o   static int VERTICAL_WRAP - Indica un diseño "estilo periodístico" con las células que fluyen verticalmente y luego horizontalmente.

Controles jComboBox.
Introducción:
La claseJComboBox es un componente que combina un botón o campo editable y una lista desplegable.

Propiedades y eventos jComboBox
Eventos de jList
Los eventos list selection ocurren cuando una seleccion de una lista o una tabla o a cambiado o acaba de cambiar. Estos eventos son disparados por un objeto que implementa el interface ListSelectionModel. Para obtener un objeto de este tipo se usa el método getSelectionModel.
Para detectar eventos list seleccion se registra un oyente con el objeto ListSelectionModel apropiado. La clase JList también ofrece la opción de registrar un oyente de la propia lista, en vez de directamente del ListSelectionModel.

Métodos de Evento List Selection

El interface ListSelectionListener tiene sólo un método por eso no tiene su correspondiente clase adaptador. Aquí está el método
void valueChanged(ListSelectionEvent)
Llamado cuando cambia la selección del componente escuchado, también justo después de que la selección haya cambiado.
Evento jcomboBox
El control JComboBox permite seleccionar un String de una lista.
Para inicializar los String que contendrá el JComboBox debemos llamar al método addItem tantas veces como elementos queremos cargar.
Un evento muy útil con este control es cuando el operador selecciona un Item de la lista. Para capturar la selección de un item debemos implementar la interface ItemListener que contiene un método llamada itemStateChanged.

EJEMPLOS:
1)
public class VentanaConfirmacion extends JDialog{
private Container contenedor;
JLabel labelTitulo;
  
public VentanaConfirmacion(VentanaPrincipal miVentanaPrincipal, boolean modal){
 /**Al llamar al constructor super(), le enviamos el
  * JFrame Padre y la propiedad booleana que determina
  * que es hija*/
  super(miVentanaPrincipal, modal);
  contenedor=getContentPane();
  contenedor.setLayout(null);
  //Asigna un titulo a la barra de titulo
  setTitle("CoDejaVu : JDialog VentanaConfirmacion");
   
  labelTitulo= new JLabel();
  labelTitulo.setText("VENTANA DE CONFIRMACION");
  labelTitulo.setBounds(20, 20, 180, 23);
  contenedor.add(labelTitulo);
  //tamaño de la ventana
  setSize(350,150);
  //pone la ventana en el Centro de la pantalla
  setLocationRelativeTo(null);
 }
}















SUMMARY:

                                                    SWING CONTROLS

Swing "combo" lists and boxes work the same way they did in the AWT, although they have increased functionality through some convenience functions that have been incorporated. For example, JList has a constructor to which you can pass an array of String objects to present them.
JList controls.
Introduction:
The JList class is a component that displays a list of objects and allows the user to select one or more elements. A separate model, ListModel, maintains the contents of the list.
· Below are the fields of the class javax.swing.JList -
Or static int HORIZONTAL_WRAP - Indicates a "journalistic style" layout with cells flowing horizontally and then vertically.
Or static int VERTICAL - Indicates a vertical arrangement of cells, in a single column; The default layout.
Or static int VERTICAL_WRAP - Indicates a "journalistic style" layout with cells that flow vertically and then horizontally.
JComboBox controls.
Introduction:
The JComboBox class is a component that combines an editable button or field and a drop-down list.
JComboBox Properties and Events
JList Events
List selection events occur when a selection from a list or table is either changed or just changed. These events are triggered by an object that implements the ListSelectionModel interface. To obtain an object of this type, the getSelectionModel method is used.
To detect list events select a listener with the appropriate ListSelectionModel object. The JList class also offers the option of registering a listener from the list itself, rather than directly from the ListSelectionModel.
Event Methods List Selection
The ListSelectionListener interface has only one method so it does not have its corresponding adapter class. Here is the method
Void valueChanged (ListSelectionEvent)
Called when the selected component is changed, also just after the selection has changed.
JcomboBox Event
The JComboBox control allows you to select a String from a list.
To initialize the String that will contain the JComboBox we must call the addItem method as many times as we want to load.
A very useful event with this control is when the operator selects an Item from the list. To capture the selection of an item we must implement the ItemListener interface that contains a method called itemStateChanged.


No hay comentarios:

Publicar un comentario