在单元格的JTable中启用/禁用Button,NullPointerException

时间:2017-07-19 04:40:48

标签: nullpointerexception cell tablecellrenderer tablecelleditor actionevent

我编写了自己的自定义对象类...我用我的自定义类填充我的JTable,实际上它就是一个像示例的按钮。

我想在第1行调用Button的ActionEvent ...我想启用(从第1个按钮行)并禁用(从第2个按钮行)第3行的按钮......

我不知道怎么做......

这是我的班级代码:

class CompButton extends JPanel {

  private final JButton intButton = new JButton();

  CompButton(ButtonInfo info) {
    super();
    setInfo(info);
    info.setOwner(this); // Another Try
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    add(intButton);
  }

  public ButtonInfo getInfo() {
    return new ButtonInfo(intButton.getText());
  }

  public void setEnableButton(boolean enable) {
    intButton.setEnabled(enable);
  }

  public void setInfo(ButtonInfo info) {
    for (ActionListener al : intButton.getActionListeners()) {
      intButton.removeActionListener(al);
    }
    if (info.getInfoText().toLowerCase().equals("red")) {
      intButton.setForeground(new Color(255, 0, 0));
      intButton.setText(info.getInfoText().toUpperCase());
      //Disable Blue Button, in Row 2 (3th row) or Cell(2,0)
      intButton.addActionListener((ActionEvent e) -> {
        System.out.println("Red Button Pressed");
        JTable table = (JTable)SwingUtilities.getAncestorOfClass(
            JTable.class, (Component) e.getSource());
        //table.getModel()).DisableButtonInCell(2,0)
        ButtonInfo binfo = (ButtonInfo)table.getModel().getValueAt(2, 0);
        //CompButton owner = binfo.getOwner();
        CompButton owner = new CompButton(binfo);
        owner.setEnableButton(false);
      });
    }
    if (info.getInfoText().toLowerCase().equals("green")) {
      intButton.setForeground(new Color(0, 196, 0));
      intButton.setText(info.getInfoText().toUpperCase());
      //Enable Blue Button, in Row 2 (3th row) or Cell(2,0)
      intButton.addActionListener((ActionEvent e) -> {
        System.out.println("green Button Pressed");
        JTable table = (JTable)SwingUtilities.getAncestorOfClass(
            JTable.class, (Component) e.getSource());
        //table.getModel()).EnableButtonInCell(2,0)
        ButtonInfo binfo = (ButtonInfo)table.getModel().getValueAt(2, 0);
        //CompButton owner = binfo.getOwner();
        CompButton owner = new CompButton(binfo);
        owner.setEnableButton(true);
      });
    }
    if (info.getInfoText().toLowerCase().equals("blue")) {
      intButton.setForeground(new Color(32, 32, 255));
      intButton.setText(info.getInfoText().toUpperCase());
      //DoClick Red Button, in Row 0 (1st row) or Cell(0,0)
      intButton.addActionListener((ActionEvent e) -> {
        System.out.println("blue Button Pressed");
        JTable table = (JTable)SwingUtilities.getAncestorOfClass(
            JTable.class, (Component) e.getSource());
        //table.getModel()).DoClickButtonInCell(0,0)
        ButtonInfo binfo = (ButtonInfo)table.getModel().getValueAt(0, 0);
        //CompButton owner = binfo.getOwner();
        CompButton owner = new CompButton(binfo);
        owner.intButton.doClick();
      });
    }
  }
}

在我的 makeUI 方法中,您可以看到,我的班级填充了表格

  public JComponent makeUI() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    JTable table = new JTable(new DefaultTableModel(
        new Object [][] {
          { new ButtonInfo("RED") },
          { new ButtonInfo("GREEN") },
          { new ButtonInfo("BLUE") }
        }, new String [] {" "}));
    table.getColumnModel().getColumn(0).setCellEditor(new EdtrRndr());
    table.getColumnModel().getColumn(0).setCellRenderer(new EdtrRndr());
    jscrollpane.setViewportView(table);
    panel.add(jscrollpane);
    return panel;
  }

我把所有代码都用来帮助我。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableWithButtons {
  final JScrollPane jscrollpane = new JScrollPane();
  static JFrame frame = new JFrame();

  public JComponent makeUI() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    JTable table = new JTable(new DefaultTableModel(
        new Object [][] {
          { new ButtonInfo("RED") },
          { new ButtonInfo("GREEN") },
          { new ButtonInfo("BLUE") }
        }, new String [] {" "}));
    table.getColumnModel().getColumn(0).setCellEditor(new EdtrRndr());
    table.getColumnModel().getColumn(0).setCellRenderer(new EdtrRndr());
    jscrollpane.setViewportView(table);
    panel.add(jscrollpane);
    return panel;
  }

  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      frame.getContentPane().add(new TableWithButtons().makeUI());
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(240, 240);
      frame.setSize(240, 240);
      frame.setVisible(true);
    });
  }
}

class CompButton extends JPanel {

  private final JButton intButton = new JButton();

  CompButton(ButtonInfo info) {
    super();
    setInfo(info);
    info.setOwner(this); // Another Try
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    add(intButton);
  }

  public ButtonInfo getInfo() {
    return new ButtonInfo(intButton.getText());
  }

  public void setEnableButton(boolean enable) {
    intButton.setEnabled(enable);
  }

  public void setInfo(ButtonInfo info) {
    for (ActionListener al : intButton.getActionListeners()) {
      intButton.removeActionListener(al);
    }
    if (info.getInfoText().toLowerCase().equals("red")) {
      intButton.setForeground(new Color(255, 0, 0));
      intButton.setText(info.getInfoText().toUpperCase());
      //Disable Blue Button, in Row 2 (3th row) or Cell(2,0)
      intButton.addActionListener((ActionEvent e) -> {
        System.out.println("Red Button Pressed");
        JTable table = (JTable)SwingUtilities.getAncestorOfClass(
            JTable.class, (Component) e.getSource());
        //table.getModel()).DisableButtonInCell(2,0)
        ButtonInfo binfo = (ButtonInfo)table.getModel().getValueAt(2, 0);
        //CompButton owner = binfo.getOwner();
        CompButton owner = new CompButton(binfo);
        owner.setEnableButton(false);
      });
    }
    if (info.getInfoText().toLowerCase().equals("green")) {
      intButton.setForeground(new Color(0, 196, 0));
      intButton.setText(info.getInfoText().toUpperCase());
      //Enable Blue Button, in Row 2 (3th row) or Cell(2,0)
      intButton.addActionListener((ActionEvent e) -> {
        System.out.println("green Button Pressed");
        JTable table = (JTable)SwingUtilities.getAncestorOfClass(
            JTable.class, (Component) e.getSource());
        //table.getModel()).EnableButtonInCell(2,0)
        ButtonInfo binfo = (ButtonInfo)table.getModel().getValueAt(2, 0);
        //CompButton owner = binfo.getOwner();
        CompButton owner = new CompButton(binfo);
        owner.setEnableButton(true);
      });
    }
    if (info.getInfoText().toLowerCase().equals("blue")) {
      intButton.setForeground(new Color(32, 32, 255));
      intButton.setText(info.getInfoText().toUpperCase());
      //DoClick Red Button, in Row 0 (1st row) or Cell(0,0)
      intButton.addActionListener((ActionEvent e) -> {
        System.out.println("blue Button Pressed");
        JTable table = (JTable)SwingUtilities.getAncestorOfClass(
            JTable.class, (Component) e.getSource());
        //table.getModel()).DoClickButtonInCell(0,0)
        ButtonInfo binfo = (ButtonInfo)table.getModel().getValueAt(0, 0);
        //CompButton owner = binfo.getOwner();
        CompButton owner = new CompButton(binfo);
        owner.intButton.doClick();
      });
    }
  }
}

class ButtonInfo {

  private String text = "";
  private CompButton owner;

  ButtonInfo(String text) {
    this.text = text;
    //owner = new CompButton(this);
  }

  public void setInfoText(String text) {
    this.text = text;
  }

  public String getInfoText() {
    return text;
  }

  public void setOwner(CompButton owner) {
    this.owner = owner;
  }

  public CompButton getOwner() {
    return this.owner;
  }
}

class EdtrRndr extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {
  private CompButton cmpbtne = new CompButton(new ButtonInfo("RED"));
  private CompButton cmpbtnr = new CompButton(new ButtonInfo("RED"));
  @Override
  public Object getCellEditorValue() {
    return cmpbtne.getInfo();
  }
  @Override
  public Component getTableCellEditorComponent(JTable table, Object value,
      boolean isSelected, int row, int column) {
    cmpbtne.setInfo((ButtonInfo) value);
    return cmpbtne;
  }
  @Override
  public Component getTableCellRendererComponent(JTable table,
      Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    cmpbtnr.setInfo((ButtonInfo) value);
    return cmpbtnr;
  }
}

如何在JTable单元格中包含的Button中执行doClick()?

如何启用/禁用JTable单元格中包含的按钮?

0 个答案:

没有答案