hi all m having problem deleting table row from button in jtable .here is my error when i press on delete button this code is similar to my original code . please i need immediate feedback m stuck in this error from days.this is the error
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 0
at java.util.Vector.removeElementAt(Vector.java:554)
at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:463)
at javaapplication8.ButtonEditorTest.selectedCol(ButtonEditorTest.java:57)
at javaapplication8.ButtonEditor.buttonPressed(ButtonEditor.java:60)
at javaapplication8.ButtonEditor.getTableCellEditorComponent(ButtonEditor.java:23)
at javax.swing.JTable.prepareEditor(JTable.java:5790)
at javax.swing.JTable.editCellAt(JTable.java:3515)
at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1108)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1038)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
at java.awt.Component.processMouseEvent(Component.java:6501)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
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:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
import java.awt.Component;
import java.util.EventObject;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;
public class ButtonEditor extends JButton implements TableCellEditor {
public ButtonEditor() {
super("Button");
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
buttonPressed(table, row, column);
return this;
}
public void cancelCellEditing() {
System.out.println("Cancel");
}
public boolean stopCellEditing() {
return true;
}
public Object getCellEditorValue() {
return null;
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public void addCellEditorListener(CellEditorListener l) {
}
public void removeCellEditorListener(CellEditorListener l) {
}
protected void fireCellEditing(ChangeEvent e){
}
private void buttonPressed(JTable table, int row, int column){
JOptionPane.showMessageDialog(table, "Pressed at " + row + " " + column);
ButtonEditorTest bet=new ButtonEditorTest();
bet.selectedCol(row);
}
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import sun.swing.table.DefaultTableCellHeaderRenderer;
public class ButtonEditorTest extends JFrame {
JTable jTable1 = new JTable();
/**
* @throws HeadlessException
*/
javax.swing.table.DefaultTableModel defaultTableModel = new javax.swing.table.DefaultTableModel();
public ButtonEditorTest() throws HeadlessException {
super();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"asdf", "asdf", "asdf", "Delete", "Update"},
{"asdf", "asdf", "asdf", "Delete", "Update"},
{"asdf", "asdf", "asdf", "Delete", "Update"},
{"asdf", "asdf", "asdf", "Delete", "Update"}
},
new String [] {
"Sr.no", "Book Title", "Author Name", "Delete", "Edit"
}
));
JScrollPane scroll = new JScrollPane(jTable1);
jTable1.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
jTable1.getColumnModel().getColumn(3).setCellEditor(new ButtonEditor());
jTable1.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
jTable1.getColumnModel().getColumn(4).setCellEditor(new ButtonEditor());
getContentPane().add(scroll, BorderLayout.CENTER);
pack();
DefaultTableCellHeaderRenderer dtchr=new DefaultTableCellHeaderRenderer();
}
public void selectedCol(int row){
int rowCount= defaultTableModel.getRowCount();
for(int i=0;i<rowCount;i++ ){
//defaultTableModel.removeRow(0);
defaultTableModel.removeRow(jTable1.getSelectedRow());
//System.out.println(i);
}
jTable1.setModel(defaultTableModel);
defaultTableModel.removeRow(row);
System.out.println("deleting");
defaultTableModel.removeRow(jTable1.getSelectedRow());
System.out.println("deleted");
}
public static void main(String[] args) {
final JFrame f = new ButtonEditorTest();
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
EventQueue.invokeLater(new Runnable(){
public void run() {
f.setVisible(true);
}});
}
}
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class ButtonRenderer extends JButton implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
String label = (value ==null) ? "" : value.toString();
setText( label );
return this;
}
}