Primero
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
# Eclipse
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
|
||||
# Compilación Java
|
||||
bin/
|
||||
target/
|
||||
*.class
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Sistema operativo
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,160 @@
|
||||
package prueba.aadd.swing;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Component;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.DefaultListCellRenderer;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.dao.IContinenteDAO;
|
||||
import org.palomafp.dam2.aadd.anp.dao.IPaisDAO;
|
||||
import org.palomafp.dam2.aadd.anp.dao.impl.ContinenteDaoCsv;
|
||||
import org.palomafp.dam2.aadd.anp.dao.impl.PaisDaoText;
|
||||
import org.palomafp.dam2.aadd.anp.model.Continente;
|
||||
import org.palomafp.dam2.aadd.anp.model.Pais;
|
||||
|
||||
public class VentanaContinentesPaises extends JFrame {
|
||||
|
||||
private JComboBox<Continente> comboContinentes;
|
||||
private JList<Pais> listaPaises;
|
||||
private DefaultListModel<Pais> modeloPaises;
|
||||
private JTextArea areaInfo;
|
||||
|
||||
private List<Pais> paisesActuales;
|
||||
|
||||
private IContinenteDAO continenteDAO;
|
||||
private IPaisDAO paisDAO;
|
||||
|
||||
public VentanaContinentesPaises() {
|
||||
|
||||
super("Continentes y países");
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(500, 350);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
|
||||
continenteDAO = new ContinenteDaoCsv();
|
||||
paisDAO = new PaisDaoText();
|
||||
|
||||
|
||||
comboContinentes = new JComboBox<>();
|
||||
modeloPaises = new DefaultListModel<>();
|
||||
listaPaises = new JList<>(modeloPaises);
|
||||
|
||||
areaInfo = new JTextArea(4, 30);
|
||||
areaInfo.setEditable(false);
|
||||
areaInfo.setLineWrap(true);
|
||||
areaInfo.setWrapStyleWord(true);
|
||||
|
||||
comboContinentes.setRenderer(new DefaultListCellRenderer() {
|
||||
@Override
|
||||
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
|
||||
boolean cellHasFocus) {
|
||||
|
||||
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||||
|
||||
if (value instanceof Continente) {
|
||||
setText(((Continente) value).getNombre());
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
JPanel panel = new JPanel(new BorderLayout(5, 5));
|
||||
|
||||
JPanel panelSuperior = new JPanel(new BorderLayout(5, 5));
|
||||
panelSuperior.add(new JLabel("Selecciona un continente:"), BorderLayout.WEST);
|
||||
panelSuperior.add(comboContinentes, BorderLayout.CENTER);
|
||||
|
||||
panel.add(panelSuperior, BorderLayout.NORTH);
|
||||
panel.add(new JScrollPane(listaPaises), BorderLayout.CENTER);
|
||||
panel.add(new JScrollPane(areaInfo), BorderLayout.SOUTH);
|
||||
|
||||
add(panel);
|
||||
|
||||
|
||||
cargarContinentes();
|
||||
|
||||
|
||||
comboContinentes.addActionListener(e -> cargarPaises());
|
||||
|
||||
listaPaises.addListSelectionListener(e -> {
|
||||
if (!e.getValueIsAdjusting()) {
|
||||
Pais seleccionado = listaPaises.getSelectedValue();
|
||||
|
||||
if (seleccionado != null) {
|
||||
mostrarInfo(seleccionado);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void cargarContinentes() {
|
||||
List<Continente> continentes = continenteDAO.obtenerTodosContinentes();
|
||||
|
||||
comboContinentes.removeAllItems();
|
||||
|
||||
if (continentes != null) {
|
||||
for (Continente continente : continentes) {
|
||||
comboContinentes.addItem(continente);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cargarPaises() {
|
||||
Continente seleccionado = (Continente) comboContinentes.getSelectedItem();
|
||||
|
||||
modeloPaises.clear();
|
||||
areaInfo.setText("");
|
||||
|
||||
if (seleccionado == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
paisesActuales = paisDAO.obtenerPaisesPorContinente(seleccionado.getCodigoContinente());
|
||||
|
||||
if (paisesActuales == null || paisesActuales.isEmpty()) {
|
||||
areaInfo.setText("No hay países disponibles para el continente seleccionado.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (Pais pais : paisesActuales) {
|
||||
modeloPaises.addElement(pais);
|
||||
}
|
||||
|
||||
if (!paisesActuales.isEmpty()) {
|
||||
listaPaises.setSelectedIndex(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void mostrarInfo(Pais pais) {
|
||||
String texto = String.format("%s, capital de %s – %d, pertenece al continente %s (%s)",
|
||||
pais.getCapital(),
|
||||
pais.getNombre(),
|
||||
pais.getId(),
|
||||
pais.getContinente().getNombre(),
|
||||
pais.getContinente().getCodigoContinente());
|
||||
|
||||
areaInfo.setText(texto);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
VentanaContinentesPaises ventana = new VentanaContinentesPaises();
|
||||
ventana.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user