ola pessoal, alguem usa alguma classe que substitua o JTextField com vantagens??
eu não achei então eu fiz uma propria, vou lançar aqui o codigo, gostaria de sugestões para melhoria e recursos, visto que sou iniciante.
Abraços!!
MYTEXTFIELD
Code:
**
* @file MyTextFile
* @author Darlan Oliveira
* @date Created on Nov 25, 2004
*/
package br.ind.freedom;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JTextField;
/**
* Classe extendida de JTextField usada como
* substituta do JTextField, uma vez que tem
* varios recursos extras
*/
public class MyTextField extends JTextField{
/**
* @param texto define Texto inicial da objeto
* @param maxLenght define numero maximo de caracteres aceitos
* @param format define o formato do objeto
*/
public MyTextField(String texto, int maxLenght, String format) {
super(texto);
this.maxLenght= maxLenght;
this.format = format;
addFocusListener(new Focu());
addKeyListener(new Key());
}
/**
* @param width define a largura do objeto
* @param maxLenght define numero maximo de caracteres aceitos
*/
public MyTextField(int width, int maxLenght) {
super("");
this.maxLenght=maxLenght;
setColumns(width);
addFocusListener(new Focu());
addKeyListener(new Key());
}
/**
* metodo construtor
*
* @param texto define Texto inicial da objeto
* @param maxLenght define numero maximo de caracteres aceitos
*/
public MyTextField(String texto, int maxLenght) {
super(texto);
this.maxLenght=maxLenght;
setColumns(maxLenght);
addFocusListener(new Focu());
addKeyListener(new Key());
}
/**
* seta o numero maximo de caracteres para o objeto
* @param value numero maximo de caracteres aceitos
* @see #getMaxLenght
*/
public void setMaxLenght(int value){
maxLenght = value;
}
/**
* retorna o numero maximo de caracteres do objeto
* @return value tipo inteiro
* @see #setMaxLenght
*/
public int getMaxLenght(){
return maxLenght;
}
/**
* classe interna para eventos de teclado
*/
private class Key extends KeyAdapter {
public void keyTyped (KeyEvent e){
if (maxLenght > 0 && maxLenght <= getText().length() && e.getKeyChar() != 8 && e.getKeyChar() != KeyEvent.VK_DELETE){
e.consume();
}
if (format.toUpperCase().equals("T")){
if (Rotina.isTime(e) == false){
e.consume();
}
} else if (format.toUpperCase().equals("N")){
if (Rotina.isNumber(e) == false){
e.consume();
}
}
}
}
/**
* classe interna para eventos de focu
*/
private class Focu extends FocusAdapter {
public void focusGained(FocusEvent e){
selectAll();
corAnt= getBackground();
setBackground(Color.getHSBColor(70,70,70));
}
public void focusLost(FocusEvent e){
//hora
if (format.toUpperCase().equals("T")){
setText(Rotina.fmtTime(getText(),false));
}
setBackground(corAnt);
}
}
private String format="";
private int maxLenght=0;
private Color corAnt;
}
MYCOMBOFIELD
Code:
package br.ind.freedom;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JComboBox;
/**
* Classe extendida do JComboBox
* utilizada para vinculo dinamico com fonte de dados
*/
public class MyComboBox extends JComboBox {
/**
*
* @param table Nome da Tabela
* @param listField Nome do campo que ira aparecer na lista
* @param listIndex Nome do campo que ira servir como indice
*/
public MyComboBox(String table, String listField, String listIndex) {
super();
this.table=table;
this.fieldList = listField;
this.fieldIndex = listIndex;
fill();
addFocusListener(new Focu());
}
/**
* preenche combo apartir de uma fonte de dados
*/
private void fill(){
listIndex.clear();
String sQuery =
"SELECT " + fieldList + "," + fieldIndex +
" FROM " + table +
" ORDER BY " + fieldList;
ResultSet rs;
Conexao cnn= new Conexao();
rs = cnn.consulta (sQuery);
try {
for (int i=0;i< rs.getRow();i++){
addItem(rs.getString(fieldList));
listIndex.add(rs.getString(fieldIndex));
rs.next();
}
rs.close();
cnn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* seta o combo para a posicao desejada
* @param value valor do indice
*
* @see #getDBIndex
*/
public void setDBIndex(int value){
Integer ret=new Integer(0);
for (int i=0; i< getItemCount();i++){
ret = new Integer(listIndex.get(i).toString());
if (ret.intValue() == value){
setSelectedIndex(i);
}
}
}
/**
* retorna o valor do indice da posicao selecionada
* @return valor do tipo inteiro
*
* @see #setDBIndex
*/
public int getDBIndex(){
return new Integer(listIndex.get(getSelectedIndex()).toString()).intValue();
}
/**
* Classe interna para eventos de focu
*/
private class Focu extends FocusAdapter {
public void focusGained(FocusEvent e){
corAnt= getBackground();
setBackground(Color.getHSBColor(70,70,70));
}
public void focusLost(FocusEvent e){
setBackground(corAnt);
}
}
/*
* fieldList - campo listado
* valueList - valor do fieldList
* fieldIndex - campo indice
* valueIndex - valor do fieldIndex
*/
private String table, fieldList, fieldIndex, valueList;
private Vector listIndex = new Vector();
private Color corAnt;
}
