Jtable (Datos Del Alumno)
Jtable (Datos Del Alumno)
Jtable (Datos Del Alumno)
MAESTRO(A):
Ing. Luis Eduardo Gutiérrez Ayala
MATERIA:
Tópicos Avanzados de Programación (TAP)
SEMESTRE:
4to
CARRERA:
Sistemas Computacionales
HORA:
Martes y jueves 10:30-12:15
Viernes 11:20-12:10
AULA:
C-C-LC1
Redacción del problema
Crear un programa con interfaz grafica en el cual se muestre un Jtable con datos de alumnos
(P.E: Nombre, Especialidad, Semestre, etc.), además agregar un escucha donde el renglón
seleccionado muestre en una etiqueta ubicada en el sur de la ventana la información del
registro seleccionado.
Código fuente
Clase
package JTable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
this.atributos();
this.armado();
this.escuchas();
this.lanzar_IGU();
}
//Atributos de los componentes
public void atributos() {
ventana.setTitle("Jtable(Datos del Alumno)");
ventana.setSize(500, 300);
ventana.setLayout(new BorderLayout());
ventana.setResizable(true);
tabla.setBackground(Color.CYAN);
tabla.setSelectionBackground(Color.lightGray);
tabla.setAutoCreateRowSorter(true);
//tmodel.addColumn("otro");
String [] temporal = {"Jesus", "ISC", "2"};
tmodel.addRow(temporal);
lblinf.setFont(new Font("Tletter", Font.HANGING_BASELINE, 18));
}
//Armar la interfaz
public void armado() {
ventana.add(scroll, BorderLayout.CENTER);
ventana.add(lblinf, BorderLayout.SOUTH);
}
//Asignar los escuchas
public void escuchas() {
Esc e = new Esc();
tabla.addMouseListener(e);
}
//Lanzar la interfaz
public void lanzar_IGU() {
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setLocationRelativeTo(null);
ventana.setVisible(true); }
public class Esc extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e) {
int selectC = tabla.getSelectedColumn();
int selectR = tabla.getSelectedRow();
lblinf.setText(String.valueOf("<html>Nombre: "+tabla.getValueAt(selectR, 0)
+"<p>Especialidad: "+tabla.getValueAt(selectR, 1)
+"<p>Semestre: "+tabla.getValueAt(selectR, 2)));
}
}
}
Clase Main
package JTable;