gpt4 book ai didi

java - 从同一列的组件菜单中删除 JTable 列时出错

转载 作者:行者123 更新时间:2023-12-02 01:16:50 26 4
gpt4 key购买 nike

我有一个 JTable,其中为表头设置了组件菜单。它包含用于删除列的条目。当删除触发组件菜单的同一列时,出现问题,并且似乎将此操作视为被拖动的列,这会导致 ArrayIndexOutOfBoundsException-1 .

如何安全地从组件菜单中删除当前列而不引发此错误?

这是一个如何触发这种行为的最小示例。只需运行它并删除您所在的同一列(请注意,当您删除最后一列时,它似乎可以工作,但这与我无关):

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class BasicExample extends JFrame {

private JTable t;
private DefaultTableModel dtm;

public BasicExample() {
init();
}

private void init() {
dtm = new DefaultTableModel(new String[][]{{"a", "b"}, {"c", "d"}}, new String[]{"A", "B"});
t = new JTable(dtm);
t.getTableHeader().setComponentPopupMenu(new PopupMenu(t));
this.setLayout(new BorderLayout());
add(t.getTableHeader(), BorderLayout.NORTH);
add(t, BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public class PopupMenu extends JPopupMenu {

private JTable table;

public PopupMenu(JTable table) {
this.table = table;
init();
}

private void init() {
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
String columnName = table.getModel().getColumnName(i);

JMenuItem item = new JMenuItem(columnName);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TableColumn tc = table.getColumn(columnName);
table.getColumnModel().removeColumn(tc);
}
});
this.add(item);
}
}
}

public static void main(String[] args) {
BasicExample be = new BasicExample();
}
}

在堆栈跟踪中,我看到它似乎将该列视为已拖动,因为它在 BasicTableHeaderUI.java 中输入条件 if (draggedColumn != null) { ,而我正在执行的操作并不是真正的阻力。完整的堆栈跟踪如下:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:734)
at java.util.Vector.elementAt(Vector.java:477)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.basic.BasicTableHeaderUI.getHeaderRenderer(BasicTableHeaderUI.java:693)
at javax.swing.plaf.basic.BasicTableHeaderUI.paintCell(BasicTableHeaderUI.java:709)
at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:685)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
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:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
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)

如果相关的话,这会在 Java 8 中发生。更改 JRE/JDK 不是一个选项。

最佳答案

我们的环境中意外地遇到了相同的错误,但确实是零星的,所以我无法重现它。但你的例子是完美的,我能够重现我的错误。请参阅下面的代码。

我在调查过程中修改了我的答案。旧的注释不会反射(reflect)最新的代码。

您的问题是已报告的错误: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8068824

这是一个基于错误解决方法的解决方案:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class BasicExample extends JFrame {
private JTable t;
private DefaultTableModel dtm;

public BasicExample() {
init();
}

private void init() {
dtm = new DefaultTableModel(new String[][] { { "a", "b" }, { "c", "d" } },
new String[] { "A", "B" });
t = new JTable(dtm);

t.getTableHeader().setComponentPopupMenu(new PopupMenu(t));
this.setLayout(new BorderLayout());
add(t.getTableHeader(), BorderLayout.NORTH);
add(t, BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public class PopupMenu extends JPopupMenu {

private JTable table;

public PopupMenu(JTable table) {
this.table = table;
init();
}

private void init() {
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
String columnName = table.getModel().getColumnName(i);

JMenuItem item = new JMenuItem(columnName);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TableColumn tc = table.getColumn(columnName);

t.getTableHeader().setDraggedColumn(null);

table.getColumnModel().removeColumn(tc);
}
});
this.add(item);
}
}
}

public static void main(String[] args) {
BasicExample be = new BasicExample();
}
}

只需在删除之前将draggedColumn设置为null即可。

我一直在挖掘,因为它没有解释为什么在我们的环境中该错误很少发生并且仅在 QA 中发生。 :)

这是代码在我们的环境中的外观(再次感谢非常好的小示例)。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class BasicExample extends JFrame {
private JTable t;
private DefaultTableModel dtm;

public BasicExample() {
init();
}

private void init() {
dtm = new DefaultTableModel(new String[][] { { "a", "b" }, { "c", "d" } },
new String[] { "A", "B" });
t = new JTable(dtm);

PopupMenu lPopupMenu = new PopupMenu(t);

// t.getTableHeader().setComponentPopupMenu(new PopupMenu(t));
t.getTableHeader().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent pE) {
lPopupMenu.show(pE.getComponent(), pE.getX(), pE.getY());
}
});

this.setLayout(new BorderLayout());
add(t.getTableHeader(), BorderLayout.NORTH);
add(t, BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public class PopupMenu extends JPopupMenu {

private JTable table;

public PopupMenu(JTable table) {
this.table = table;
init();
}

private void init() {
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
String columnName = table.getModel().getColumnName(i);

JMenuItem item = new JMenuItem(columnName);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TableColumn tc = table.getColumn(columnName);

// t.getTableHeader().setDraggedColumn(null);

table.getColumnModel().removeColumn(tc);
}
});
this.add(item);
}
}
}

public static void main(String[] args) {
BasicExample be = new BasicExample();
}
}

与您的代码的区别在于我们不使用setComponentPopupMenu(),而是手动显示PopupMenu。并且您的用例不会在我们的代码中产生异常。
为什么?
好问题,但我没有答案。
不知何故,您的代码 JPopupMenu 消耗了鼠标事件。并使用我们的代码 JPopupMenu 将鼠标事件传播到底层组件。如果你用 UI 代替表头就可以看到它。
这就是我用我们的代码重现异常的方式:在标题上按鼠标右键,但不要释放它,并继续将鼠标移到远离标题的弹出窗口上。当指针远离标题时释放右键(这样标题就不会处理 MOUSE_RELEASED 事件)。获取异常。

关于java - 从同一列的组件菜单中删除 JTable 列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58432804/

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