java:从popupmenu actionListener事件中获取行数据

时间:2013-03-27 11:28:59

标签: java swing jtable mouseevent popupmenu

我在这里有一个小方案可能是重复的。我有JTable,我在那里显示一些数据,我有一个mouselistener,它可以监听桌面上的右键,并显示一个带有一个menuitem的弹出菜单。我的目标是当用户点击menuitem时,我从表中获取值并将它们输入到自定义对话框中,该对话框中有一些填充字段,这样用户就不必手动输入整个对话框,因为我会使用在表格上选择的值提供对话框。但我的问题是,menuitem actionevent没有办法获得这一点,以便我可以使用table.getRowAt()。我已经阅读了另一条评论(请点击这里https://stackoverflow.com/a/4122082/1788917),我被告知我可以将行号保存在一个变量中,然后由actionlistener为menuitem访问。

我希望它看起来像这样

theScrollPane.getViewport().add(myJTable, null);
JMenuItem menuItem = new JMenuItem(clickMe);

menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
    menuItemActionPerformed(e);
}
});

MouseListener popupListener = new PopupListener(); 
popupMenu.add(menuItem);
myJTable.addMouseListener(popupListener);

protected void menuItemActionPerformed (ActionEvent e) {
    // what to do here to get the table row data
    // and send it to my custom dialog via it's constructor ???
}


// a simple nested class for the popup
class PopupListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
        int row = myJTable.rowAtPoint(e.getPoint());
        int selectedRow = myJTable.getSelectedRow();

        // just to make sure the popup appears only where the row is 
                    // selected
        if (row == selectedRow) {
            showPopup(e);
        }
    }

    public void mouseReleased(MouseEvent e) {
        int row = myJTable.rowAtPoint(e.getPoint());
        int selectedRow = myJTable.getSelectedRow();
        if (row == selectedRow) {
            showPopup(e);
        }
    }

    private void showPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
        popupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

}

所以我的问题是,保存行号是我能做到这一点的唯一方法还是有更好的方法?

2 个答案:

答案 0 :(得分:3)

  

我已经阅读了另一条评论(请点击此处https://stackoverflow.com/a/4122082/1788917),我被告知我可以将行号保存在变量中

你读错了评论。

如果您阅读上面链接的答案(即7票的答案),您将看到解决方案是在显示弹出菜单之前选择您单击的行。然后在菜单项Action中,您可以参考

table.getSelectedRow();

答案 1 :(得分:2)

我已经针对此case尝试了您的案例,我可以获取该行并在JDialog中显示该行。我只是修改了我的旧代码来做到这一点。我没有经过彻底的测试。

Pop Up In JTable

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


public class AllTableExample {


    private JTabbedPane tabbedPane;
    private JTable allTable;
    private AllTableModel allTableModel;

    public AllTableExample() {

        List<TableData> data = new ArrayList<TableData>();
        data.add(new TableData("John1", "A", "Maths", "Hellen1"));
        data.add(new TableData("John2", "A", "Maths", "Hellen2"));
        data.add(new TableData("John3", "A", "Maths", "Hellen3"));
        data.add(new TableData("John4", "A", "Maths", "Hellen4"));
        data.add(new TableData("John5", "A", "Maths", "Hellen5"));

        allTableModel = new AllTableModel(data);
    }

    public void createUI() {
        JFrame frame = new JFrame();

        tabbedPane = new JTabbedPane();

        tabbedPane.add("All", getAllTablePanel());

        frame.add(tabbedPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Table Model Example.");
        frame.pack();
        frame.setVisible(true);
    }

    private JPanel getAllTablePanel() {
        JPanel panel = new JPanel();
        allTable = new JTable(allTableModel);
        JScrollPane scroll = new JScrollPane(allTable);
        panel.add(scroll);

        allTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                int r = allTable.rowAtPoint(e.getPoint());
                if (r >= 0 && r < allTable.getRowCount()) {
                    allTable.setRowSelectionInterval(r, r);
                } else {
                    allTable.clearSelection();
                }

                int rowindex = allTable.getSelectedRow();
                if (rowindex < 0)
                    return;
                if (e.getComponent() instanceof JTable) {
                    JDialog dialog = new JDialog();
                    int selectedRow = allTable.getSelectedRow();

                    dialog.setTitle("Edit Row: " + selectedRow);

                    TableData data = ((AllTableModel) allTable.getModel()).getTableData().get(selectedRow);
                    List<TableData> tempData = new ArrayList<TableData>();
                    tempData.add(data);

                    AllTableModel tempModel = new AllTableModel(tempData);

                    JTable table = new JTable(tempModel);
                    dialog.add(new JScrollPane(table));
                    dialog.setVisible(true);
                }
            }
            });

        return panel;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new AllTableExample().createUI();
            }
        };

        EventQueue.invokeLater(r);
    }

}


class AllTableModel extends AbstractTableModel {

    List<TableData> tableData = new ArrayList<TableData>();

    Object[] columnNames = {"Name", "Grade", "Subject", "Staff"};

    public AllTableModel(List<TableData> data) {

        tableData = data;
    }

    public List<TableData> getTableData() {
        return tableData;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column].toString();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return tableData.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        TableData data = tableData.get(rowIndex);
        switch (columnIndex) {
        case 0:
            return data.getName();
        case 1:
            return data.getGrade();
        case 2:
            return data.getSubject();
        case 3:
            return data.getStaff();
        default:
            return null;
        }
    }

}

class TableData {

    private String name;
    private String grade;
    private String subject;
    private String staff;

    public TableData(String name, String grade, String subject, String staff) {
        super();
        this.name = name;
        this.grade = grade;
        this.subject = subject;
        this.staff = staff;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getStaff() {
        return staff;
    }
    public void setStaff(String staff) {
        this.staff = staff;
    }
}

注意:存储弹出窗口打开的表的行号,以便在弹出对话框中的更改后我们必须更新原始行。