El comboBox tiene por nombre cmbTipo, el grid tblPro, el cuadro de texto txtPeso, las etiquetas txtSub, txtTax y txtTotal, el boton nuevo btnNuevo.
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class frmBotellas extends javax.swing.JFrame {
DefaultTableModel modelo=new DefaultTableModel();
public frmBotellas() {
initComponents();
String titu[]={"Tipo","Peso","$/lb","Importe"};
modelo.setColumnIdentifiers(titu);
tblPro.setModel(modelo);
}
private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {
int itipo=cmbTipo.getSelectedIndex();
String datos[]={"1","2","3","4"};
switch (itipo){
case 0:datos[0]="Aluminio";datos[2]="2.01";break;
case 1:datos[0]="Plastico CRV";datos[2]="1.01";break;
case 2:datos[0]="Metal";datos[2]="1.5";break;
}
datos[1]=txtPeso.getText();
datos[3]=Float.parseFloat(datos[2])*Float.parseFloat(datos[1])+"";
modelo.addRow(datos);
calTota();
}
private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {
cmbTipo.setSelectedIndex(-1);
txtPeso.setText(null);
cleanTable();
txtSub.setText(null);
txtTax.setText(null);
txtTotal.setText(null);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if (tblPro.getSelectedRow()>-1){
int i=tblPro.getSelectedRow();
modelo.removeRow(i);
calTota();
}else{JOptionPane.showMessageDialog(null, "Seleccione Fila");}
}
private void cleanTable(){
while (modelo.getRowCount()>0){
modelo.removeRow(modelo.getRowCount()-1);
}
}
private void calTota(){
float Sub=0;
float tax,total;
if (tblPro.getRowCount()>0){
for(int i=0;i<tblPro.getRowCount();i++){
Sub=Sub+Float.parseFloat(modelo.getValueAt(i, 3).toString());
}
}
txtSub.setText(redo(Sub)+"");
tax=(float) (Sub*0.09);
txtTax.setText(redo(tax)+"");
total=Sub+tax;
txtTotal.setText(redo(total)+"");
}
private float redo(float N){
return (float) (Math.rint(N*100)/100);
}