Send data to jInternalFrame with a button from another JinternalFrame, without losing its visibility. JAVA

3

My problem is that when I want to use the same function again (press the button to change the label for another value in the table), the first window runs out of functionality !. When it is done the first time it makes it perfect, but when wanting to do it again it is as if it were not useful at all from the form. It does not capture any of the text fields nor the values of the table. I already made the test with debugging the code. My code is simple is just to capture a value from the second row of the jtable and from there to save it in a jlabel. As I said the first one makes it perfect but then the first jinternalframe is left without functionality. What am I doing wrong? Or the instances declare it in another way? or just missing something? Here's the Code:

import Controlador.control_Productos;
import Controlador.control_existencias;
import Modelo.Productos;
import com.mxrck.autocompleter.TextAutoCompleter;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public final class vGestion_Productos extends javax.swing.JInternalFrame {

    control_existencias conexis = new control_existencias();
    control_Productos producto = new control_Productos();
    Productos p = new Productos();
    private TextAutoCompleter ac;
    String id, fecha, nombre;
    Timestamp fechaseleccionada;
    DefaultTableModel datos;
    public vGestion_Productos() {
        initComponents();
        AutoCompletarCategoriaProducto();
        Mostrar();
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int i = JOptionPane.showConfirmDialog(null, "Esta seguro que desea modificar?", "confirmar", JOptionPane.YES_NO_OPTION);
                    if (i == 0) {
                        int fila = jTable1.rowAtPoint(e.getPoint());
                        fecha = jTable1.getValueAt(fila, 4).toString();
                        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            fechaseleccionada = new java.sql.Timestamp(df.parse(fecha).getTime());
                        } catch (ParseException ex) {
                            Logger.getLogger(vLista_Compras.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        id = jTable1.getValueAt(fila, 0).toString();
                        jTextField1.setText(jTable1.getValueAt(fila, 1).toString());
                        jTextField2.setText(jTable1.getValueAt(fila, 2).toString());
                        jTextField3.setText(jTable1.getValueAt(fila, 3).toString());
                        jDateChooser1.setDate(fechaseleccionada);
                    }
                }
            }
        });
    }

    public void AutoCompletarCategoriaProducto() {
        ac = new TextAutoCompleter(jTextField1);
        Object[] dato = conexis.combox("categoriasproductos", "descripcion");
        for (Object row : dato) {
            ac.addItem(row.toString());
        }
    }

    public void Mostrar() {
        String[] columnas = {"IDPROD", "IDCATEGORIAPRODUCTO", "DESCRIPCION", "PRECIO VENTA", "FECHA REGISTRO"};
        Object[][] dato = producto.MostrarDatos();
        datos = new DefaultTableModel(dato, columnas);
        jTable1.setModel(datos);
        ocultar_columnas();
    }

    public void ocultar_columnas() {
        jTable1.getColumnModel().getColumn(0).setMaxWidth(0);
        jTable1.getColumnModel().getColumn(0).setMinWidth(0);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(0);
    }

    public void LimpiarSeleccion(){
        jTable1.clearSelection();
        jTable1.getSelectionModel().clearSelection();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 =  new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        jMenuItem1.setText("jMenuItem1");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem1);

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setAutoscrolls(true);
        setComponentPopupMenu(jPopupMenu1);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e1) {
            e1.printStackTrace();
        }
        setVisible(true);

        jLabel1.setText("Categoria");

        jLabel2.setText("Nombre");

        jLabel3.setText("Precio Venta");

        jLabel4.setText("Fecha Registro");

        jDateChooser1.setDateFormatString("yyyy-MM-dd HH:mm:ss");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setComponentPopupMenu(jPopupMenu1);
        jTable1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Modificar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Eliminar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Abrir Receta");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(123, 123, 123)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(142, 142, 142)
                .addComponent(jButton4)
                .addGap(129, 129, 129))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        p.setDescripcion(jTextField2.getText());
        p.setPrecioventa(Float.parseFloat(jTextField3.getText()));

        if (!jTextField2.getText().trim().equals("") && !jTextField3.getText().trim().equals("")) {
            if (producto.InsertarProductos(p)) {
                JOptionPane.showMessageDialog(null, "Nuevo Producto agregado");
                Mostrar();
            }
        }
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        p.setDescripcion(jTextField2.getText());
        p.setPrecioventa(Float.parseFloat(jTextField3.getText()));
        p.setIdproducto(Integer.parseInt(id));

        if (!jTextField2.getText().trim().equals("") && !jTextField3.getText().trim().equals("")) {
            if (producto.EditarProductos(p)) {
                JOptionPane.showMessageDialog(null, "Modificacion Completa");
                Mostrar();
            }
        }
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        int seleccionado=jTable1.getSelectedRow();
        if (seleccionado == -1) {
            JOptionPane.showMessageDialog(null, "Debes seleccionar una fila");
        } else {
            int i = JOptionPane.showConfirmDialog(null, "Esta seguro de Eliminar?", "confirmar", JOptionPane.YES_NO_OPTION);
            if (i == 0) {
                p.setIdproducto(Integer.parseInt((String) jTable1.getValueAt(seleccionado, 0)));
                if (producto.EliminarProductos(p)) {
                    JOptionPane.showMessageDialog(null, "Eliminado");
                    datos.removeRow(seleccionado + 1);
                    Mostrar();
                }
            }
        }
    }                                        

// From below the problem begins.

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                   
        int seleccionado= jTable1.getSelectedRow();
        if(seleccionado == -1){
          JOptionPane.showMessageDialog(null, "Debes seleccionar una fila");       
        }else{
            vGestion_Recetas receta = new vGestion_Recetas();
            if(receta.isShowing()==false){
                vMenuPrincipal.jDesktopPane1.add(receta);
                receta.toFront();
                receta.setVisible(true);
                vGestion_Recetas.jLabel2.setText(jTable1.getValueAt(seleccionado, 2).toString());
            }else{
                vGestion_Recetas.jLabel2.setText(jTable1.getValueAt(seleccionado, 2).toString());
            }              
        }
        LimpiarSeleccion();
}

Thank you in advance for your help!

    
asked by Exe Herrera 30.09.2018 в 09:52
source

1 answer

2

Well I found the solution to this. It turns out that the second jinternalframe (which opens with the button), we must add in its constructor the first jinternalframe with its corresponding jdesktoppane added from it. It seems that you lose the component inside the container when another window opens on the same window.

public void habilitar(){
        vGestion_Productos producto = new vGestion_Productos();
        vMenuPrincipal.jDesktopPane1.add(producto); 
        producto.toFront();
        producto.setVisible(true);
}

In my case I made a method that does not return anything and I implement it in the constructor of the jinternalframe that was called.

    
answered by 30.09.2018 в 22:04