/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package frames;
import modelo.Producto;
import vista.SwingValidator;
import javax.swing.table.DefaultTableModel;
public class MantProd extends javax.swing.JFrame {
String cols[] = {"Código", "Descripción", "Precio"};
String datos[][];
DefaultTableModel mod = new DefaultTableModel(datos, cols);
private SwingValidator sv = new SwingValidator();
String accion; // agregar, actualizar o eliminar
boolean combo; // activación del combobox
/**
* Creates new form MantProd
*/
public MantProd() {
initComponents();
productos.setModel(mod);
setDatosMode(false);
setAgrMode();
}
/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
codigo = new javax.swing.JTextField();
precio = new javax.swing.JTextField();
descripcion = new javax.swing.JTextField();
seleccionar = new javax.swing.JComboBox<>();
actualizar = new javax.swing.JButton();
eliminar = new javax.swing.JButton();
aceptar = new javax.swing.JButton();
cancelar = new javax.swing.JButton();
agregar = new javax.swing.JButton();
mensaje = new javax.swing.JLabel();
marco = new javax.swing.JScrollPane();
productos = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Mantenimiento de Productos");
setFont(new java.awt.Font("aakar", 0, 20)); // NOI18N
jLabel1.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
jLabel1.setText("Código :");
jLabel2.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
jLabel2.setText("Descripción :");
jLabel3.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
jLabel3.setText("Precio :");
jLabel4.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
jLabel4.setText("producto (descripción): ");
codigo.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
codigo.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
codigoKeyTyped(evt);
}
});
precio.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
precio.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
precioKeyTyped(evt);
}
});
descripcion.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
descripcion.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
descripcionKeyTyped(evt);
}
});
seleccionar.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
seleccionar.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
seleccionarItemStateChanged(evt);
}
});
actualizar.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
actualizar.setText("Actualizar");
actualizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actualizarActionPerformed(evt);
}
});
actualizar.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
actualizarKeyPressed(evt);
}
});
eliminar.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
eliminar.setText("Eliminar");
eliminar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
eliminarActionPerformed(evt);
}
});
eliminar.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
eliminarKeyPressed(evt);
}
});
aceptar.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
aceptar.setText("Aceptar");
aceptar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aceptarActionPerformed(evt);
}
});
cancelar.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
cancelar.setText("Cancelar");
cancelar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelarActionPerformed(evt);
}
});
agregar.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
agregar.setText("Agregar");
agregar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
agregarActionPerformed(evt);
}
});
agregar.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
agregarKeyPressed(evt);
}
});
mensaje.setFont(new java.awt.Font("Liberation Sans", 0, 20)); // NOI18N
mensaje.setForeground(new java.awt.Color(255, 51, 51));
marco.setBorder(javax.swing.BorderFactory.createTitledBorder("Productos"));
productos.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
marco.setViewportView(productos);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(seleccionar, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(precio, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(agregar)
.addGap(18, 18, 18)
.addComponent(actualizar)
.addGap(18, 18, 18)
.addComponent(eliminar)
.addGap(18, 18, 18)
.addComponent(aceptar)
.addGap(26, 26, 26)
.addComponent(cancelar))
.addComponent(marco, javax.swing.GroupLayout.PREFERRED_SIZE, 586, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(264, 264, 264)
.addComponent(mensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(33, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(seleccionar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(precio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addComponent(mensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(agregar)
.addComponent(cancelar)
.addComponent(aceptar)
.addComponent(actualizar)
.addComponent(eliminar))
.addGap(18, 18, 18)
.addComponent(marco, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarActionPerformed
setDatosMode(true);
seleccionar.setEnabled(false);
seleccionar.setVisible(false);
codigo.requestFocusInWindow();
actualizar.setEnabled(false);
eliminar.setEnabled(false);
aceptar.setEnabled(true);
cancelar.setEnabled(true);
accion = "agr";
mensaje.setText("Agregando");
agregar.setEnabled(false);
combo = false;
}//GEN-LAST:event_agregarActionPerformed
private void setDatosMode(boolean b){
seleccionar.setEnabled(b);
codigo.setEnabled(b);
descripcion.setEnabled(b);
precio.setEnabled(b);
agregar.requestFocusInWindow();
}
private void setAgrMode() {
agregar.setEnabled(true);
actualizar.setEnabled(false);
eliminar.setEnabled(false);
aceptar.setEnabled(false);
}
private void clearFields() {
codigo.setText("") ;
descripcion.setText("");
precio.setText("");
}
private void fillFields(Producto p) {
codigo.setText(p.getCodigo()) ;
descripcion.setText(p.getDescripcion()) ;
precio.setText(p.getFormattedPrecio());
}
private boolean isValidData(){
return sv.isPresent(codigo, "Código") &&
sv.isPresent(descripcion, "Descripcion") &&
sv.isPresent(precio, "Precio") &&
sv.isFloat(precio, "Precio");
}
private Producto getProducto() { // obtiene producto de los datos
Producto p = new Producto();
p.setCodigo(codigo.getText());
p.setDescripcion(descripcion.getText());
p.setFormattedPrecio(precio.getText());
return p;
}
private void selectProducto(Producto p) {seleccionar.setSelectedItem(p);}
private Producto getCurrentProducto() {
int i = seleccionar.getSelectedIndex();
if(i>-1) return getProducto(i);
Producto p = null;
return p;
}
private void showProducto(Producto p) {
codigo.setText(p.getCodigo());
descripcion.setText(p.getDescripcion());
precio.setText(p.getFormattedPrecio());
}
private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
mensaje.setText("");
setDatosMode(false);
clearFields();
agregar.setEnabled(true);
actualizar.setEnabled(true);
eliminar.setEnabled(true);
aceptar.setEnabled(false);
cancelar.setEnabled(false);
agregar.requestFocusInWindow();
}//GEN-LAST:event_cancelarActionPerformed
private void actualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actualizarActionPerformed
seleccionar.setEnabled(true);
seleccionar.setVisible(true);
agregar.setEnabled(false);
eliminar.setEnabled(false);
aceptar.setEnabled(false);
cancelar.setEnabled(true);
accion = "act";
mensaje.setText("Actualizando");
actualizar.setEnabled(false);
combo = true;
if(seleccionar.getItemCount()>0){
seleccionar.setSelectedIndex(0);
Producto p = getProducto(0);
codigo.setText(p.getCodigo());
descripcion.setText(p.getDescripcion());
precio.setText(p.getFormattedPrecio());
descripcion.requestFocusInWindow();
precio.setEnabled(true);
descripcion.setEnabled(true);
aceptar.setEnabled(true);
}
}//GEN-LAST:event_actualizarActionPerformed
private void add(Producto p){
String[] fila = {p.getCodigo(), p.getDescripcion(), p.getFormattedPrecio()};
mod.addRow(fila);
}
private void update(Producto p){
int n = -1;
String cod = p.getCodigo();
for(int i=0; i<mod.getRowCount();i++){
n++;
if( mod.getValueAt(i, 0).equals(cod)) {
mod.setValueAt(p.getDescripcion(), n, 1);
mod.setValueAt(p.getFormattedPrecio(), n, 2);
break;
}
}
}
private void delete(int i){
mod.removeRow(i);
}
private Producto getProducto(int i){ // lee producto de la tabla
return new Producto((String)mod.getValueAt(i, 0),
(String)mod.getValueAt(i, 1),
Float.parseFloat((String)mod.getValueAt(i, 2)));
}
void fillSeleccionar(){
seleccionar.removeAllItems();
for(int i=0; i<mod.getRowCount();i++)
seleccionar.addItem((String)mod.getValueAt(i, 1));
}
private void aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarActionPerformed
combo = false;
mensaje.setText("");
if(accion.equals("eli")){
int indice = seleccionar.getSelectedIndex();
seleccionar.removeItemAt(indice);
delete(indice);
}else{
if (isValidData()) {
Producto p = getProducto();
if (accion.equals("agr")){
seleccionar.addItem(p.getDescripcion());
add(p);
} else {
update(p);
fillSeleccionar();
}
} else return;
}
seleccionar.setSelectedItem(0);
clearFields();
setDatosMode(false);
seleccionar.setEnabled(false);
agregar.setEnabled(true);
if(seleccionar.getItemCount()==0){
actualizar.setEnabled(false);
eliminar.setEnabled(false);
} else {
actualizar.setEnabled(true);
eliminar.setEnabled(true);
}
aceptar.setEnabled(false);
cancelar.setEnabled(false);
agregar.requestFocusInWindow();
}//GEN-LAST:event_aceptarActionPerformed
private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarActionPerformed
seleccionar.setEnabled(true);
seleccionar.setVisible(true);
agregar.setEnabled(false);
actualizar.setEnabled(false);
aceptar.setEnabled(false);
cancelar.setEnabled(true);
accion = "eli";
eliminar.setFocusable(false);
mensaje.setText("Eliminando");
eliminar.setEnabled(false);
combo = true;
if(seleccionar.getItemCount()>0){
seleccionar.setSelectedIndex(0);
Producto p = getProducto(0);
codigo.setText(p.getCodigo());
descripcion.setText(p.getDescripcion());
precio.setText(p.getFormattedPrecio());
aceptar.setEnabled(true);
}
}//GEN-LAST:event_eliminarActionPerformed
private void codigoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_codigoKeyTyped
if (evt.getKeyChar()==10) descripcion.requestFocusInWindow();
}//GEN-LAST:event_codigoKeyTyped
private void descripcionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descripcionKeyTyped
if (evt.getKeyChar() ==10) precio.requestFocusInWindow();
}//GEN-LAST:event_descripcionKeyTyped
private void precioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_precioKeyTyped
char c = evt.getKeyChar();
if (evt.getKeyChar()!='0' && c!='1' && c!='2' && c!='3' && c!='4' && c!='5' && c!='6' &&
c!='7' && c!='8' && c!='9' && c!='.' && c!='+' && c!='-') evt.consume();
if (c ==10) aceptar.requestFocusInWindow(); // TODO add your handling code here:
}//GEN-LAST:event_precioKeyTyped
private void seleccionarItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_seleccionarItemStateChanged
if (!combo) return;
Producto p = getProducto(seleccionar.getSelectedIndex());
fillFields(p);
agregar.setEnabled(false);
aceptar.setEnabled(true);
if (accion.equals("act")) {
descripcion.setEnabled(true);
precio.setEnabled(true);
eliminar.setEnabled(false);
descripcion.requestFocusInWindow();
} else{
actualizar.setEnabled(false);
aceptar.requestFocusInWindow();
}
}//GEN-LAST:event_seleccionarItemStateChanged
private void agregarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_agregarKeyPressed
if(evt.getKeyCode()==39) actualizar.requestFocusInWindow();
}//GEN-LAST:event_agregarKeyPressed
private void eliminarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_eliminarKeyPressed
if(evt.getKeyCode()==37) actualizar.requestFocusInWindow();
}//GEN-LAST:event_eliminarKeyPressed
private void actualizarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_actualizarKeyPressed
eliminar.setFocusable(true);
if(evt.getKeyCode()==39) eliminar.requestFocusInWindow();
if(evt.getKeyCode()==37) agregar.requestFocusInWindow();
}//GEN-LAST:event_actualizarKeyPressed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MantProd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MantProd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MantProd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MantProd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MantProd().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton aceptar;
private javax.swing.JButton actualizar;
private javax.swing.JButton agregar;
private javax.swing.JButton cancelar;
private javax.swing.JTextField codigo;
private javax.swing.JTextField descripcion;
private javax.swing.JButton eliminar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane marco;
private javax.swing.JLabel mensaje;
private javax.swing.JTextField precio;
private javax.swing.JTable productos;
private javax.swing.JComboBox<String> seleccionar;
// End of variables declaration//GEN-END:variables
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package modelo;
import java.text.NumberFormat;
/**
*
* @author carlos
*/
public class Producto {
String codigo;
String descripcion;
float precio;
public Producto(){}
public Producto(String codigo, String descripcion, float precio){
this.codigo = codigo;
this.descripcion = descripcion;
this.precio = precio;
}
public String getCodigo(){return codigo;}
public String getDescripcion(){return descripcion;}
public float getPrecio(){return precio;}
public String getFormattedPrecio(){return NumberFormat.getNumberInstance().format(precio);}
public void setCodigo(String codigo){this.codigo = codigo;}
public void setDescripcion(String descripcion){this.descripcion = descripcion;}
public void setPrecio(float precio){this.precio = precio;}
public void setFormattedPrecio(String precio){this.precio = Float.parseFloat(precio);}
public String toString(){
return codigo + "\t" + descripcion + "\t" + NumberFormat.getNumberInstance().format(precio);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vista;
import javax.swing.* ;
import javax.swing.text.JTextComponent;
/**
*
* @author carlos
*/
public class SwingValidator {
public boolean isPresent(JTextComponent c, String fieldName){
if (c.getText().length() == 0) {
showMessage(c, fieldName + " es un campo requerido.");
c.requestFocusInWindow() ;
return false;
}
return true;
}
public boolean isInteger(JTextComponent c, String fieldName){
try{
int i = Integer.parseInt(c.getText());
return true;
} catch (NumberFormatException e){
showMessage(c, fieldName + " debe ser entero.");
c.requestFocusInWindow() ;
return false;
}
}
public boolean isFloat(JTextComponent c, String fieldName) {
try{
float f = Float.parseFloat(c.getText());
return true;
} catch (NumberFormatException e){
showMessage(c, fieldName + " debe ser un número válido.");
c.requestFocusInWindow() ;
return false;
}
}
private void showMessage(JTextComponent c, String message){
JOptionPane.showMessageDialog(c, message);
}
}