我有这个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 + "'.");
}
}
}
我无法测试或建议修复您的代码,因为它不是SSCCE,所以我只能在这一点上提供建议。
代替PopupMenuListener
为您的组合检测表中所选的项目,如何在单独的JButton的ActionListener
做它?
或者,如果您试图捕获用户单击的表中的任何单元格,您可以为列添加TableColumnModelListener
,为行添加ListSelectionListener
,以达到相同的效果。
-
在Swing中不可能同时
show()
两个弹出窗口, -
JPopupMenu
将(立即)hide()
通过点击JCombobox
来显示Arrow Button
-
这可以将
JPopup
更改为JWindow
,最好不修饰JDialog
-
示例如何
add
/remove
/modify
JMenuItems
之前的JPopupMenu
是可见的,在mousePressed
中添加所需的更改,在mouseReleased
中更好,在代码行maybeShowPopup(e);
之前执行