Está en la página 1de 0

1

Java Avanzado
Java Foundation Classes
(Swing)
Copyright (c) 2004
J os M. Ordax
Copyright
Este documento puede ser distribuido solo bajo los
trminos y condiciones de la Licencia de
Documentacin de javaHispano v1.0 o posterior.
La ltima versin se encuentra en
http://www.javahispano.org/licencias/
2
Se creo como una extensin de la AWT aadiendo
las siguientes caractersticas:
Componentes Swing.
Java Foundation Classes
Se trata de un conjunto de clases para mejorar el
soporte al desarrollo de GUIs.
Soporte de Look & Feel.
API de accesibilidad.
J ava 2D API.
Soporte de Drag & Drop.
Swing vs. AWT
Swing es el conjunto de nuevos componentes
visuales.
Habitualmente tambin se usa como trmino
genrico para referirse a las J FC.
Su diferencia mas importante con la AWT es que
los componentes son lightweight.
Para diferenciar los componentes Swing de los
AWT, sus nombres estn precedidos por una J .
Todas las clases Swing se encuentran en el
paquete javax.swing.*
3
Swing vs. AWT (cont.)
Nunca debemos mezclar componentes Swing con
componentes AWT en una misma aplicacin:
Lightweight vs. Heavyweight.
Swing sigue trabajando con los conceptos de la
AWT:
Contenedores.
Componentes.
LayoutManagers.
Eventos.
Jerarqua de clases
4
Jerarqua de clases (cont.)
Jerarqua de clases (cont.)
5
Jerarqua de clases (cont.)
javax.swing.JComponent
Hereda de la clase java.awt.Container.
Ayudas emergentes.
Bordes.
Gestin del Look & Feel.
Gestin de teclas asociadas.
Soporte de Drag & Drop.
Se trata de una clase abstracta que implementa
toda la funcionalidad bsica de las clases visuales.
Gestin de la accesibilidad.
6
Migrando de AWT a Swing
Eliminar todos los import de paquetes java.awt.*
Frame ->J Frame, Button ->J Button, etc
Importar el paquete javax.swing.*
Cambiar cada componente AWT por el Swing ms
parecido:
Ojo! No se pueden aadir componentes o
establecer LayoutManagers directamente sobre
J Window, J Frame, J Dialog o J Applet.
Hay que hacerlo sobre el Container que devuelve el
mtodo: public Container getContentPane();
5.0
A partir de J 2SE 5.0
ya no es necesario.
AWT:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
public class Test
{
public static void main(String[] args)
{
Frame f =new Frame();
f.setTitle("Test de migracin");
f.setSize(200,150);
f.setLayout(new FlowLayout());
Button b =new Button("Ok");
f.add(b);
f.setVisible(true);
}
}
Migrando de AWT a Swing
7
Swing:
import java.awt.FlowLayout;
import javax.swing.J Button;
import javax.swing.J Frame;
public class Test
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("Test de migracin");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J Button b =new J Button("Ok");
f.getContentPane().add(b);
f.setVisible(true);
}
}
Migrando de AWT a Swing
javax.swing.JFrame
import javax.swing.J Frame;
public class J FrameTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J FrameTest");
f.setSize(200,150);
f.setVisible(true);
}
}
8
javax.swing.JInternalFrame
import javax.swing.*;
public class J InternalFrameTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J InternalFrameTest");
f.getContentPane().setLayout(null);
f.setSize(230,200);
J InternalFrame f1 =new J InternalFrame("InternalFrame 1");
f1.setBounds(10,10,150,100);
f1.setVisible(true);
J InternalFrame f2 =new J InternalFrame("InternalFrame 2");
f2.setBounds(50,50,150,100);
f2.setVisible(true);
f.getContentPane().add(f2);
f.getContentPane().add(f1);
f.setVisible(true);
}
}
javax.swing.JButton
import java.awt.FlowLayout;
import javax.swing.J Button;
import javax.swing.J Frame;
public class J ButtonTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ButtonTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J Button b =new J Button("Ok");
f.getContentPane().add(b);
f.setVisible(true);
}
}
9
javax.swing.JCheckBox
import java.awt.FlowLayout;
import javax.swing.J CheckBox;
import javax.swing.J Frame;
public class J CheckboxTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J CheckBoxTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J CheckBox c =new J CheckBox("Mayor de 18 aos");
f.getContentPane().add(c);
f.setVisible(true);
}
}
javax.swing.JRadioButton
import java.awt.FlowLayout;
import javax.swing.*;
import javax.swing.J Frame;
import javax.swing.J RadioButton;
public class J RadioButtonTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J RadioButtonTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
ButtonGroup bg =new ButtonGroup();
J RadioButton c1 =new J RadioButton("Hombre",true);
bg.add(c1);
J RadioButton c2 =new J RadioButton("Mujer",false);
bg.add(c2);
f.getContentPane().add(c1);
f.getContentPane().add(c2);
f.setVisible(true);
}
}
10
javax.swing.JToggleButton
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.J Frame;
import javax.swing.J ToggleButton;
public class J ToggleButtonTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ToggleButtonTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
ButtonGroup bg =new ButtonGroup();
J ToggleButton b1 =new J ToggleButton("Hombre",true);
bg.add(b1);
J ToggleButton b2 =new J ToggleButton("Mujer",false);
bg.add(b2);
f.getContentPane().add(b1);
f.getContentPane().add(b2);
f.setVisible(true);
}
}
javax.swing.JComboBox
import java.awt.FlowLayout;
import javax.swing.J ComboBox;
import javax.swing.J Frame;
public class J ComboBoxTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ComboBoxTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
String[] list ={"Rojo","Amarillo","Blanco"};
J ComboBox c =new J ComboBox(list);
f.getContentPane().add(c);
f.setVisible(true);
}
}
11
javax.swing.JLabel
import java.awt.FlowLayout;
import javax.swing.*;
public class J LabelTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J LabelTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J Label l1 =new J Label("Una etiqueta");
J Label l2 =new J Label();
l2.setText("Otra etiqueta");
f.getContentPane().add(l1);
f.getContentPane().add(l2);
f.setVisible(true);
}
}
javax.swing.JList
import java.awt.FlowLayout;
import javax.swing.J Frame;
import javax.swing.J List;
public class J ListTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ListTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
String[] list ={"Primero","Segundo","Tercero","Cuarto"};
J List l =new J List(list);
f.getContentPane().add(l);
f.setVisible(true);
}
}
12
javax.swing.JMenuBar
import javax.swing.*;
public class J MenuBarTest
{
public static void main(String[] args)
{
J Frame f =new J Frame("J MenuBarTest");
f.setSize(200,150);
J MenuBar mb =new J MenuBar();
J Menu m1 =new J Menu("Menu 1");
m1.add(new J MenuItem("Opcin 1"));
m1.add(new J MenuItem("Opcin 2"));
J Menu m2 =new J Menu("Menu 2");
m2.add(new J CheckBoxMenuItem("Opcin 1"));
m2.add(new J CheckBoxMenuItem("Opcin 2", true));
m2.addSeparator();
m2.add(new J RadioButtonMenuItem("Opcin 3", true));
mb.add(m1);
mb.add(m2);
f.setJ MenuBar(mb);
f.setVisible(true);
}
}
import java.awt.FlowLayout;
import javax.swing.J Frame;
import javax.swing.J ScrollBar;
public class J ScrollBarTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ScrollBarTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J ScrollBar sb =new J ScrollBar(J ScrollBar.HORIZONTAL,0,5,-100,100);
f.getContentPane().add(sb);
f.setVisible(true);
}
}
javax.swing.JScrollBar
13
import java.awt.FlowLayout;
import javax.swing.J Frame;
import javax.swing.J TextField;
public class J TextFieldTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J TextFieldTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J TextField tf =new J TextField("Escribe aqu...");
f.getContentPane().add(tf);
f.setVisible(true);
}
}
javax.swing.JTextField
import java.awt.FlowLayout;
import javax.swing.J Frame;
import javax.swing.J PasswordField;
public class J PasswordFieldTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J PasswordFieldTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J PasswordField pf =new J PasswordField("chemi");
f.getContentPane().add(pf);
f.setVisible(true);
}
}
javax.swing.JPasswordField
14
javax.swing.JTextArea
import java.awt.FlowLayout;
import javax.swing.J Frame;
import javax.swing.J TextArea;
public class J TextAreaTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J TextAreaTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J TextArea ta =new J TextArea("Escribe aqu...",5,15);
f.getContentPane().add(ta);
f.setVisible(true);
}
}
javax.swing.JScrollPane
import java.awt.FlowLayout;
import javax.swing.J Frame;
import javax.swing.J ScrollPane;
import javax.swing.J TextArea;
public class J ScrollPaneTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ScrollPaneTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J TextArea ta =new J TextArea("Escribe aqu...",5,5);
J ScrollPane p =new J ScrollPane(ta);
f.getContentPane().add(p);
f.setVisible(true);
}
}
15
javax.swing.JColorChooser
import java.awt.Color;
import javax.swing.J ColorChooser;
import javax.swing.J Frame;
public class J ColorChooserTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ColorChooserTest");
f.setSize(200,150);
f.setVisible(true);
Color c =J ColorChooser.showDialog(f,"Seleccione un color",Color.RED);
System.out.println("El color seleccionado es: " +c);
}
}
javax.swing.JFileChooser
import javax.swing.J FileChooser;
import javax.swing.J Frame;
public class Test
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J FileChooserTest");
f.setSize(200,150);
f.setVisible(true);
J FileChooser fc =new J FileChooser();
int op =fc.showOpenDialog(f);
if(op ==J FileChooser.APPROVE_OPTION)
System.out.println(fc.getSelectedFile());
}
}
16
import java.awt.FlowLayout;
import javax.swing.J Frame;
import javax.swing.J Slider;
public class J SliderTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J SliderTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J Slider s =new J Slider(J Slider.HORIZONTAL,0,30,15);
s.setMajorTickSpacing(10);
s.setMinorTickSpacing(1);
s.setPaintTicks(true);
s.setPaintLabels(true);
f.getContentPane().add(s);
f.setVisible(true);
}
}
javax.swing.JSlider
import java.awt.FlowLayout;
import javax.swing.*;
public class J SpinnerTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J SpinnerTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
String[] dias ={"L","M","X","J ","V","S","D };
SpinnerListModel modelo =new SpinnerListModel(dias);
J Spinner s =new J Spinner(modelo);
f.getContentPane().add(s);
f.setVisible(true);
}
}
javax.swing.JSpinner
17
javax.swing.JTable
import java.awt.FlowLayout;
import javax.swing.*;
public class J TableTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J TableTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
Object[][] datos =
{
{"Nombre1", "Apellido1", new Integer(911234567) },
{"Nombre2", "Apellido2", new Integer(917463527) },
{"Nombre3", "Apellido3", new Integer(912494735) },
{"Nombre4", "Apellido4", new Integer(912387448) },
};
String[] columnas ={"Nombre", "Apellidos", "Tfno"};
J Table t =new J Table(datos, columnas);
J ScrollPane sp =new J ScrollPane(t);
f.getContentPane().add(sp);
f.setVisible(true);
}
}
javax.swing.JTree
import java.awt.FlowLayout;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class J TreeTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J TreeTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
DefaultMutableTreeNode titulo =new DefaultMutableTreeNode("Programacin en J ava");
DefaultMutableTreeNode capitulo =new DefaultMutableTreeNode("AWT");
titulo.add(capitulo);
capitulo =new DefaultMutableTreeNode("J FC");
titulo.add(capitulo);
J Tree tree =new J Tree(titulo);
J ScrollPane sp =new J ScrollPane(tree);
f.getContentPane().add(sp);
f.setVisible(true);
}
}
18
java.swing.JToolTip
import java.awt.FlowLayout;
import javax.swing.J Button;
import javax.swing.J Frame;
public class J ToolTipTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ToolTipTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J Button b =new J Button("Ok");
b.setToolTipText("Pulsar Ok");
f.getContentPane().add(b);
f.setVisible(true);
}
}
javax.swing.JDialog
import javax.swing.J Dialog;
import javax.swing.J Frame;
public class J DialogTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J FrameTest");
f.setSize(200,150);
f.setVisible(true);
J Dialog d =new J Dialog(f);
d.setTitle("J DialogTest");
d.setBounds(50,50,70,50);
d.setVisible(true);
}
}
19
javax.swing.JOptionPane
import javax.swing.*;
public class J OptionPaneTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J OptionPaneTest");
f.setSize(200,150);
f.setVisible(true);
J OptionPane.showMessageDialog(f, "MessageDialog");
Object[] opciones ={"Aceptar","Cancelar" };
int i =J OptionPane.showOptionDialog(f,"OptionDialog",
"Option",J OptionPane.YES_NO_OPTION,
J OptionPane.QUESTION_MESSAGE,null,
opciones,opciones[0]);
i =J OptionPane.showConfirmDialog(f,"ConfirmDialog");
String s =J OptionPane.showInputDialog(f,"InputDialog");
}
}
javax.swing.JTabbedPane
import javax.swing.*;
import javax.swing.J Panel;
import javax.swing.J TabbedPane;
public class J TabbedPaneTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J TabbedPaneTest");
f.setSize(200,150);
J TabbedPane tabbedPane =new J TabbedPane();
J Panel panel1 =new J Panel();
tabbedPane.addTab("Pestaa 1", panel1);
J Panel panel2 =new J Panel();
tabbedPane.addTab("Pestaa 2", panel2);
f.getContentPane().add(tabbedPane);
f.setVisible(true);
}
}
20
javax.swing.ImageIcon
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.J Frame;
import javax.swing.J Label;
public class ImageIconTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("ImageIconTest");
f.setSize(200,150);
f.getContentPane().setLayout(new FlowLayout());
J Label l =new J Label();
l.setIcon(new ImageIcon("duke.gif")); // Soporta formatos GIF, J PG y PNG.
f.getContentPane().add(l);
f.setVisible(true);
}
}
javax.swing.JToolBar
import java.awt.*;
import javax.swing.*;
public class J ToolBarTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J ToolBarTest");
f.setSize(200,150);
J ToolBar tb =new J ToolBar();
J Button b =new J Button(new ImageIcon("New24.gif"));
tb.add(b);
b =new J Button(new ImageIcon("Open24.gif"));
tb.add(b);
b =new J Button(new ImageIcon("Save24.gif"));
tb.add(b);
b =new J Button(new ImageIcon("Print24.gif"));
tb.add(b);
f.getContentPane().add(tb,BorderLayout.NORTH);
f.setVisible(true);
}
}
Nota:
Galera de iconos para el J ava Look & Feel Metal:
http://java.sun.com/developer/techDocs/hi/repository/
21
javax.swing.JSplitPane
import java.awt.Dimension;
import javax.swing.*;
public class J SplitPaneTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("J SplitPaneTest");
f.setSize(275,252);
J Label l1 =new J Label(newImageIcon("argh.jpg"));
l1.setMinimumSize(new Dimension(20, 20));
J Label l2 =new J Label(new ImageIcon("comic.jpg"));
l2.setMinimumSize(new Dimension(20, 20));
J SplitPane sp =new J SplitPane(J SplitPane.HORIZONTAL_SPLIT,l1,l2);
sp.setContinuousLayout(true);
sp.setOneTouchExpandable(true);
sp.setDividerLocation(100);
f.getContentPane().add(sp);
f.setVisible(true);
}
}
Aplicacin SwingSet2
Incluida en las demos del SDK. Arrancar mediante: java jar SwingSet2.jar
22
Layout Managers
Todos los contenedores Swing tienen asociado un
LayoutManager para coordinar el tamao y la
situacin de sus componentes.
J Panel ->FlowLayout
Alineacin de izquierda a derecha.
Cada Layout se caracteriza por el estilo que
emplea para situar los componentes en su
interior:
Alineacin en rejilla.
Alineacin del frente a atrs.
J Frame ->BorderLayout
Implementan el interface java.awt.LayoutManager.
BoxLayout: sita los componentes en lnea vertical u
horizontal. Respeta sus tamaos.
La clase javax.swing.Box tiene mtodos para crear
zonas con espacio como createVerticalStrut(int) y zonas
que absorban los espacios como createVerticalGlue(int).
SpringLayout: permite definir la relacin (distancia) entre los
lmites de los distintos controles.
ScrollPaneLayout, ViewportLayout: utilizados internamente
por Swing para algunos de los componentes como el
ScrollPane.
Nuevos Layout Managers
23
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.J Button;
import javax.swing.J Frame;
public class BoxLayoutTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("BoxLayoutTest");
f.setSize(300,150);
f.getContentPane().setLayout(new BoxLayout(f.getContentPane(),BoxLayout.Y_AXIS));
f.getContentPane().add(new J Button("Aceptar"));
f.getContentPane().add(Box.createVerticalStrut(25));
f.getContentPane().add(new J Button("Si"));
f.getContentPane().add(new J Button("No"));
f.getContentPane().add(Box.createVerticalGlue());
f.getContentPane().add(new J Button("Cancelar"));
f.setVisible(true);
}
}
javax.swing.BoxLayout
import java.awt.Container;
import javax.swing.*;
public class SpringLayoutTest
{
public static void main(String[] args)
{
J Frame f =new J Frame();
f.setTitle("SpringLayoutTest");
Container container =f.getContentPane();
SpringLayout layout =new SpringLayout();
container.setLayout(layout);
J Label label =new J Label("Nombre: ");
J TextFieldtext =new J TextField(15);
f.getContentPane().add(label);
f.getContentPane().add(text);
layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, container);
layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, container);
layout.putConstraint(SpringLayout.WEST, text, 5, SpringLayout.EAST, label);
layout.putConstraint(SpringLayout.NORTH, text, 5, SpringLayout.NORTH, container);
layout.putConstraint(SpringLayout.EAST, container, 5, SpringLayout.EAST, text);
layout.putConstraint(SpringLayout.SOUTH, container, 5, SpringLayout.SOUTH, text);
f.pack();
f.setVisible(true);
}
}
javax.swing.SpringLayout
24
Nuevos tipos de eventos
Se ha activado, entrado o salido de un
hyperlink.
HyperlinkEvent
Ha cambiado el contenido.
ListDataEvent
Se ha activado, cerrado, desactivado,
minimizado, maximizado o abierto un internal
frame.
InternalFrameEvent
Los atributos de un Document han cambiado,
se ha insertado o se ha eliminado contenido.
DocumentEvent
El estado ha cambiado.
ChangeEvent
El cursor en un texto ha cambiado.
CaretEvent
Un padre se ha aadido, movido o eliminado.
AncestorEvent
Nuevos tipos de eventos
Se ha mostrado, ocultado o seleccionado
un men emergente.
PopupMenuEvent
El modelo de la tabla ha cambiado.
TableModelEvent
Se ha aadido, eliminado, movido,
redimensionada o seleccionada una
columna.
TableColumnModelEvent
Se ha pulsado, soltado o tecleado sobre
un men.
MenuKeyEvent
Se ha seleccionado o deseleccionado un
men.
MenuEvent
El ratn se ha arrastrado, entrado, salido,
soltado en un men.
MenuDragMouseEvent
Ha cambiado la seleccin en una lista.
ListSelectionEvent
25
Nuevos tipos de eventos
Se ha realizado una operacin que
no se puede deshacer.
UndoableEditEvent
Ha cambiado la seleccin en el
rbol.
TreeSelectionEvent
Se ha cambiado, aadido o
eliminado un elemento del rbol.
TreeModelEvent
Se ha abierto o cerrado el rbol.
TreeExpansionEvent
Origen de eventos
CellEditorListener DefaultCellEditor
DefaultTreeCellEditor
CaretListener J TextComponent
AncestorListener J Component
AdjustmentListener J ScrollBar
ActionListener AbstractButton
DefaultButtonModel
J ComboBox
J FileChooser
J TextField
Timer
Listener Componente Swing
26
Origen de eventos
ColumnModelListener DefaultTableColumnModel
ChangeListener AbstractButton
DefaultBoundedRangeModel
DefaultButtonModel
DefaultCaret
DefaultColorSelectionModel
DefaultSingleSelectionModel
J ProgressBar
J Slider
J TabbedPane
J Viewport
MenuSelectionManager
StyleContext
StyleContext.NamedStyle
Listener Componente Swing
Origen de eventos
MenuDragMouseListener J MenuItem
ListDataListener AbstractListModel
ListSelectionListener DefaultListSelectionModel
J List
MenuKeyListener J MenuItem
ItemListener AbstractButton
DefaultButtonModel
J ComboBox
InternalFrameListener J InternalFrame
HyperlinkListener J EditorPane
DocumentListener AbstractDocument
DefaultStyledDocument
Listener Componente Swing
27
Origen de eventos
TreeSelectionListener DefaultTreeSelectionModel
J Tree
TreeExpansionListener J Tree
TreeModelListener DefaultTreeModel
TableModelListener AbstractTableModel
PropertyChangeListener AbstractAction
DefaultTreeSelectionModel
J Component
SwingPropertyChangeSupport
TableColumn
UIDefaults
UIManager
PopupMenuListener J PopupMenu
MenuListener J Menu
Listener Componente Swing
Origen de eventos
VetoableChangeListener J Component
UndoableEditListener AbstractDocument
UndoableEditSupport
Listener Componente Swing
28
hyperlinkUpdate HyperlinkListener
changedUpdate
insertUpdate
removeUpdate
DocumentListener
stateChanged ChangeListener
editingCanceled
editingStopped
CellEditorListener
caretUpdate CaretListener
ancestorAdded
ancestorMoved
ancestorRemoved
AncestorListener
Mtodos Listener interface
Mtodos de los interfaces
Mtodos de los interfaces
valueChanged ListSelectionListener
contentsChanged
intervalAdded
intervalRemoved
ListDataListener
internalFrameActivated
internalFrameClosed
internalFrameClosing
internalFrameDeactivated
internalFrameDeiconified
internalFrameIconified
internalFrameOpened
InternalFrameListener
Mtodos Listener interface
29
Mtodos de los interfaces
menuCanceled
menuDeselected
menuSelected
MenuListener
menuKeyPressed
menuKeyReleased
menuKeyTyped
MenuKeyListener
menuDragMouseDragged
menuDragMouseEntered
menuDragMouseExited
menuDragMouseReleased
MenuDragMouseListener
Mtodos Listener interface
popupmenuCanceled
popupMenuWillBecomeInvisible
popupMenuWillBecomeVisible
PopupMenuListener
mouseClicked
mouseDragged
mouseEntered
mouseExited
mouseMoved
mousePressed
mouseReleased
MouseInputListener
Mtodos Listener interface
Mtodos de los interfaces
30
treeCollapsed
treeExpanded
TreeExpansionListener
tableChanged TableModelListener
treeNodesChanged
treeNodesInserted
treeNodesRemoved
treeStructureChanged
TreeModelListener
columnAdded
columnMarginChanged
columnMoved
columnRemoved
columnSelectionChanged
TableColumnModelListener
Mtodos Listener interface
Mtodos de los interfaces
undoableEditHappened UndoableEditListener
valueChanged TreeSelectionListener
Mtodos Listener interface
Mtodos de los interfaces
31
Look & Feel
AWT: Heavyweight Swing: Lightweight
Look & Feel
Windows Look & Feel. Funciona solo en plataforma
Microsoft Windows.
En WinXP, el Look & Feel de Windows es diferente.
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
32
Look & Feel
J ava Look & Feel (Metal). Multiplataforma.
Mac Look & Feel (Aqua). Funciona solo en
plataforma Macintosh.
javax.swing.plaf.metal.MetalLookAndFeel
com.sun.java.swing.plaf.mac.MacLookAndFeel
Look & Feel
Motif Look & Feel. Multiplataforma.
GTK Look & Feel. Multiplataforma.
com.sun.java.swing.plaf.motif.Moti fLookAndFeel
com.sun.java.swing.plaf.gtk.GTKLookAndFeel
33
Existen distintas alternativas para seleccionar el
Look & Feel de una aplicacin J ava:
java Dswing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel MiApp
Look & Feel
Por lnea de comando:
#Swing properties
swing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel
Mediante el fichero swing.properties (localizado en el
directorio \lib del J RE):
Por cdigo de forma esttica (al inicio del programa):
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch (Exception ex) { }
Look & Feel
Por cdigo de forma dinmica:
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
}
catch (Exception ex)
{
}
Nota: siendo frame, el contenedor raz de la aplicacin.
En J 2SE 5.0, el J ava Look & Feel
Metal tiene una configuracin
distinta (theme Ocean).
5.0
34
Bibliografa
J ava Swing (2nd edition).
Marc Loy, Robert Eckstein, Dave Wood, J ames Elliot y Brian Cole.
OReilly
Graphic J ava 2, Volume 2: Swing (3rd edition)
David M. Geary.
Prentice Hall.
The Swing tutorial (on-line)
http://java.sun.com/docs/books/tutorial/uiswing/
The Swing Tutorial (2nd edition)
Kathy Walrath y Mary Campione.
Addison-Wesley.

También podría gustarte