gpt4 book ai didi

java - JTable 和组合框。右键打开

转载 作者:行者123 更新时间:2023-11-30 06:26:18 29 4
gpt4 key购买 nike

我创建了一个表,在该表中某些行可以使用组合框进行编辑。我希望通过右键单击而不是左键单击打开此组合框。我尝试了几种方法,但我认为这是我最接近的方法:

resultsTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {


Point p = new Point(evt.getX(), evt.getY());
int row = resultsTable.rowAtPoint(p);
int column = resultsTable.columnAtPoint(p);

if (resultsTable.getModel().isCellEditable(row, column)) {
if (SwingUtilities.isRightMouseButton(evt)) {
resultsTable.setRowSelectionInterval(row, row);
resultsTable.setColumnSelectionInterval(column, column);

resultsTable.editCellAt(row, column);
StatusComboBox box = (StatusComboBox) resultsTable.getModel().getValueAt(row, column);
box.setVisible(true);
box.showPopup();
}
}
}
});

但是,它给了我以下错误:

java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2062)
at java.awt.Component.getLocationOnScreen(Component.java:2036)
at javax.swing.JPopupMenu.show(JPopupMenu.java:948)
at javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:209)
at javax.swing.plaf.basic.BasicComboBoxUI.setPopupVisible(BasicComboBoxUI.java:877)
at javax.swing.JComboBox.setPopupVisible(JComboBox.java:816)
at javax.swing.JComboBox.showPopup(JComboBox.java:801)
at org.zaproxy.zap.extension.authorisationChecker.view.AuthorisationCheckerStatusPanel$1.mouseClicked(AuthorisationCheckerStatusPanel.java:154)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

With是由box.showPopup()引起的;

我不明白这个错误,因为 editCellAt 确实显示了组合框。它确实(显然^^)没有打开组合框。

谢谢!

最佳答案

您需要访问用作单元格编辑器的组合框。

基本代码是:

int row = table.rowAtPoint(e.getPoint());
int column = table.columnAtPoint(e.getPoint());
table.changeSelection(row, column, false, false);
table.editCellAt(row, column);
Component c = table.getEditorComponent();

if (c instanceof JComboBox)
{
JComboBox comboBox = (JComboBox)c;
comboBox.showPopup();
}

关于java - JTable 和组合框。右键打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47139029/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com