Jtable 单元格上的 Jbutton 只能工作一次

发布于 2024-10-08 02:47:25 字数 3347 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

摘星┃星的人 2024-10-15 02:47:25

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

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...

走野 2024-10-15 02:47:25

我希望它像常规按钮一样工作。

如果这意味着您想在按下按钮时调用操作,那么您可以查看 表格按钮列是一种方法。

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.

无畏 2024-10-15 02:47:25

尝试按 Esc 并再次单击。如果现在有效,那就是焦点问题。
不要解决这个问题,请按照 camickr 的回答 或其衍生产品

基本大纲应该是这样的:

渲染器:

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

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文