I commented to them about a problem that came up, I decided to create a JTablePersonalizado, to facilitate the change of colors of cells, of the heading, that the text is centered, but my problem is that when I obtain, the model of the table, so that I can recognize a change of some value in some cell, I launch an event.
This would do it like this: tabla.getModel.addTableModelListener (this);
The problem is that in doing so, it does not detect events, whereas if I do it with a normal JTable, it does.
NOTE: The DefaultTableModel, I also modify it in another class so that only some cells can be edited.
JTablePersonalizado.java
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public final class JTablePersonalizado extends JTable{
private static Color COLOR_IMPARES = new Color(153,153,255);
private static Color COLOR_PARES = new Color(204,204,255);
private static Color COLOR_FONDO_ENCABEZADO = new Color(247, 216, 62);
private static Color COLOR_TEXTO_ENCABEZADO = new Color(254, 254, 254);
private static Color COLOR_FILA_SELECCIONADA = new Color(222, 246, 250);
public JTablePersonalizado(){
this.setAutoCreateColumnsFromModel(true);
this.setAutoscrolls(true);
this.setVisible(true);
FilasRenderer filasRenderer = new FilasRenderer(2);
this.setDefaultRenderer(Object.class, filasRenderer);
this.getTableHeader().setReorderingAllowed(false);
this.setFillsViewportHeight(true);
this.getTableHeader().setBackground(COLOR_FONDO_ENCABEZADO);
this.getTableHeader().setForeground(COLOR_TEXTO_ENCABEZADO);
}
private class FilasRenderer extends DefaultTableCellRenderer{
private final int nFila;
public FilasRenderer(int nFila){
this.nFila = nFila;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
this.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
this.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12));
if ( row %2 == 0){
if ( isSelected ){
this.setBackground(COLOR_FILA_SELECCIONADA);
}else{
this.setBackground(COLOR_PARES);
}
}else{
if ( isSelected ){
this.setBackground(COLOR_FILA_SELECCIONADA);
}else{
this.setBackground(COLOR_IMPARES);
}
}
return this;
}
}
private class EncabezadoRenderer extends DefaultTableCellRenderer{
public EncabezadoRenderer() {
super();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
comp.setBackground(COLOR_FONDO_ENCABEZADO);
comp.setForeground(COLOR_TEXTO_ENCABEZADO);
this.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(14, 14, 162)));
comp.setFont(new java.awt.Font("Tahoma", 1, 11));
return comp;
}
}
}
}
MyModelTabla.java
import javax.swing.table.DefaultTableModel;
public class MiModeloTabla extends DefaultTableModel{
public static int EDITAR_COLUMNA = -1;
private int columnaEditable;
private boolean permitirEditable;
/**
* Constructor vacio, que no permitirá que ninguna celda pueda editarse.
*/
public MiModeloTabla() {
super();
permitirEditable = false;
}
/**
* Constructor que recibe el EDITAR_COLUMNA, solo para indicar que se podra
* editar alguna columna, luego debes hacer uso del método setColumnaEditable
* para indicar que columna desea que sea la editable.
* @param editar
*/
public MiModeloTabla(int editar) {
permitirEditable = true;
this.columnaEditable = editar;
}
/**
* Metodo sobreescrito, para permitir o no que celdas podrian editarse
* @param row
* @param column
* @return
*/
@Override
public final boolean isCellEditable(int row, int column) {
if ( permitirEditable == false ){
return false;
}else if (permitirEditable == true ){
if ( column == columnaEditable ){
return true;
}
}
return false;
}
/**
* Indica el numero de columna que sea editable, recuerde que en la tabla
* la primera celda es 0
* @param columnaEditable
*/
public void setColumnaEditable(int columnaEditable) {
this.columnaEditable = columnaEditable;
}
}