Creacion de number formated text field y implementacion de prueba como campos de efectivo
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2018-2019 Daniel Cortes
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package danielcortes.xyz.views.components;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
|
||||
public class NumberFormatedTextField extends JTextField {
|
||||
private int value;
|
||||
private NumberFormat nf;
|
||||
|
||||
public NumberFormatedTextField() {
|
||||
super();
|
||||
|
||||
this.nf = NumberFormat.getIntegerInstance();
|
||||
|
||||
this.addFocusListener(new FieldFocusListener());
|
||||
this.addKeyListener(new FieldKeyAdapter());
|
||||
}
|
||||
|
||||
public void setValue(int value){
|
||||
this.value = value;
|
||||
this.setText(nf.format(value));
|
||||
}
|
||||
|
||||
public int getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
private void readValue(){
|
||||
try {
|
||||
String currentText = this.getText();
|
||||
if(currentText.length() > 0){
|
||||
this.value = nf.parse(currentText).intValue();
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void formatText(){
|
||||
this.setText(nf.format(this.value));
|
||||
}
|
||||
|
||||
private class FieldFocusListener implements FocusListener{
|
||||
/**
|
||||
* Selecciona todo al momento de ganar foco
|
||||
*/
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
NumberFormatedTextField.this.select(0, getText().length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Actualiza el texto y el valor interno al perder el foco
|
||||
*/
|
||||
@Override
|
||||
public void focusLost(FocusEvent e) {
|
||||
NumberFormatedTextField.this.readValue();
|
||||
NumberFormatedTextField.this.formatText();
|
||||
}
|
||||
}
|
||||
|
||||
private class FieldKeyAdapter extends KeyAdapter{
|
||||
|
||||
/**
|
||||
* Solo permitir introducir un maximo de 9 digitos
|
||||
*/
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
String currentText = NumberFormatedTextField.this.getText();
|
||||
int parsedText = 0;
|
||||
|
||||
if(currentText.length() > 0) {
|
||||
try {
|
||||
parsedText = nf.parse(currentText).intValue();
|
||||
} catch (ParseException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
int length = String.valueOf(parsedText).length();
|
||||
|
||||
if (length >= 9) {
|
||||
e.consume();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user