我有一个列(7),有一个按钮,操作只是删除行。我还有mouseclick事件到table.i想要当用户点击删除按钮时它将删除行和一些获取所选单元格值的其他东西。我的删除操作工作正常但是当我点击按钮时抛出异常但是当我点击同一行中的另一个单元格时它给了我正确的行和列数据。我不明白实际问题。按钮不被视为单击的单元格,就像同一行中的其他单元格一样......? 这是我的主要构造函数
public SellWindow() { //dafault constructor
String title[]={"Qty","Code","Name","vat","Discount","Unit Value","Line Total","Delete"};
table.setColumnIdentifiers(title);
this.entry.setModel(table); //entry is the jtable
//entry.getColumn("Delete");
ButtonColumn buttonColumn = new ButtonColumn(entry, delete,7);
buttonColumn.setMnemonic(KeyEvent.VK_D);
}
这是我的按钮类
public ButtonColumn(JTable table, Action action, int column)
{
this.table = table;
this.action = action;
renderButton = new JButton();
editButton = new JButton();
editButton.setFocusPainted( false );
editButton.addActionListener( this );
originalBorder = editButton.getBorder();
setFocusBorder( new LineBorder(Color.BLUE) );
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer( this );
columnModel.getColumn(column).setCellEditor( this );
table.addMouseListener( this );
}
这是我的删除操作代码
Action delete = new AbstractAction(){
public void actionPerformed(ActionEvent e){
JTable table = (JTable)e.getSource();
int modelRow = Integer.valueOf( e.getActionCommand() );
((DefaultTableModel)table.getModel()).removeRow(modelRow);
getIndexRow(); // i add this here just to check but same exception
}
};
这是我点击表格的代码
private void entryMouseClicked(java.awt.event.MouseEvent evt) {
getIndexRow();
}
这是获取表格数据的代码
private void getIndexRow(){ //this works fine for other cells but not for the cell holding the delete button
int row=entry.getSelectedRow();
Object o=entry.getModel().getValueAt(row,6);
System.out.println(row);
System.out.println((String)o);
}
以下是插入行的代码(通过单击aenter按钮获取产品信息)
private void addItemActionPerformed(java.awt.event.ActionEvent evt) {
// this is the action performed by pressing the add item button
result=p.getInfo(this.addItemField.getText());
for(String s:result){
data[i]=s;
i+=1;
}
data[0]="1";
quantity=Integer.parseInt(data[0]);
price=Integer.parseInt(data[5]);
int tPrice=price*quantity;
total=tPrice+total;
ItemTotal +=1;
Itotal.setText(Integer.toString(ItemTotal));
showTotal.setText(Integer.toString(total));
data[6]=Integer.toString(tPrice);
table.addRow(data);
this.addItemField.grabFocus();
addItemField.setText("");
}
例外
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:730)
at java.util.Vector.elementAt(Vector.java:473)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at SellWindow.getIndexRow(SellWindow.java:581)
at SellWindow.access$000(SellWindow.java:26)
at SellWindow$2.actionPerformed(SellWindow.java:49)
at ButtonColumn.actionPerformed(ButtonColumn.java:190)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at javax.swing.plaf.basic.BasicTableUI$Handler.repostEvent(BasicTableUI.java:948)
at javax.swing.plaf.basic.BasicTableUI$Handler.mouseReleased(BasicTableUI.java:1164)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)