Jbutton inside a jtable wont fire action
Hi guys I have a big problem...Im trying to fire an action for a button inside a table, the problem....the button wont fire anything....
I just copyed a jtable button example and adapted it to my model.
I hope you can help me guys, thanks a lot!
the editor
Code:
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(label + ": Ouch!");
fireEditingStopped();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
return button;
}
public Object getCellEditorValue() {
if (isPushed) {
//
//
JOptionPane.showMessageDialog(button, label + ": Ouch!");
System.out.println(label + ": Ouch!");
}
isPushed = false;
return new String(label);
}
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
public class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText((value == null) ? "" : value.toString());
return this;
}
}
import java.util.List;
import javax.swing.table.AbstractTableModel;
import bean.EstructuraLog;
public class ModeloTablaFiles extends AbstractTableModel {
final String[] columnNames = {"Fecha Hora Inicio", "Fecha Hora Fin","Duracion",
"Nombre Archivo","Ruta Archivo","Resultado","Info"};
private List<EstructuraLog> listaEstructurada;
public ModeloTablaFiles(List<EstructuraLog> listaEstructurada){
this.listaEstructurada = listaEstructurada;
}
public ModeloTablaFiles(){
this.listaEstructurada= null;
}
public String getColumnName(int col) {
return columnNames[col];
}
public boolean isCellEditable() {
return false;
}
public int getRowCount() {
return listaEstructurada.size();
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex)
{
case 0:
return this.listaEstructurada.get(rowIndex).getFechaInicio();
case 1:
return this.listaEstructurada.get(rowIndex).getFechaFin();
case 2:
return this.listaEstructurada.get(rowIndex).getDuracion();
case 3:
return this.listaEstructurada.get(rowIndex).getNombreArchivo();
case 4:
return this.listaEstructurada.get(rowIndex).getRutaArchivo();
case 5:
return this.listaEstructurada.get(rowIndex).getResultado();
default:
return null;
}
}
public Class getColumnClass(int c) {
//return getValueAt(0, c).getClass();
Object value=this.getValueAt(0,c);
return (value==null?Object.class:value.getClass());
}
}
I hope you can help me out guys, im really new to swing...
This is the final code:
Code:
ModeloTablaFiles mimodelo = new ModeloTablaFiles(
listaEstructuraLog);
tbProceso.removeAll();
tbProceso = new JTable(mimodelo);
tbProceso.getColumn("Info").setCellRenderer(new test.ButtonRenderer());
tbProceso.getColumn("Info").setCellEditor(new test.ButtonEditor(new JCheckBox()));
Thanks!