Results 1 to 6 of 6
- 07-19-2011, 01:06 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
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
I hope you can help me out guys, im really new to swing...PHP 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()); } }
This is the final code:
Thanks!PHP 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()));
- 07-19-2011, 02:02 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Table Button Column « Java Tips Weblog shows another approach to this problem.
- 07-19-2011, 02:08 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
- 07-19-2011, 04:11 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Thanks, I actually followed that post too...but didnt work either, the only thing i could do was a mouse event, but the problem was that it triggered on each buttom the same action and not an individual action that im looking for.
Is there another way?
- 07-19-2011, 05:16 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Use the "Code" tags not the "PHP" tags for Java code.
What do you mean you want a different Action? How do you determine which Action is to be performed on a given row?
- 08-02-2011, 08:07 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Icon wont change on a JButton
By Bagzli in forum New To JavaReplies: 9Last Post: 05-15-2011, 10:48 PM -
JButton action
By jperson in forum New To JavaReplies: 3Last Post: 03-24-2010, 06:47 PM -
JButton Action Listner
By 2o2 in forum AWT / SwingReplies: 19Last Post: 10-11-2008, 01:07 AM -
Few action in one Jbutton
By kubiasty in forum New To JavaReplies: 0Last Post: 07-25-2007, 10:19 AM -
actionErrors inside of Action
By Jack in forum JDBCReplies: 2Last Post: 07-04-2007, 03:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks