JTable与动态JPopupMenu

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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。我怎样才能让它发挥作用?为什么这样做,为什么我的代码不起作用?我在网上搜索了我的问题的答案,但没有找到太多,奇怪的是。

答案

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

你的逻辑应该是:

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

以上是关于JTable与动态JPopupMenu的主要内容,如果未能解决你的问题,请参考以下文章

如何用 JDialog 模仿 JPopupMenu 的行为?

JTable动态更改行高[重复]

jpopupmenu menuitem actionperformed未触发

在 World Wind 显示中获取 JPopupMenu

JAVA中怎么把list或map里的值动态添加至JTable

java Jtable 如何动态添加button按钮