0

我在这里有一个小场景,可能是重复的。我有 JTable,我在其中显示一些数据,我有一个鼠标监听器,它监听表上的右键单击并显示一个带有一个菜单项的弹出菜单。我的目标是,当用户单击菜单项时,我从表中获取值并将它们输入到自定义对话框中,该对话框具有一些填充字段,这样用户就不必手动输入整个对话框,因为我会将表格中选择的值提供给对话框。但我的问题是 menuitem actionevent 没有办法让我可以使用table.getRowAt(). 我读过另一条评论(在这里查看https://stackoverflow.com/a/4122082/1788917) 我被告知我可以将行号保存在一个变量中,然后菜单项的操作侦听器可以访问该变量。

我希望它看起来像这样

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

}

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

4

2 回答 2

3

我已经尝试过您提到此案例的案例,并且我能够获取该行并将其显示在JDialog. 我刚刚修改了我的旧代码来做到这一点。我没有彻底测试过。

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

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

于 2013-03-27T12:16:47.107 回答
3

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

你读错了评论。

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

table.getSelectedRow();
于 2013-03-27T15:20:41.930 回答