Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Java 1 Java 2
Java 3 Java 4
Swing: Modelo-Delegado Ejemplo de modelo compartido
public class ModeloCompartido {
JTextArea areaTexto1, areaTexto2;
JFrame ventana;
Document documento;
public ModeloCompartido(String titulo){
Componente ventana = new JFrame(titulo);
Swing Vista Container panelContenido = ventana.getContentPane();
areaTexto1= new JTextArea(10,100);
documento=areaTexto1.getDocument();
Modelo areaTexto2= new JTextArea(documento);
areaTexto2.setColumns(100);
areaTexto2.setRows(10);
panelContenido.setLayout(new BoxLayout(panelContenido, BoxLayout.Y_AXIS));
panelContenido.add(new JScrollPane(areaTexto1));
Controlador panelContenido.add(Box.createGlue());
delegado panelContenido.add(new JScrollPane(areaTexto2));
panelContenido.add(Box.createGlue());
ventana.setSize(500, 300); ventana.setVisible(true);
}
public static void main(String args[]) {
ModeloCompartido aplicacion = new ModeloCompartido("Prueba documento compartido"); }
}//ModeloDocumentoCompartido
Java 5 Java 6
Java 7 Java 8
Modelos de datos de las listas Ejemplo del tutorial de Swing de Sun
• interface ListModel listModel = new DefaultListModel();
listModel.addElement("Alison Huml");
❏ void addListDataListener(ListDataListener l) listModel.addElement("Kathy Walrath");
Adds a listener to the list that's notified each time a change to the listModel.addElement("Lisa Friendly");
data model occurs. listModel.addElement("Mary Campione");
❏ Object getElementAt(int index)
list = new JList(listModel);
Returns the value at the specified index.
❏ int getSize() public void actionPerformed(ActionEvent e) {
Returns the length of the list. int index = list.getSelectedIndex();
❏ void removeListDataListener(ListDataListener l) listModel.remove(index);
Removes a listener from the list that's notified each time a change int size = listModel.getSize();
to the data model occurs. //Nobody's left, disable firing
if (size == 0) {
• class DefaultListModel fireButton.setEnabled(false); //Adjust the selection }
Oyente de acciones
registrado en el
• Modelo de datos de las listas que se comporta como un Vector else {
botón Fire
//removed item in last position
• Implementa el interface ListModel if (index == listModel.getSize())
index--; //otherwise select same index
list.setSelectedIndex(index); } }
Java 9 Java 10
Ejemplo continuación
public void actionPerformed(ActionEvent e) {
//User didn't type in a name...
if (employeeName.getText().equals("")) { Oyente de acciones
Toolkit.getDefaultToolkit().beep(); registrado en el
return; botón Hire
}
int index = list.getSelectedIndex();
int size = listModel.getSize();
//If no selection or if item in last position is selected,
//add the new hire to end of list, and select new hire
if (index == -1 || (index+1 == size)) {
listModel.addElement(employeeName.getText());
list.setSelectedIndex(size);
//Otherwise insert the new hire after the current selection, and select new hire
}
else {
listModel.insertElementAt(employeeName.getText(), index+1);
list.setSelectedIndex(index+1);
}}
Java 11