user544799
user544799

Reputation: 105

Jbutton on a Jtable cell works only once

I am quite new with Java Swing and have some problems with Jbutton on a Jtable cell.

The problem is Jbutton works only once and then it is not functioning. After first time, when I click on again nothing happens, getTableCellEditorComponent is called only once. I want it to work as a regular button.

My Button editor and button renderer classes are as follows:

package buttonontable;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.HashMap;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;


 public class MyButtonEditor extends JButton implements TableCellEditor {
  protected JButton button;
  ButtonOnJtableAction buttonOnJtableAction;

  String labelStr;




  public MyButtonEditor(String buttonStr,ButtonOnJtableAction _buttonOnJtableAction) {
    super(buttonStr);
    buttonOnJtableAction = _buttonOnJtableAction;
    button = new JButton();
    button.setOpaque(true);
    System.out.println("MyButtonEditor constructed");

     }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {


        System.out.println("getTableCellEditorComponent");
        JOptionPane.showMessageDialog(table, "Pressed at " + row + "x" + column);
        buttonOnJtableAction.buttonPressed(table,row,column);
        return this;
    }

    public void cancelCellEditing() {
        System.out.println("cancelCellEditing");
        System.out.println("Cancel");
    }

    public boolean stopCellEditing() {
        System.out.println("stopCellEditing");
        return true;
    }

    public Object getCellEditorValue() {
        System.out.println("getCellEditorValue");
        return null;
    }

    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    public void addCellEditorListener(CellEditorListener l) {
    }

    public void removeCellEditorListener(CellEditorListener l) {
    }

    protected void fireCellEditing(ChangeEvent e){

    }

}



    package buttonontable;

    import java.awt.Component;
    import javax.swing.JButton;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.table.TableCellRenderer;


        public class MyButtonRenderer extends JButton implements TableCellRenderer {

          String buttonStr;
          public MyButtonRenderer(String _buttonStr) {
            setOpaque(true);
            buttonStr = _buttonStr;
            setText(buttonStr);
          }

          public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {

           System.out.println("getTableCellRendererComponent");

            if (isSelected) {
              setForeground(table.getSelectionForeground());
              setBackground(table.getSelectionBackground());
            } else{
              setForeground(table.getForeground());
              setBackground(UIManager.getColor("Button.background"));
            }
            setText( (value ==null) ? buttonStr : value.toString() );
            return this;
          }
        }

Upvotes: 1

Views: 3104

Answers (3)

Asaf
Asaf

Reputation: 2520

Try to Esc and click again. If it works now, it's a focus issue.
Don't fix that, follow camickr's answer or a spin-off of it.

A basic outline should like this:

Renderer:

class MyClassCellRenderer implements TableCellRenderer {

private final JButton button;

public MyClassCellRenderer() {
  button = new JButton();
  button.setFocusable(false);
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,        boolean isSelected, boolean hasFocus, int row, int column) {
  button.setText(((Contact)value).getName());
  return button;
}
}

Editor:

class MyClassCellEditor extends AbstractCellEditor implements TableCellEditor {
private final JButton button;

public MyClassCellEditor() {
  button = new JButton(new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
      logger.trace("I");
    }
  });
  button.setFocusable(false);
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value,      boolean isSelected, int row, int column) {
  button.setText(((Contact)value).getName());
  return button;
}

@Override
public Object getCellEditorValue() {
  return null;
}
}

Usage:

model.getColumn(0).setCellRenderer(new MyClassCellRenderer());
model.getColumn(0).setCellEditor(new MyClassCellEditor());

Upvotes: 0

camickr
camickr

Reputation: 324118

I want it to work as a regular button.

If that means you want to invoke an Action when the button is pressed then you can check out Table Button Column for one approach.

Upvotes: 1

Merky
Merky

Reputation: 494

Usually when I have to do this I refer to http://download.oracle.com/javase/6/docs/api/ then go to JTable on the left of the page and at the beginning of the JTable javadoc page there is a link to "How to use Tables". Near the bottom is an example of how to put a button in a JTable. Looks like the section in that page is "Using Other Editors". I usually follow what SUN specified. Hopefully that helps a bit...

Upvotes: 1

Related Questions