Java 如何在弹出窗口中直接显示动态添加的项目,而无需双击?
我有一个PopupMenuListenerJava 如何在弹出窗口中直接显示动态添加的项目,而无需双击?,java,swing,jcombobox,jpopupmenu,jpopup,Java,Swing,Jcombobox,Jpopupmenu,Jpopup,我有一个PopupMenuListener public class MyPopupMenuListener implements PopupMenuListener { protected JTable _table; public MyPopupMenuListener(JTable table) { _table = table; } @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
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 + "'.");
}
}
}
公共类示例扩展了JFrame{
专用JTable_表;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
示例帧=新示例();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共示例(){
立根(100100450300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_table=新的JTable();
_table.setModel(新的DefaultTableModel(
新对象[][]{
{MyEnum.ONE.getDescription()},
{MyEnum.ONE.getDescription()},
{MyEnum.ONE.getDescription()},
},
新字符串[]{
“选择”
}
) {
类[]列类型=新类[]{
String.class
};
公共类getColumnClass(int columnIndex){
返回columnTypes[columnIndex];
}
});
getContentPane().add(_table,BorderLayout.CENTER);
_表1.getColumnModel()
.getColumn(0)
.setCellEditor(新MyCellEditor(新MySwitchComboBox)(新字符串[]){
MyEnum.ONE.getDescription(),
MyEnum.TWO.getDescription(),
})(表),;
}
@抑制警告(“串行”)
公共类MyCellEditor扩展了DefaultCellEditor{
专用JComboBox\u组合框;
公共MyCellEditor(JComboBox组合框,JTable表){
超级(组合框);
_组合框=组合框;
_表=表;
}
@凌驾
公共组件getTableCellEditorComponent(JTable表,
对象值,
他当选了,,
int row,
int列){
矩形r=\u table.getCellRect(行、列、真);
_组合框。设置位置(新点(r.x、r.y+r.height));
返回super.getTableCellEditorComponent(表、值、isSelected、行、列);
}
}
@抑制警告(“串行”)
公共类MySwitchComboBox扩展了JComboBox{
公共MySwitchComboBox(字符串[]项){
超级(项目);
addPopupMenuListener(新的PopupMenuListener(){
私有布尔值_firedpopupmenue将变为可见;
@凌驾
public void弹出菜单将变为可见(弹出菜单事件e){
JComboBox comboBox=null;
comboBox=(JComboBox)e.getSource();
最终整数选择箭头=
_table.rowAtPoint(comboBox.getLocation());
最终整数选择列=
_table.columnAtPoint(comboBox.getLocation());
if(_table.getValueAt(selectedRow,selectedColumn)
.equals(MyEnum.BOTH.getDescription()){
comboBox.getModel().setSelectedItem(MyEnum.BOTH.getDescription());
如果(_firedpopupmenue将可见){
_firedpopupMenuWillBecomeVisible=false;
}否则{
_firedPopupMenuWillBecomeVisible=true;
comboBox.FirePopupMenu将变为可见();
}
}
}
@凌驾
public void PopupMenu将变得不可见(PopupMenuEvent e){
}
@凌驾
公共无效popupMenuCanceled(PopupMenuEvent e){
}
});
addItemListener(新的ItemListener(){
@凌驾
公共无效itemStateChanged(ItemEvent e){
JComboBox comboBox=null;
MyEnum selectedConstant=MyEnum.getEnum(例如getItem().toString());
如果(例如getStateChange()==ItemEvent.SELECTED&&selectedConstant!=MyEnum.BOTH){
comboBox=(JComboBox)e.getSource();
final int selectedRow=_table.rowAtPoint(comboBox.getLocation());
final int selectedColumn=_table.columnAtPoint(comboBox.getLocation());
如果(selectedRow!=0){
布尔allEqual=false;
布尔allNotEqual=false;
对于(int i=0;i<3;i++){
如果(0==i){
继续;
}
如果(selectedRow==i){
allEqual=true;
}else if(selectedConstant==MyEnum.getEnum(_table.getValueAt(i,
选定列)
.toString()){
allEqual=true;
}否则{
allNotEqual=true;
}
}
if(allEqual&!allNotEqual){
setHeaderValue(selectedConstant.getDescription(),
0,
选定列);
}否则{
setHeaderValue(MyEnum.BOTH)。