import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tablas extends JFrame implements ActionListener {
String[] columnNames = {"Nombre", "Apellidos", "Telefono", "Direccion"};
//Objetos que se agregaran a la ventana principal
private JTable tabla = new JTable(new String[1][4], columnNames);
private JScrollPane scroll = new JScrollPane(tabla);
private JButton btnAceptar = new JButton("Procesar Datos");
private JPanel MyPanel = new JPanel(new BorderLayout());
//Constructor
public Tablas() {
Container contenedor = getContentPane();
contenedor.add("North", new JLabel("Introduce tus datos personales"));
contenedor.add("Center", scroll);
MyPanel.add("East", btnAceptar);
contenedor.add("South", MyPanel);
btnAceptar.addActionListener( this );
//Propiedades de la ventana
setTitle("Tablas");
setSize(600,110);
setResizable(false);
setVisible(true);
}
//Eventos
public void actionPerformed (ActionEvent ev) {
try {
String texto = "";
//Recorre la tabla de la misma manera que se recorre una matriz
for (int a=0; a<tabla.getRowCount(); a++)
for (int b=0; b<tabla.getColumnCount(); b++) {
//Obtiene el valor x,y y el nombre de la columna
texto += tabla.getColumnName(b) + ": " + tabla.getValueAt(a,b) + "\n";
}
JOptionPane.showMessageDialog(null, texto);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Ocurrio un error al obtener los datos");
}
}
public static void main(String args[]) {
new Tablas();
}
}