0

我有这个 PopupMenuListener

public class MyPopupMenuListener implements PopupMenuListener {

protected JTable _table;

public MyPopupMenuListener(JTable table) {
    _table = table;
}

@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    JComboBox comboBox = null;

    comboBox = (JComboBox) e.getSource();
    final int selectedRow = _table.rowAtPoint(comboBox.getLocation());
    final int selectedColumn = _table.columnAtPoint(comboBox.getLocation());

    if (_table.getValueAt(selectedRow, selectedColumn)
            .equals(MyEnum.BOTH.getDescription())) {
        comboBox.getModel().setSelectedItem(MyEnum.BOTH.getDescription());
    }
}

@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}

@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
}

当我单击此组合框时,该项目已添加,但我必须再次单击以查看包含新项目和原始三个项目的弹出菜单。我想添加该项目并在不双击的情况下显示所有四个项目。

更新:这是一个 SSCCE。在示例中,弹出菜单是直接可见的,在我的代码库中,当我第一次单击带有添加项目的组合框时,我看不到其他的,我必须再次单击组合框才能看到所有这些。

public class Example extends JFrame {
private JTable _table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Example frame = new Example();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Example() {
    setBounds(100, 100, 450, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    _table = new JTable();
    _table.setModel(new DefaultTableModel(
        new Object[][] {
            {MyEnum.ONE.getDescription()},
            {MyEnum.ONE.getDescription()},
            {MyEnum.ONE.getDescription()},
        },
        new String[] {
            "Selection"
        }
    ) {
        Class[] columnTypes = new Class[] {
            String.class
        };
        public Class getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }
    });
    getContentPane().add(_table, BorderLayout.CENTER);
    _table.getColumnModel()
            .getColumn(0)
            .setCellEditor(new MyCellEditor(new MySwitchComboBox(new String[] {
                    MyEnum.ONE.getDescription(),
                    MyEnum.TWO.getDescription(),
            }), _table));

}

@SuppressWarnings("serial")
public class MyCellEditor extends DefaultCellEditor {

    private JComboBox _comboBox;

    public MyCellEditor(JComboBox comboBox, JTable table) {
        super(comboBox);
        _comboBox = comboBox;
        _table = table;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 int row,
                                                 int column) {

        Rectangle r = _table.getCellRect(row, column, true);
        _comboBox.setLocation(new Point(r.x, r.y + r.height));
        return super.getTableCellEditorComponent(table, value, isSelected, row, column);
    }

}

@SuppressWarnings("serial")
public class MySwitchComboBox extends JComboBox {

    public MySwitchComboBox(String[] items) {
        super(items);
        addPopupMenuListener(new PopupMenuListener() {
            private boolean _firedPopupMenuWillBecomeVisible;

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                JComboBox comboBox = null;

                comboBox = (JComboBox) e.getSource();
                final int selectedRow =
                        _table.rowAtPoint(comboBox.getLocation());
                final int selectedColumn =
                        _table.columnAtPoint(comboBox.getLocation());

                if (_table.getValueAt(selectedRow, selectedColumn)
                        .equals(MyEnum.BOTH.getDescription())) {
                    comboBox.getModel().setSelectedItem(MyEnum.BOTH.getDescription());

                    if (_firedPopupMenuWillBecomeVisible) {
                        _firedpopupMenuWillBecomeVisible = false;
                    } else {
                       _firedPopupMenuWillBecomeVisible = true;
                       comboBox.firePopupMenuWillBecomeVisible();
                    }
                }
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
            }
        });

        addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                JComboBox comboBox = null;

                MyEnum selectedConstant = MyEnum.getEnum(e.getItem().toString());
                if (e.getStateChange() == ItemEvent.SELECTED && selectedConstant != MyEnum.BOTH) {
                    comboBox = (JComboBox) e.getSource();
                    final int selectedRow = _table.rowAtPoint(comboBox.getLocation());
                    final int selectedColumn = _table.columnAtPoint(comboBox.getLocation());


                    if (selectedRow != 0) {
                        boolean allEqual = false;
                        boolean allNotEqual = false;
                        for (int i = 0; i < 3; i++) {
                            if (0 == i) {
                                continue;
                            }

                            if (selectedRow == i) {
                                allEqual = true;
                            } else if (selectedConstant == MyEnum.getEnum(_table.getValueAt(i,
                                                                                            selectedColumn)
                                    .toString())) {
                                allEqual = true;
                            } else {
                                allNotEqual = true;
                            }
                        }

                        if (allEqual && !allNotEqual) {
                            setHeaderValue(selectedConstant.getDescription(),
                                           0,
                                           selectedColumn);
                        } else {
                            setHeaderValue(MyEnum.BOTH.getDescription(),
                                           0,
                                           selectedColumn);
                        }
                        // do something
                    } else {
                        for (int i = 0; i < 3; i++) {
                            if (selectedRow != i) {
                                _table.setValueAt(selectedConstant.getDescription(),
                                                  i,
                                                  selectedColumn);
                                // do something
                            }
                        }
                    }
                }
            }

            protected void setHeaderValue(final String value,
                                          final Integer row,
                                          final int column) {
                _table.setValueAt(value, row, column);
            }

        });
    }
}

public enum MyEnum {
    BOTH("both"),
    ONE("one"),
    TWO("two");

    private String _description;

    private MyEnum(String description) {
        _description = description;
    }

    public synchronized final String getDescription() {
        return _description;
    }

    public static MyEnum getEnum(final String description) {
        for (final MyEnum element : MyEnum.values()) {
            if (element.getDescription().equals(description)) {
                return element;
            }
        }

        throw new IllegalArgumentException(
                "No enum in const class " + MyEnum.class.getCanonicalName()
                        + " contains description '" + description + "'.");
    }

}
}
4

2 回答 2

1
  1. 在 Swing 中不可能同时show()弹出两个窗口,

  2. JPopupMenu 将(立即)hide()通过点击JCombobox进入Arrow Button

  3. 这可能会更改JPopupJWindow, 更好的 un_decoratedJDialog

  4. 示例如何 add//之前是可见的,在代码行执行之前添加所需的remove更改,更好的是modifyJMenuItemsJPopupMenumousePressedmouseReleasedmaybeShowPopup(e);

于 2013-05-17T18:46:50.203 回答
1

我无法测试或建议对您的代码进行修复,因为它不是SSCCE,所以我现在只能提供建议。

而不是PopupMenuListener让您的组合来检测表格中的选定项目,如何在ActionListener单独的 JButton 中进行呢?

或者,如果您尝试捕获用户单击的表格中的任何单元格,您可以TableColumnModelListener为列添加一个,ListSelectionListener为行添加一个来实现相同的目的。

于 2013-05-17T17:10:47.127 回答