Boa Tarde pessoal, to meio desesperado aqui, a dois dias estou tentando colocar um campo Data em uma Jtable… e está muito dificil, estou com problemas no CellEditor…
Então eu vim até aqui para ver se alguem n teria um CellEditor de Data para me mostrar… ou pelo menos me dizer quais os metodos que eu teria a nessecidade de implementar, estou8 com dificuldade pq eu tenho o meu proprio modelo e o meu proprio CellRenderer…
Vou postar os codigos aqui se alguem poder ajudar eu vou ficar muito feliz!!
package model.tablemodel;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
public class DemostrativoCellEditor extends DefaultCellEditor {
MaskFormatter data;
DateFormat format;
Date value;
public DemostrativoCellEditor(){
super(new JFormattedTextField(getDateMask()));
}
public static MaskFormatter getDateMask(){
MaskFormatter data= null;
try{
data = new MaskFormatter("##/##/####");
data.setPlaceholderCharacter('_');
}
catch(ParseException excp){
}
return data;
}
}
package model.tablemodel;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
public class DemostrativoEmprestimosTableModel extends AbstractTableModel {
private Object [][]dados;
public DemostrativoEmprestimosTableModel(Object [][]dados){
if(dados == null)
this.dados = new Object[5][11];
else
this.dados = dados;
}
public int getColumnCount() {
return 11;
}
public String getColumnName(int coluna) {
switch(coluna){
case 0:return "Taxa de \nJuros";
case 1:return " \nVencto.";
case 2:return " \nGarantia";
case 3:return "Valor \nContratado";
case 4:return "Saldo \nAtual";
case 5:return "Valores \nVencidos";
case 6:return "A vencer \nCurto Prazo";
case 7:return "A vencer \nLongo Prazo";
case 8:return " \nPMT";
case 9:return " \nDestinação";
case 10:return " \nObservações";
default:return "";
}
}
public int getRowCount() {
return 5;
}
public Class getColumnClass(int columnIndex) {
switch(columnIndex){
case 0:return Double.class;
case 1:return Date.class;
case 2:return String.class;
case 3:return Double.class;
case 4:return Double.class;
case 5:return Double.class;
case 6:return Double.class;
case 7:return Double.class;
case 8:return String.class;
case 9:return String.class;
case 10:return String.class;
default:return Object.class;
}
}
public Object getValueAt(int row, int col) {
return dados[row][col];
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int column) {
if(value != null){
if(column==0){
if((Double)value <= 100)
dados[row][column] = value;
}
if(column == 1){
System.out.println("VALOR INICIAL"+value);
if(value.equals("__/__/____")){
dados[row][column] = null;
value = null;
}else{
dados[row][column] = value;
Date date = converteData(value);
dados[row][column] = (Date)date;
}
}
}else{
dados[row][column] = null;
}
fireTableCellUpdated(row,column);
/*dados[row][column] = value;
fireTableCellUpdated(row,column);*/
}
private Date converteData(Object dataAntiga){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
formatter.setLenient(false);
Date date = null;
try {
date = formatter.parse((String)dataAntiga);
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, "DATA INCORRETA");
date = null;
}
return date;
}
}
package model.tablemodel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.SoftBevelBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.text.MaskFormatter;
public class DemostrativoEmprestimosCellRenderer extends DefaultTableCellRenderer{
private static final long serialVersionUID = 1L;
private NumberFormat formatter;
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setFont(new java.awt.Font("Lucida Sans", 0, 12 ));
setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
setBackground(new Color(244, 242, 228));
if (hasFocus)
setBackground(new Color(230,230,250));
if(value != null){
if(column==0){
formatter = NumberFormat.getPercentInstance();
formatter.setMaximumIntegerDigits(3);
formatter.setMaximumFractionDigits(1);
Double porcentagem = (Double)value/100;
if(porcentagem <= 1)
setText(formatter.format(porcentagem));
else{
setText("");
}
setHorizontalAlignment(SwingConstants.CENTER);
}else if(column==1){
DateFormat formatter = DateFormat.getDateInstance();
setText(formatter.format(value) );
}else{
formatter = NumberFormat.getCurrencyInstance();
formatter.setMaximumFractionDigits(2);
setText(formatter.format(value));
setFont(new Font("helvetica",Font.ROMAN_BASELINE,11));
setHorizontalAlignment(SwingConstants.RIGHT);
}
}else{
setText("");
}
return this;
}
}