gpt4 book ai didi

java - 删除 Jtable 中的行后出错

转载 作者:行者123 更新时间:2023-12-02 08:22:42 25 4
gpt4 key购买 nike

我有一个框架,其中包含一个表格和两个按钮。

enter image description here

1) 重复行2) 删除行

表格第三列的组合

我有一个场景,我复制空白行并单击最后一行的组合框以选择该行,然后单击删除行按钮...然后在删除之后,每当我尝试单击任何单元格时,它都会给我以下错误。

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6 >= 6
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source)
at javax.swing.JTable.setValueAt(Unknown Source)
at javax.swing.JTable.editingStopped(Unknown Source)
at javax.swing.AbstractCellEditor.fireEditingStopped(Unknown Source)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(Unknown Source)
at javax.swing.DefaultCellEditor$3.stopCellEditing(Unknown Source)
at javax.swing.DefaultCellEditor.stopCellEditing(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$300(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是相关框架的源代码。

public class UI {

JFrame frame;
JTable TblFilePane1;

UI()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screen = kit.getScreenSize();

frame = new JFrame("Web Automation Tool (Selenium)");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
frame.setLocation(screen.width/5, screen.height/300);
frame.setPreferredSize(new Dimension(1000, 820));
frame.pack();
frame.setVisible(true);

final JPanel Pane1 = new JPanel(new BorderLayout());

final JPanel FotterPanelPane1 = new JPanel(new BorderLayout());


String columnNamesTestSuitePane1[] = { "Scope ID","Test Suite","Data File Required","Data File Path"};
// Create some data
String dataValuesTestSuitePane1[][] =
{
{ "","","Yes","" },
};


// Create a new table instance
TblFilePane1 = new JTable( dataValuesTestSuitePane1, columnNamesTestSuitePane1 );
TblFilePane1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
TblFilePane1.getTableHeader().setReorderingAllowed(false);
TblFilePane1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultTableModel modelPane1 = new DefaultTableModel(dataValuesTestSuitePane1, columnNamesTestSuitePane1);

TblFilePane1.setModel(modelPane1);
TblFilePane1.setRowHeight(22);
final JScrollPane scrollPane1 = new JScrollPane( TblFilePane1 );
//scrollPane1.setBounds(0, 0, 250, 250);

//final LineNumberTableRowHeader TblFilePane1LineNumber = new LineNumberTableRowHeader(scrollPane1, TblFilePane1);
//TblFilePane1LineNumber.setBackground(Color.orange);
//scrollPane1.setRowHeaderView(TblFilePane1LineNumber);


TableColumn ScopeIDPane1 = TblFilePane1.getColumnModel().getColumn(0);
ScopeIDPane1.setPreferredWidth(30);

TableColumn TSIDPane1 = TblFilePane1.getColumnModel().getColumn(1);
ScopeIDPane1.setPreferredWidth(30);

TableColumn dfReq = TblFilePane1.getColumnModel().getColumn(2);
JComboBox cmbDfReq = new JComboBox();
cmbDfReq.addItem("Yes");
cmbDfReq.addItem("No");
dfReq.setPreferredWidth(40);

TableColumn DataFile = TblFilePane1.getColumnModel().getColumn(3);
DataFile.setPreferredWidth(490);

TableColumn NotEditable = TblFilePane1.getColumnModel().getColumn(0);
JTextField ScopeIDNE = new JTextField();
ScopeIDNE.setEnabled(false);


TableColumn NotEditableTS = TblFilePane1.getColumnModel().getColumn(1);
JTextField TSIDNE = new JTextField();
TSIDNE.setEnabled(false);

NotEditableTS.setCellEditor(new DefaultCellEditor(TSIDNE));
NotEditable.setCellEditor(new DefaultCellEditor(ScopeIDNE));
dfReq.setCellEditor(new DefaultCellEditor(cmbDfReq));

Border margin = new EmptyBorder(10, 10, 5, 10);
FotterPanelPane1.setBorder(margin);

FotterPanelPane1.add(scrollPane1,BorderLayout.CENTER);

final JPanel BtnPanelPane1 = new JPanel();
JButton BtnDuplicateRow = new JButton("Duplicate Row");
JButton BtnDeleteRowPane1 = new JButton("Delete Row");
BtnPanelPane1.add(BtnDuplicateRow);
BtnPanelPane1.add(BtnDeleteRowPane1);

Pane1.add(FotterPanelPane1,BorderLayout.CENTER);
Pane1.add(BtnPanelPane1,BorderLayout.SOUTH);

frame.add(Pane1);

frame.pack();

BtnDuplicateRow.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
DefaultTableModel model = (DefaultTableModel)TblFilePane1.getModel();
int totalRows = TblFilePane1.getRowCount();

Vector data = model.getDataVector();

int[] rows = TblFilePane1.getSelectedRows();
if(rows.length != 1)
{
JOptionPane.showMessageDialog(null, "Please Select one row to Duplicate !");
}

else
{
Vector row = (Vector) data.elementAt(rows[0]);
row = (Vector) row.clone();
model.addRow(row);
}
}
});


BtnDeleteRowPane1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

int row = TblFilePane1.getSelectedRow();

if(row < 0 )
{
JOptionPane.showMessageDialog(null, "Please Select a Row to Delete !");
return;
}
((DefaultTableModel) TblFilePane1.getModel()).removeRow(row);

}
});

}


public static void main(String args[])
{
new UI();

}
//frame.setMaximumSize(new Dimension(1000,820));
}

最佳答案

当您单击按钮时,编辑器仍然处于 Activity 状态。您需要在删除行之前停止编辑。

您可以将以下内容添加到您的 ActionListener 中:

if (TblFilePane1.isEditing())
TblFilePane1.getCellEditor().stopCellEditing();

您应该将它添加到两个监听器。或者您可能想查看 Table Stop Editing对于可以在表格上设置的属性,当表格失去焦点时停止编辑。

此外,变量名不应以大写字符开头。学习并关注 Java Conventions .

关于java - 删除 Jtable 中的行后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35417055/

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