JMenuItem actionListner在JTable JPopupMenu内部不起作用

问题描述 投票:0回答:1

我需要使用JMenuItem'delete'删除选定的表行。我尝试使用actionlistner,但无法正常工作。

PatientListView.java

public class PatientListView extends JPanel implements ListSelectionListener, ActionListener
{
  PatientTableModel patientTableModel = new PatientTableModel();

  private DefaultTableModel patient;

  private JTable table;

  JMenuItem deleteItem;

  public PatientListView()
  {
    JPanel panel = new JPanel();
    String[] col = { "Name", "Age", "Address", "Emp. Status" };
    patient = new DefaultTableModel(col, 0)
    {
      @Override public boolean isCellEditable(int row, int column)
      {
        return column == 2 || column == 3;
      }
    };

    table = new JTable(patient);
    table.addMouseListener(new MouseAdapter()
    {
      @Override public void mouseReleased(MouseEvent e)
      {
        int r = table.rowAtPoint(e.getPoint());
        if (r >= 0 && r < table.getRowCount())
        {
          table.setRowSelectionInterval(r, r);
        }
        else
        {
          table.clearSelection();
        }
        int rowindex = table.getSelectedRow();
        if (rowindex < 0)
          return;
        if (e.isPopupTrigger() && e.getComponent() instanceof JTable)
        {
          JPopupMenu popup = new JPopupMenu();
          deleteItem = new JMenuItem("Delete");
          popup.add(deleteItem);
          popup.show(e.getComponent(), e.getX(), e.getY());
        }
      }
    });

    panel.add(new JScrollPane(table));
    add(panel);
    setVisible(true);
  }

  public void deleteSelectedRow()
  {
    //delete method
  }

  public void addTableSelectionListener(ListSelectionListener listSelectionListener)
  {
    ListSelectionModel listSelection = table.getSelectionModel();
    listSelection.addListSelectionListener(listSelectionListener);
  }

  public void addActionListener(ActionListener actionListener)
  {
    // nothing
  }

  public JTable getTable()
  {
    return table;
  }

  @Override public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==deleteItem){
      System.out.println("shavindi");
    }
  }
}

MainWindow.java


public class MainWindow extends JFrame implements DataChangedListner, ActionListener, ListSelectionListener
{
  private InputPanel inputPanel;

  private ButtonPanel buttonPanel;

  private SearchCriteriaPanel searchCriteriaPanel;

  private PatientListView patientListView;

  public MainWindow()
  {
    super("Patient Registration");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.inputPanel = new InputPanel();
    this.buttonPanel = new ButtonPanel();
    this.searchCriteriaPanel = new SearchCriteriaPanel();
    this.patientListView = new PatientListView();
    setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 0;
    this.add(inputPanel, constraints);
    constraints.gridy = 1;
    this.add(buttonPanel, constraints);
    constraints.gridy = 2;
    this.add(searchCriteriaPanel, constraints);
    constraints.gridy = 3;
    this.add(patientListView, constraints);
    buttonPanel.addActionListener(this);
    searchCriteriaPanel.addActionListener(this);
    patientListView.addActionListener(this);        //calling deleteItem actionlistner
    patientListView.addTableSelectionListener(this);
    pack();
    setVisible(true);
  }

  @Override public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == buttonPanel.getClearButton())
    {
      //works fine
    }
    else if (e.getSource() == searchCriteriaPanel.searchButton)
    {
      // works fine
    }
    else if (e.getSource() == patientListView.deleteItem)
    {
      System.out.println("delete clicked");
      patientListView.deleteSelectedRow();
    }
  }


  @Override public void valueChanged(ListSelectionEvent e)
  {

  }

}

我找不到我的代码中的错误。 listSelectionListner可以正常工作并返回选定的行等。但是,单击菜单项“删除”时,不会发生更改。

java swing user-interface actionlistener jmenuitem
1个回答
0
投票

问题是我尚未向JMenuItem添加操作。

可以使用AbstractAction添加操作。

初始化JMenuItem时,请执行以下操作。

        deleteItem = new JMenuItem(new AbstractAction("Delete")
          {
            @Override public void actionPerformed(ActionEvent e)
            {
              deleteSelectedRow();
            }
          });
© www.soinside.com 2019 - 2024. All rights reserved.