How to fill Java Textbox depending on the value of 2 Combobox?

2

I have created a java form with NetBeans, a JFrame (Main), Dialog (Enrollment), Student Class, Student Manager Class. In Enrollment I have 2 comboboxes one for the cycle and another for the course, depending on that the txtcredito (Credito) and txtcostocredito (CostCredit) is filled according to the table:

This is the form:

I have done the programming for the calculate button (credit * costcredito) = montomatricula. and the summary button that shows you all the entered data (Name, Surname, Cycle, etc.).

Also perform the conditional with the combobox to fill the textbox but do not show it to me. There I require your support. I put the code to help me.

FrmCalculaMatricula (JDialog)

    import javax.swing.JOptionPane;

    public class FrmCalculaMatricula extends javax.swing.JDialog
    {
private GestorEstudiante estudiantes;
private Estudiante estudiante;

public GestorEstudiante getEstudiante() {
    return estudiantes;
}

public void setEstudiantes(GestorEstudiante estudiante) {
    this.estudiantes = estudiante;
}

/**
 * Creates new form FrmCalculaMatricula
 */
public FrmCalculaMatricula(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    txtResumen = new javax.swing.JTextArea();
    lblCreditos = new javax.swing.JLabel();
    btnSalir = new javax.swing.JButton();
    lblCostoCredito = new javax.swing.JLabel();
    lblMontoMatricula = new javax.swing.JLabel();
    txtCreditos = new javax.swing.JTextField();
    lblMatriculaA = new javax.swing.JLabel();
    lblResumenMatricula = new javax.swing.JLabel();
    txtMontoMatricula = new javax.swing.JTextField();
    txtCostoCredito = new javax.swing.JTextField();
    cboCiclo = new javax.swing.JComboBox();
    jSeparator1 = new javax.swing.JSeparator();
    btnCalcular = new javax.swing.JButton();
    txtApellido = new javax.swing.JTextField();
    lblCurso = new javax.swing.JLabel();
    lblNombre = new javax.swing.JLabel();
    lblApellido = new javax.swing.JLabel();
    cboCurso = new javax.swing.JComboBox();
    lblCiclo = new javax.swing.JLabel();
    txtNombre = new javax.swing.JTextField();
    lblDatoAlumno = new javax.swing.JLabel();
    btnResumir = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Calcular Matricula");

    jPanel1.setToolTipText("");
    jPanel1.setName("");

    txtResumen.setColumns(20);
    txtResumen.setEditable(false);
    txtResumen.setRows(5);
    jScrollPane1.setViewportView(txtResumen);

    lblCreditos.setText("Créditos");

    btnSalir.setText("Salir");
    btnSalir.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnSalirActionPerformed(evt);
        }
    });

    lblCostoCredito.setText("Costo-Credito");

    lblMontoMatricula.setText("Monto de Matrícula");

    txtCreditos.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
    txtCreditos.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtCreditosActionPerformed(evt);
        }
    });

    lblMatriculaA.setText("Matricula Alumno");

    lblResumenMatricula.setText("Resumen de la Matricula");

    txtCostoCredito.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtCostoCreditoActionPerformed(evt);
        }
    });

    cboCiclo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" }));
    cboCiclo.setSelectedItem(cboCiclo);
    cboCiclo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cboCicloActionPerformed(evt);
        }
    });

    btnCalcular.setText("Calcular");
    btnCalcular.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCalcularActionPerformed(evt);
        }
    });

    lblCurso.setText("Curso");

    lblNombre.setText("Nombre");

    lblApellido.setText("Apellido");

    cboCurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Curso A", "Curso B", "Curso C" }));

    lblCiclo.setText("Ciclo");

    txtNombre.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtNombreActionPerformed(evt);
        }
    });

    lblDatoAlumno.setText("Datos del Alumno");

    btnResumir.setText("Resumir");
    btnResumir.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnResumirActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(170, 170, 170)
                    .addComponent(lblMatriculaA))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(lblDatoAlumno)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(lblNombre)
                            .addGap(18, 18, 18)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(38, 38, 38)
                    .addComponent(lblApellido)
                    .addGap(18, 18, 18)
                    .addComponent(txtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 458, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(17, 17, 17)
                    .addComponent(lblCiclo)
                    .addGap(18, 18, 18)
                    .addComponent(cboCiclo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(29, 29, 29)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lblCurso)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(btnResumir)
                                .addGap(11, 11, 11)))
                        .addComponent(btnCalcular, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(32, 32, 32)
                            .addComponent(lblCreditos)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txtCreditos, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(lblCostoCredito)
                            .addGap(18, 18, 18)
                            .addComponent(txtCostoCredito, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(lblMontoMatricula)
                            .addGap(18, 18, 18)
                            .addComponent(txtMontoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblResumenMatricula)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(108, 108, 108)))
            .addContainerGap(40, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(lblDatoAlumno)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblNombre)
                .addComponent(lblApellido)
                .addComponent(txtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(lblMatriculaA)
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lblCiclo)
                .addComponent(cboCiclo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblCurso)
                .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblCreditos)
                .addComponent(txtCreditos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblCostoCredito)
                .addComponent(txtCostoCredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(65, 65, 65)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnCalcular)
                        .addComponent(txtMontoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblMontoMatricula))
                    .addGap(38, 38, 38)
                    .addComponent(lblResumenMatricula)
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(35, 35, 35)
                    .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(189, 189, 189)
                    .addComponent(btnResumir)))
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

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

private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
}                                         

private void cboCicloActionPerformed(java.awt.event.ActionEvent evt) {                                         

}                                        

private void btnResumirActionPerformed(java.awt.event.ActionEvent evt) {                                           

    this.txtResumen.setText( "Datos del Alumno "+ "  Nombres: "+ txtNombre.getText()
            +" Apellidos "+txtApellido.getText()+" Ciclo:  "+ cboCiclo.getSelectedItem()+" Curso: "+
            cboCurso.getSelectedItem()+" Credito: "+ txtCreditos.getText()+
            " Costo Credito: "+ txtCostoCredito.getText()+
            " Monto a Pagar Matricula: "+ txtMontoMatricula.getText());
   this.txtResumen.setLineWrap(true);
   this.txtResumen.setWrapStyleWord(true);
}                                          

private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.dispose();
}                                        

private void txtCreditosActionPerformed(java.awt.event.ActionEvent evt) {                                            

    int opCiclo = this.cboCiclo.getSelectedIndex();
    int opCurso = this.cboCurso.getSelectedIndex();
    float cred;

    if (opCiclo==0 || opCiclo==1 || opCiclo==2)
    {
        switch(opCurso)
        {
            case 0:
                cred=0;
                this.txtCreditos.setText(""+cred);
                break;
            case 1:
                cred=4;
                this.txtCreditos.setText(""+cred);
                break;
            default:
                cred=3;
                this.txtCreditos.setText(""+cred);                 
                break;
        }
    }
    else if (opCiclo==3 || opCiclo==4 || opCiclo==5)
    {
        switch(opCurso)
        {
            case 0:
                cred=4;                    
                this.txtCreditos.setText(""+cred);                 
                break;
            case 1:
                cred=3;                    
                this.txtCreditos.setText(""+cred);                 
                break;
            default:
                cred=6;                    
                this.txtCreditos.setText(""+cred);                 
                break;
        }
    }
    else
    {
        switch(opCurso)
        {
            case 0:
                cred=3;                    
                this.txtCreditos.setText(""+cred);                 
                break;
            case 1:
                cred=4;                    
                this.txtCreditos.setText(""+cred);                 
                break;
            default:
                cred=5;                    
                this.txtCreditos.setText(""+cred);                 
                break;
        }
    }        
}                                           

private void txtCostoCreditoActionPerformed(java.awt.event.ActionEvent evt) {                                               
    int opCiclo = this.cboCiclo.getSelectedIndex();
    int opCurso = this.cboCurso.getSelectedIndex();
    float cred;

    if (opCiclo==0 || opCiclo==1 || opCiclo==2)
    {
        switch(opCurso)
        {
            case 0:
                cred=5;
                this.txtCostoCredito.setText(""+cred);
                break;
            case 1:
                cred=8;
                this.txtCostoCredito.setText(""+cred);
                break;
            default:
                cred=6;
                this.txtCostoCredito.setText(""+cred);                  
                break;                
        }
    }
    else if (opCiclo==3 || opCiclo==4 || opCiclo==5)
    {
        switch(opCurso)
        {
            case 0:
                cred=6;
                this.txtCostoCredito.setText(""+cred);               
                break;
            case 1:
                cred=2;
                this.txtCostoCredito.setText(""+cred);
                break;
            default:
                cred=8;
                this.txtCostoCredito.setText(""+cred);                  
                break;                
        }
    }
    else
    {
        switch(opCurso)
        {
            case 0:
                cred=2;
                this.txtCostoCredito.setText(""+cred);                   
                break;
            case 1:
                cred=5;
                this.txtCostoCredito.setText(""+cred);
                break;
            default:
                cred=4;
                this.txtCostoCredito.setText(""+cred);                 
                break;                
        }
    }
}                                               

private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) {                                            

    float resultado;
    float valor1 = Float.parseFloat(this.txtCostoCredito.getText());
    float valor2 = Float.parseFloat(this.txtCreditos.getText());        
    resultado = valor1 * valor2;
    this.txtMontoMatricula.setText(""+resultado);
    //this.txtMontoMatricula

}                                           

public static void main(String args[]) 
{
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            FrmCalculaMatricula dialog = new FrmCalculaMatricula(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JButton btnCalcular;
private javax.swing.JButton btnResumir;
private javax.swing.JButton btnSalir;
private javax.swing.JComboBox cboCiclo;
private javax.swing.JComboBox cboCurso;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JLabel lblApellido;
private javax.swing.JLabel lblCiclo;
private javax.swing.JLabel lblCostoCredito;
private javax.swing.JLabel lblCreditos;
private javax.swing.JLabel lblCurso;
private javax.swing.JLabel lblDatoAlumno;
private javax.swing.JLabel lblMatriculaA;
private javax.swing.JLabel lblMontoMatricula;
private javax.swing.JLabel lblNombre;
private javax.swing.JLabel lblResumenMatricula;
private javax.swing.JTextField txtApellido;
private javax.swing.JTextField txtCostoCredito;
private javax.swing.JTextField txtCreditos;
private javax.swing.JTextField txtMontoMatricula;
private javax.swing.JTextField txtNombre;
private javax.swing.JTextArea txtResumen;
// End of variables declaration

}

    
asked by Andres Tapia 29.11.2016 в 04:02
source

1 answer

1

It is assumed that (line 103)

cboCiclo.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        cboCicloActionPerformed(evt);
    }
});

performs the "listening" event (ActionListener) and delegates the event method cboCicloActionPerformed()...

If you look at the implementation of this method, you will find that you are not doing anything:

private void cboCicloActionPerformed(java.awt.event.ActionEvent evt) {

}

The logic that updates the txtcredito(Credito) y txtcostocredito(CostoCredito) is in:

txtCreditosActionPerformed(...

line 288

there should be the problem ...

Suggestion

The keyword import serves precisely to remove as much clutter and "re-call" to the same class multiple times.

instead of having for example:

new javax.swing.JPanel(); // ocupa "javax.swing." de más

The correct form should be:

package xxxxx;

import javax.swing.JPanel;

and in your case, it would be better still:

import javax.swing.*;

and remove all javax.swing.... that makes the code look horrible.

    
answered by 29.11.2016 в 04:43