如何将数据从弹出菜单传递给actionlistener

时间:2017-02-24 22:54:45

标签: java swing jpopupmenu

我在JTables中添加了JPopUpMenu。我可以从弹出式管理中点击我已经点击的一排表。当我单击菜单项时,如何将表行传递给ItemMenu的ActionListener?

mycode的:

package it.rex.view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import it.rex.model.Interventi;

public class InterventiView extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private Interventi interventi;



    public InterventiView(Interventi i) {

        this.interventi = i;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 500, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(28, 56, 422, 153);
        contentPane.add(scrollPane);

        table = new JTable();
        DefaultTableModel dtm = new DefaultTableModel(0, 0);

        // add header of the table
        String header[] = new String[] { "Id", "Descrizione", "Stato", "Data inizio", "Tipo", "Responsabile" };

        // add header in table model     
         dtm.setColumnIdentifiers(header);
         //set model into the table object
               table.setModel(dtm);

             // add row dynamically into the table  
             // for now only dummy data
        for (int count = 1; count <= 3; count++) {
                dtm.addRow(new Object[] { Integer.toString(count), "data", "data",
                        "data", "data", "data" });
         }

        table.getColumnModel().getColumn(2).setResizable(false);
        scrollPane.setViewportView(table);

        // prepare PopupMenu
        JPopupMenu popupMenu = new JPopupMenu();
        addPopup(table, popupMenu);

        JMenuItem mntmAggiungiIntervento = new JMenuItem("Aggiungi Intervento");
        popupMenu.add(mntmAggiungiIntervento);

        JMenuItem mntmModificaIntervento = new JMenuItem("Modifica Intervento");
        popupMenu.add(mntmModificaIntervento);

        JSeparator separator = new JSeparator();
        popupMenu.add(separator);

        JMenuItem mntmEliminaIntervento = new JMenuItem("Elimina Intervento");
        popupMenu.add(mntmEliminaIntervento);

        JButton btnOk = new JButton("Ok");
        btnOk.setBounds(262, 220, 89, 23);
        contentPane.add(btnOk);

        JButton btnAnnulla = new JButton("Annulla");
        btnAnnulla.setBounds(361, 220, 89, 23);
        contentPane.add(btnAnnulla);

        //Manage action over popupMenu
        mntmAggiungiIntervento.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // Which row on JTable was selected?
                System.out.println("Add " + arg0.getActionCommand());
            }
        });

        mntmEliminaIntervento.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // Which row on JTable was selected?
                System.out.println("Delete");

            }
        });

        mntmModificaIntervento.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // Which row on JTable was selected?
                System.out.println("Modify");

            }
        });


    }

    // Mouse events
    private static void addPopup(Component component, final JPopupMenu popup) {
        component.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }
            private void showMenu(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());

                // Identifico la riga
                JTable table = ((JTable) component);
                int tRaw = table.rowAtPoint(e.getPoint());

                if (tRaw >= 0 && tRaw < table.getRowCount()) {
                    table.setRowSelectionInterval(tRaw, tRaw);
                } else {
                    table.clearSelection();
                }

                System.out.println("Row: " + Integer.toString(tRaw+1));
            }
        });


    }

}

2 个答案:

答案 0 :(得分:0)

构建一个实现ActionListener接口的MyItemActionListener类。为MyItemActionListener提供一个私有字段myPopupManager。使用适当的构造函数在构建监听器时将弹出管理器传递给字段。实现在菜单项中触发的操作的方法,作为获取行号的方法。应该看起来像

 class MyItemActionListener implements ActionListener {
 private PopupManager myPopupManager;

 MyItemActionListener(PopupManager myPopupManager) {
    this.myPopupManager = myPopupManager;
 }

 @Override
 void actionPerformed(ActionEvent e) {
     ((MyMenuItem) e.getSource()).doSomething(myPopupManager.getRow());
 }

 }

将侦听器添加到弹出菜单项。那应该可以胜任。

答案 1 :(得分:0)

您可以通过多种方式执行此操作,您可以创建一个自定义ActionListener,其中引用了JTable,这没关系,重新使用它有点麻烦和麻烦,但它可以工作。

另一个解决方案是利用Action API,参见How to Use Actions,它构建了ActionListener的概念,并提供自包含的属性使其更具可重用性 - 您可以在菜单,按钮和键绑定中使用,而无需重新实现功能。

我首先要定义一个“基础”表格动作......

public abstract class AbstractTableAction<TM extends TableModel> extends AbstractAction {

    private JTable table;
    private TM model;

    public AbstractTableAction(JTable table, TM model) {
        this.table = table;
        this.model = model;
    }

    public TM getModel() {
        return model;
    }

    public JTable getTable() {
        return table;
    }

    public int getSelectedRow() {
        return getTable().getSelectedRow();
    }

    public int getSelectedColumn() {
        return getTable().getSelectedColumn();
    }
}

没什么特别的,它引用了JTable和一些辅助方法。

现在您只需要在表格上定义要执行的操作......

public class AddRowAction extends AbstractTableAction<DefaultTableModel> {

    public AddRowAction(JTable table, DefaultTableModel model) {
        super(table, model);
        putValue(NAME, "Add");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Add @ " + getSelectedRow() + "x" + getSelectedColumn());
    }

}

public class DeleteRowAction extends AbstractTableAction<DefaultTableModel> {

    public DeleteRowAction(JTable table, DefaultTableModel model) {
        super(table, model);
        putValue(NAME, "Delete");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Delete @ " + getSelectedRow() + "x" + getSelectedColumn());
    }

}

public class EditRowAction extends AbstractTableAction<DefaultTableModel> {

    public EditRowAction(JTable table, DefaultTableModel model) {
        super(table, model);
        putValue(NAME, "Edit");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Edit @ " + getSelectedRow() + "x" + getSelectedColumn());
    }

}

一旦你完成所有排序,你只需构建弹出菜单......

JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new AddRowAction(table, dtm));
popupMenu.add(new EditRowAction(table, dtm));
popupMenu.add(new DeleteRowAction(table, dtm));

现在,您需要显示弹出菜单,我更喜欢使用JComponent#setComponentPopupMenu,但这不会选择表格的行/列,这很难过:(所以我们需要自己< / p>

public class MousePopupHandler extends MouseAdapter {

    private JTable table;
    private JPopupMenu popupMenu;

    public MousePopupHandler(JTable table, JPopupMenu popupMenu) {
        this.table = table;
        this.popupMenu = popupMenu;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        doPopup(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        doPopup(e);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        doPopup(e);
    }

    protected void doPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            int x = e.getPoint().x;
            int y = e.getPoint().y;
            int row = table.rowAtPoint(e.getPoint());
            int col = table.columnAtPoint(e.getPoint());
            table.setRowSelectionInterval(row, row);
            table.setColumnSelectionInterval(col, col);
            popupMenu.show(table, x, y);
        }
    }

}

因此,基本上,这可以确保在弹出菜单显示之前选择单击的行/列

这就是你所需要的一切,只需将它们连接在一起,你就会知道能够在触发动作时确定动作的背景

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import static javax.swing.Action.NAME;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                DefaultTableModel dtm = new DefaultTableModel(0, 0);
                // add header of the table
                String header[] = new String[]{"Id", "Descrizione", "Stato", "Data inizio", "Tipo", "Responsabile"};
                // add header in table model     
                dtm.setColumnIdentifiers(header);
                //set model into the table object

                // add row dynamically into the table  
                // for now only dummy data
                for (int count = 1; count <= 3; count++) {
                    dtm.addRow(new Object[]{Integer.toString(count), "data", "data",
                                                                    "data", "data", "data"});
                }

                JTable table = new JTable(dtm);

                JPopupMenu popupMenu = new JPopupMenu();
                popupMenu.add(new AddRowAction(table, dtm));
                popupMenu.add(new EditRowAction(table, dtm));
                popupMenu.add(new DeleteRowAction(table, dtm));

                table.addMouseListener(new MousePopupHandler(table, popupMenu));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MousePopupHandler extends MouseAdapter {

        private JTable table;
        private JPopupMenu popupMenu;

        public MousePopupHandler(JTable table, JPopupMenu popupMenu) {
            this.table = table;
            this.popupMenu = popupMenu;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            doPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            doPopup(e);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            doPopup(e);
        }

        protected void doPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                int x = e.getPoint().x;
                int y = e.getPoint().y;
                int row = table.rowAtPoint(e.getPoint());
                int col = table.columnAtPoint(e.getPoint());
                table.setRowSelectionInterval(row, row);
                table.setColumnSelectionInterval(col, col);
                popupMenu.show(table, x, y);
            }
        }

    }

    public abstract class AbstractTableAction<TM extends TableModel> extends AbstractAction {

        private JTable table;
        private TM model;

        public AbstractTableAction(JTable table, TM model) {
            this.table = table;
            this.model = model;
        }

        public TM getModel() {
            return model;
        }

        public JTable getTable() {
            return table;
        }

        public int getSelectedRow() {
            return getTable().getSelectedRow();
        }

        public int getSelectedColumn() {
            return getTable().getSelectedColumn();
        }
    }

    public class AddRowAction extends AbstractTableAction<DefaultTableModel> {

        public AddRowAction(JTable table, DefaultTableModel model) {
            super(table, model);
            putValue(NAME, "Add");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Add @ " + getSelectedRow() + "x" + getSelectedColumn());
        }

    }

    public class DeleteRowAction extends AbstractTableAction<DefaultTableModel> {

        public DeleteRowAction(JTable table, DefaultTableModel model) {
            super(table, model);
            putValue(NAME, "Delete");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Delete @ " + getSelectedRow() + "x" + getSelectedColumn());
        }

    }

    public class EditRowAction extends AbstractTableAction<DefaultTableModel> {

        public EditRowAction(JTable table, DefaultTableModel model) {
            super(table, model);
            putValue(NAME, "Edit");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Edit @ " + getSelectedRow() + "x" + getSelectedColumn());
        }

    }
}