Results 1 to 6 of 6
  1. #1
    giskard is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default 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
    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()); 
     }
    }
    I hope you can help me out guys, im really new to swing...
    This is the final code:

    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()));
    Thanks!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Table Button Column Java Tips Weblog shows another approach to this problem.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

  4. #4
    giskard is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    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?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    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?

  6. #6
    giskard is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    The problem was my table model...

    @Override
    public boolean isCellEditable(int row, int column) {
    return true;
    }

    Thanks

Similar Threads

  1. Icon wont change on a JButton
    By Bagzli in forum New To Java
    Replies: 9
    Last Post: 05-15-2011, 10:48 PM
  2. JButton action
    By jperson in forum New To Java
    Replies: 3
    Last Post: 03-24-2010, 06:47 PM
  3. JButton Action Listner
    By 2o2 in forum AWT / Swing
    Replies: 19
    Last Post: 10-11-2008, 01:07 AM
  4. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 10:19 AM
  5. actionErrors inside of Action
    By Jack in forum JDBC
    Replies: 2
    Last Post: 07-04-2007, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •