Pass a JtextField to a Jtable

0

I need to pass the information that is placed in a JtextField to a Jtable this is activated when you click on the voton update someone who can help me please this is my code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class ConexionAcess extends JFrame{

private JTable tbl;
private static DefaultTableModel mod;
private int i=-1;
private int a=-1;
private JTextField tnom, tarea, tmot, tf,th,ths,thl;
private JLabel lnom, larea,lmot,lf,lh,lhs,lhl;
JScrollPane pan;
String titulos [] = {"Nombre","Area","Motivo","Fecha","Horas autorizdas","Hora de salida","Hora de llegada"};
JButton btnagr,nuev,ex,im;

    public ConexionAcess(){

        JTable tabl;
        JScrollPane pan;
        DefaultTableModel datprin;

            setLayout(null);
            setTitle("Pases de salida");
            setSize(700,500);
            setVisible(true);

            btnagr = new JButton("Actualizar");
            nuev = new JButton("Agregar nuevo");    
            ex = new JButton("Guardar Tabla");
            im=new JButton("Seleccionar tabla");

            tnom = new JTextField();
            tarea = new JTextField();
            tmot = new JTextField();
            tf = new JTextField();
            th = new JTextField();
            ths = new JTextField();
            thl = new JTextField();

            lnom = new JLabel ("Nombre:");
            larea = new JLabel ("Area:");
            lmot = new JLabel ("Motivo:");
            lf = new JLabel ("Fecha:");
            lh = new JLabel ("Horas autorizadas:");
            lhs = new JLabel ("Hora de salida:");
            lhl = new JLabel ("Hora de llegada:");


            datprin = new DefaultTableModel(null,titulos);
            tabl = new JTable(datprin);
            pan = new JScrollPane(tabl);

            add(pan);
            add(tnom);
            add(tarea);
            add(tmot);
            add(tf);
            add(th);
            add(ths);
            add(thl);

            add(lnom);
            add(larea);
            add(lmot);
            add(lf);
            add(lh);
            add(lhs);
            add(lhl);

            add(btnagr);
            add(nuev);
            add(ex);
            add(im);

            pan.setBounds(5,250,700,200);

            lnom.setBounds(10,10,200,25);
            tnom.setBounds(70,10,200,25);

            larea.setBounds(10,50,200,25);
            tarea.setBounds(70,50,200,25);

            lmot.setBounds(10,85,200,25);
            tmot.setBounds(70,85,200,25);

            lf.setBounds(10,125,200,25);
            tf.setBounds(70,125,200,25);

            lh.setBounds(350,10,200,25);
            th.setBounds(470,10,200,25);

            lhs.setBounds(350,50,200,25);
            ths.setBounds(470,50,200,25);

            lhl.setBounds(350,85,200,25);
            thl.setBounds(470,85,200,25);

            btnagr.setBounds(50, 180, 100, 25);
            nuev.setBounds(180, 180, 120, 25);
            ex.setBounds(330, 180, 120, 25);
            im.setBounds(480, 180, 140, 25);


            tnom.setColumns(0);
            tarea.setColumns(2);
            tmot.setColumns(3);
            tf.setColumns(4);
            th.setColumns(5);
            ths.setColumns(6);
            thl.setColumns(7);


            nuev.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    nuevo();
                }
            });

            btnagr.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    agregar();
                }

                private void agregar() {
                    Object []object = new Object[7];
                    object[0] = tnom.getText();
                    object[1] = tarea.getText(); 
                    object[2] = tmot.getText(); 
                    object[3] = tf.getText(); 
                    object[4] = th.getText(); 
                    object[5] = ths.getText(); 
                    object[6] = thl.getText(); 

                    mod.addRow(object);

                    }


            });

            tabl.addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent evt) {
                    tablMouseClicked(evt);
                }

                private void tablMouseClicked(MouseEvent evt) {
                    // TODO Auto-generated method stub
                    tbl = new JTable();
                      tbl.addMouseListener(new MouseAdapter() {
                       @Override
                       public void mouseClicked(MouseEvent e) {
                       tnom.setText("");
                        tarea.setText("");
                        tmot.setText("");
                        tf.setText("");
                        th.setText("");
                        ths.setText("");
                        thl.setText("");


                        int row = tbl.rowAtPoint(e.getPoint());

                        tnom.setText(tbl.getValueAt(row, 0).toString());
                        tarea.setText(tbl.getValueAt(row, 1).toString());
                        tmot.setText(tbl.getValueAt(row, 2).toString());
                        tf.setText(tbl.getValueAt(row, 3).toString());
                        th.setText(tbl.getValueAt(row, 4).toString());
                        ths.setText(tbl.getValueAt(row, 5).toString());
                        thl.setText(tbl.getValueAt(row, 6).toString());
                       }
                      });
                }
            });


            ex.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    exportar();

                }

            });     

            im.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    importar();
                }
            });
}


    private void importar() {
        // TODO Auto-generated method stub


    }


    private void nuevo() {
        // TODO Auto-generated method stub

        tnom.setText("");
        tarea.setText("");
        tmot.setText("");
        tf.setText("");
        th.setText("");
        ths.setText("");
        thl.setText("");
    }


    private void exportar() {
        // TODO Auto-generated method stub

        JFileChooser seleccionar = new JFileChooser();
        File archivo;
        if (seleccionar.showDialog(null, "Exportar a Excel") == JFileChooser.APPROVE_OPTION){
            archivo = seleccionar.getSelectedFile();
            int cantFila = mod.getRowCount();
            int cantColumna = mod.getColumnCount();
            HSSFWorkbook wb;
            wb = new HSSFWorkbook();

            Sheet hoja = ((org.apache.poi.ss.usermodel.Workbook) wb).createSheet(" ");

                try {
                        List<JTable> tb = new ArrayList<JTable>();
                        tb.add(tbl);

        for (int j = -1; j < cantFila; j++) {
            Row fila = hoja.createRow(j+ 1);
            for (int i = 0; i < cantColumna; i++) {
                Cell celda = fila.createCell(i);
                if (j == -1) {
                    celda.setCellValue(String.valueOf(mod.getColumnName(i)));
                } else {
                    celda.setCellValue(String.valueOf(mod.getValueAt(j, i)));
                }
                wb.write(new FileOutputStream(archivo + ".xls"));
            }
        }
        JOptionPane.showMessageDialog(null, "Exportacion exitosa");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Vuelve a intentarlo");
    }
}
    }   


    public static void main(String [] args){
        new ConexionAcess();

    }

}
    
asked by nataly paola avila pineda 26.07.2018 в 19:53
source

0 answers