具有动态JPopupMenu的JTable

时间:2018-11-15 16:36:32

标签: java swing jtable jpopupmenu

我正在尝试编写一个上下文菜单,该菜单根据行来更改其项目。问题是我无法更新它。这是一个工作示例:

package jframe.jTable;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class TestTableRightClick {
    JPopupMenu popupMenu;

    protected void initUI() {
        final JFrame frame = new JFrame(TestTableRightClick.class.getSimpleName());
        Vector<String> columns = new Vector<String>(Arrays.asList("Name", "Age"));
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 50; i++) {
            Vector<String> row = new Vector<String>();
            for (int j = 0; j < columns.size(); j++) {
                row.add("Cell " + (i + 1) + "," + (j + 1));
            }
            data.add(row);
        }
        JTable table = new JTable(data, columns);
        popupMenu = new JPopupMenu();

        popupMenu.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                //int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupMenu, new Point(0, 0), table));
                //generateTablePopupMenu(rowAtPoint);

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupMenu, new Point(0, 0), table));
                        generateTablePopupMenu(rowAtPoint);
                        if (rowAtPoint > -1) {
                            table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
                        }
                    }
                });
            }

            private void generateTablePopupMenu(int rowAtPoint) {
                System.out.println(rowAtPoint);
                popupMenu.removeAll();
                if ( (rowAtPoint & 1) == 0 ) {
                 System.out.println("even");
                 JMenuItem item = new JMenuItem("Even Row");
                 popupMenu.add(item);}
                 else {
                     System.out.println("odd");
                     //popupMenu = new JPopupMenu();
                     JMenuItem item = new JMenuItem("Odd Row");
                     popupMenu.add(item);}

            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                // TODO Auto-generated method stub

            }


        });


        table.setComponentPopupMenu(popupMenu);
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTableRightClick().initUI();
            }
        });
    }
}

执行代码后,即使将正确的行报告回generateTablePopupMenu,并且添加了新的JMenuItem,JPopupMenu仍为空白。我还试图使代码像下面这样在.invokelater之前运行:

public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupMenu, new Point(0, 0), table));
                generateTablePopupMenu(rowAtPoint);

                SwingUtilities.invokeLater(new Runnable() {...

但是返回的行整数始终为-1。我该如何工作?为什么这样做有效,为什么我的代码无效?我已经在网上搜索了我的问题的答案,但奇怪的是,发现的却不多。

1 个答案:

答案 0 :(得分:2)

每当您从可见容器中添加/删除组件时,都需要告诉容器您已完成添加组件,以便可以调用布局管理器。

您的逻辑应该是:

private void generateTablePopupMenu(int rowAtPoint)
{
    popupMenu.removeAll();

    if ( (rowAtPoint & 1) == 0 )
    {
        JMenuItem item = new JMenuItem("Even Row");
        popupMenu.add(item);}
    else
    {
        JMenuItem item = new JMenuItem("Odd Row");
        popupMenu.add(item);
    }

    popupMenu.revalidate();
}