Jtable单元格上的Jbutton只能运行一次

时间:2010-12-16 13:26:42

标签: java swing jtable jbutton

我对Java Swing很新,并且在Jtable单元格上遇到了Jbutton的一些问题。

问题是Jbutton只工作一次然后它不起作用。 第一次,当我再次点击时没有任何反应,getTableCellEditorComponent只被调用一次。 我希望它作为常规按钮工作。

我的按钮编辑器和按钮渲染器类如下:

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;
          }
        }

3 个答案:

答案 0 :(得分:1)

通常当我必须这样做时,我会参考http://download.oracle.com/javase/6/docs/api/然后转到页面左侧的JTable,在JTable javadoc页面的开头有一个指向“如何使用表”的链接。底部附近是如何将按钮放在JTable中的示例。看起来该页面中的部分是“使用其他编辑器”。我通常遵循SUN指定的内容。希望这有点帮助...

答案 1 :(得分:1)

  

我想让它作为常规按钮工作。

如果这意味着你想在按下按钮时调用一个Action,那么你可以查看Table Button Column一种方法。

答案 2 :(得分:0)

尝试Esc然后再次点击。如果现在有效,这是一个焦点问题 不要解决此问题,请关注camickr's answerspin-off

基本大纲应该是这样的:

渲染器:

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;
}
}

编辑:

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;
}
}

用法:

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