0% encontró este documento útil (0 votos)
61 vistas6 páginas

Jtable (Datos Del Alumno)

Descargar como pdf o txt
Descargar como pdf o txt
Descargar como pdf o txt
Está en la página 1/ 6

NOMBRE:

José de Jesús Gaytán Ramírez

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;

public class tabla {


private JFrame ventana;
private JPanel panel;
private JTable tabla;
private DefaultTableModel tmodel;
private JLabel lblinf;
private String [] titulos = {"Nombre del alumno", "Especialidad", "Semestre"};
private String [][] datos = {{"Jesus Gaytan", "Sistemas", "4"},
{"Norma Rojas", "Industrial", "8"},
{"Jose Oramas", "Mecatronica", "3"},
{"Jose Ramirez", "Electro", "6"},
{"Cecilia Ramirez", "Gestion", "8"},
{"Sharif Fernandez", "Tic´s", "6"},
{"Daniel Heredia", "Logistica", "5"},
{"Willy Rodriguez", "Sistemas", "7"},
{"Adan Nuñez", "Tic´s", "8"}};
private JScrollPane scroll;
//constructor inicializa los componentes
public tabla() {
ventana= new JFrame();
tmodel = new DefaultTableModel(datos, titulos);
panel = new JPanel();
//tabla = new JTable(datos, titulos);
tabla = new JTable(tmodel);
scroll = new JScrollPane(tabla);
lblinf = new JLabel("Informacion del registro", SwingConstants.LEADING);

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;

public class PruebaTabla {

public static void main(String[] args) {


new tabla();
}
}
Capturas del funcionamiento del programa

También podría gustarte