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;
    }
}

i use this try catch as u told but now i getting this error

exception com.sun.xml.internal.ws.api.model.ExceptionType is never thrown in body of corresponding try statement

incompatible types
  required: java.lang.Throwable
  found:    com.sun.xml.internal.ws.api.model.ExceptionType
so i tried this 
try {
         defaultTableModel.removeRow(jTable1.getSelectedRow());
     } catch (Exception e) {
     }

but its still gives me the same error:(

Edited 3 Years Ago by Dani: Formatting fixed

incompatible types
required: java.lang.Throwable
found: com.sun.xml.internal.ws.api.model.ExceptionType

The error message says the compiler does not think you are not using the correct type.
It wants a Throwable instead of what you have there.

can u please set my code given above to able it too delete the required row :( i am very mush pissed off by this error of java.lang.ArrayIndexOutOfBoundsException

if you will copy this code to Ur netbeans or eclipse it will run please have a look at it i do not know how to create SSCCE .i created this and i thought that it is SSCCE where as my real code is much more complex than this one :(:(

Your posted text of the error message did NOT give the source line number and show the source line where the error occurs.
Please post the full text of the error message.

This is very confusing.
You create a model called defaultTableModel and have lots of code that works with this model, but the model that's actually in the JTable (at least until line 51) is the completely different one whose definition begins

(new javax.swing.table.DefaultTableModel(
   new Object [][] {
    {"asdf",

Edited 5 Years Ago by JamesCherrill: n/a

public void selectedCol(int row) {



//when the compiler comes to this point error occurs 
       
        defaultTableModel.removeRow(row);
        System.out.println("deleting");
      //  defaultTableModel.removeRow(jTable1.getSelectedRow());
        System.out.println("deleted");

    }

here the error comes

Add a test to see if there is a row there before trying to remove it.

Print out the number of rows and the value of row before the remove call.

Yes, well, I didn't see any code to put any data in defaultTableModel, so I'm not surprised that trying to delete a row gives an error.

This is very confusing.
You create a model called defaultTableModel and have lots of code that works with this model, but the model that's actually in the JTable (at least until line 51) is the completely different one whose definition begins

(new javax.swing.table.DefaultTableModel(
   new Object [][] {
    {"asdf",

should i creat table in normal manner like
Jtable jtable=new Jtable(2,3);
i cqan create it in this way too .but the error wont go away please do some thing please:(

Yes, well, I didn't see any code to put any data in defaultTableModel, so I'm not surprised that trying to delete a row gives an error.

so brither whats the solution please:(

Add a test to see if there is a row there before trying to remove it.

Print out the number of rows and the value of row before the remove call.

ok i try this and tell u right away

One way to keep from getting the exception is to test if there is a row and not try to remove it if it does not exist.

Comments

no there is no row in jtable .how is that possible? why there is no row in jtable??? please tell me how to correct it:(

Please read my first post. You have two table models in your program. You should have only one. The data is is the one you display, but you try to remove from the other one, which has no data.
Norm - I don't know how to make this clearer - can you help?

Comments

i created jtable in this way now but still it is giving the same error:(

Object rows[][] = { { "AMZN", "Amazon", "67 9/16" ,"DELETE","UPDATE"},
        { "AOL", "America Online", "68 3/4" ,"DELETE","UPDATE"},
        { "BOUT", "About.com", "56 3/8" ,"DELETE","UPDATE"},
        { "CDNW", "CDnow", "4 7/16" ,"DELETE","UPDATE"},
        { "DCLK", "DoubleClick", "87 3/16" ,"DELETE","UPDATE"},
        { "EBAY", "eBay", "180 7/8","DELETE","UPDATE" },
        { "EWBX", "EarthWeb", "18 1/4" ,"DELETE","UPDATE"},
        { "MKTW", "MarketWatch", "29" ,"DELETE","UPDATE"},
        { "TGLO", "Theglobe.com", "4 15/16" ,"DELETE","UPDATE"},
        { "YHOO", "Yahoo!", "151 1/8" ,"DELETE","UPDATE"} };
    Object columns[] = {"Sr.no", "Book Title", "Author Name", "Delete", "Edit"};
    JTable table = new JTable(rows, columns);

i get it but how should i make it right please edit my program and post it i will be very thankful to you :( please

now this is how my code code looks like

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 {

  
    /**
     * @throws HeadlessException
     */
    javax.swing.table.DefaultTableModel defaultTableModel = new javax.swing.table.DefaultTableModel();

    public ButtonEditorTest() throws HeadlessException {
        super();
Object rows[][] = { { "AMZN", "Amazon", "67 9/16" ,"DELETE","UPDATE"},
        { "AOL", "America Online", "68 3/4" ,"DELETE","UPDATE"},
        { "BOUT", "About.com", "56 3/8" ,"DELETE","UPDATE"},
        { "CDNW", "CDnow", "4 7/16" ,"DELETE","UPDATE"},
        { "DCLK", "DoubleClick", "87 3/16" ,"DELETE","UPDATE"},
        { "EBAY", "eBay", "180 7/8","DELETE","UPDATE" },
        { "EWBX", "EarthWeb", "18 1/4" ,"DELETE","UPDATE"},
        { "MKTW", "MarketWatch", "29" ,"DELETE","UPDATE"},
        { "TGLO", "Theglobe.com", "4 15/16" ,"DELETE","UPDATE"},
        { "YHOO", "Yahoo!", "151 1/8" ,"DELETE","UPDATE"} };
    Object columns[] = {"Sr.no", "Book Title", "Author Name", "Delete", "Edit"};
    JTable table = new JTable(rows, columns);
        
                    
              

        JScrollPane scroll = new JScrollPane(table);

        table.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(3).setCellEditor(new ButtonEditor());
        table.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(4).setCellEditor(new ButtonEditor());

        getContentPane().add(scroll, BorderLayout.CENTER);
        pack();
        
    }

    public void selectedCol(int row,int col) {


 System.out.println("deleting");
defaultTableModel.getValueAt(row, col);
System.out.print(defaultTableModel.getValueAt(row, col));
       // 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 java.util.EventObject;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
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 btnEditorTest = (ButtonEditorTest) SwingUtilities
         .getWindowAncestor(table);
   btnEditorTest.selectedCol(row,column);
        
        
    }

}
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;
    }
}

now this is how my code looks like please tell me how to make it correct how to delete row form the right table? :(

import java.awt.Component;
import java.util.EventObject;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
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 btnEditorTest = (ButtonEditorTest) SwingUtilities
         .getWindowAncestor(table);
   btnEditorTest.selectedCol(row,column);
        
        
    }

}
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 {

  
    /**
     * @throws HeadlessException
     */
    javax.swing.table.DefaultTableModel defaultTableModel = new javax.swing.table.DefaultTableModel();

    public ButtonEditorTest() throws HeadlessException {
        super();
Object rows[][] = { { "AMZN", "Amazon", "67 9/16" ,"DELETE","UPDATE"},
        { "AOL", "America Online", "68 3/4" ,"DELETE","UPDATE"},
        { "BOUT", "About.com", "56 3/8" ,"DELETE","UPDATE"},
        { "CDNW", "CDnow", "4 7/16" ,"DELETE","UPDATE"},
        { "DCLK", "DoubleClick", "87 3/16" ,"DELETE","UPDATE"},
        { "EBAY", "eBay", "180 7/8","DELETE","UPDATE" },
        { "EWBX", "EarthWeb", "18 1/4" ,"DELETE","UPDATE"},
        { "MKTW", "MarketWatch", "29" ,"DELETE","UPDATE"},
        { "TGLO", "Theglobe.com", "4 15/16" ,"DELETE","UPDATE"},
        { "YHOO", "Yahoo!", "151 1/8" ,"DELETE","UPDATE"} };
    Object columns[] = {"Sr.no", "Book Title", "Author Name", "Delete", "Edit"};
    JTable table = new JTable(rows, columns);
        
                    
              

        JScrollPane scroll = new JScrollPane(table);

        table.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(3).setCellEditor(new ButtonEditor());
        table.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(4).setCellEditor(new ButtonEditor());

        getContentPane().add(scroll, BorderLayout.CENTER);
        pack();
        
    }

    public void selectedCol(int row,int col) {


 System.out.println("deleting");
defaultTableModel.getValueAt(row, col);
System.out.print(defaultTableModel.getValueAt(row, col));
       // 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 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;
    }
}

please edit my code please :( i dont see two modules :( please editing my code will help me understand gettting this problem solved quickly please :( i tried every thing i could :(

You still have two models. You need to put all that data into defaultTableModel

please tell me how

Here's the first

javax.swing.table.DefaultTableModel defaultTableModel = new javax.swing.table.DefaultTableModel();

and this creates a second new one for your JTable

Object rows[][] = { { "AMZN", "Amazon", "67 9/16" ,"DELETE","UPDATE"},
{ "AOL", "America Online", "68 3/4" ,"DELETE","UPDATE"},
{ "BOUT", "About.com", "56 3/8" ,"DELETE","UPDATE"},
{ "CDNW", "CDnow", "4 7/16" ,"DELETE","UPDATE"},
{ "DCLK", "DoubleClick", "87 3/16" ,"DELETE","UPDATE"},
{ "EBAY", "eBay", "180 7/8","DELETE","UPDATE" },
{ "EWBX", "EarthWeb", "18 1/4" ,"DELETE","UPDATE"},
{ "MKTW", "MarketWatch", "29" ,"DELETE","UPDATE"},
{ "TGLO", "Theglobe.com", "4 15/16" ,"DELETE","UPDATE"},
{ "YHOO", "Yahoo!", "151 1/8" ,"DELETE","UPDATE"} };
Object columns[] = {"Sr.no", "Book Title", "Author Name", "Delete", "Edit"};
JTable table = new JTable(rows, columns);

You need to put all that data into defaultTableModel then do a

jTable1.setModel(defaultTableModel);

Use one of the "set" methods for DefaultTableModel. You can read about them in the API JavaDoc.

private void buttonPressed(JTable table, int row, int column){
        
        ButtonEditorTest btnEditorTest = (ButtonEditorTest) SwingUtilities
         .getWindowAncestor(table);
   btnEditorTest.selectedCol();

i this the second table? m i trying to delete value from this table? if so than this means that should create mouse listner in the ButtonEditorTest calss?

Here's the first

javax.swing.table.DefaultTableModel defaultTableModel = new javax.swing.table.DefaultTableModel();

and this creates a second new one for your JTable

Object rows[][] = { { "AMZN", "Amazon", "67 9/16" ,"DELETE","UPDATE"},
{ "AOL", "America Online", "68 3/4" ,"DELETE","UPDATE"},
{ "BOUT", "About.com", "56 3/8" ,"DELETE","UPDATE"},
{ "CDNW", "CDnow", "4 7/16" ,"DELETE","UPDATE"},
{ "DCLK", "DoubleClick", "87 3/16" ,"DELETE","UPDATE"},
{ "EBAY", "eBay", "180 7/8","DELETE","UPDATE" },
{ "EWBX", "EarthWeb", "18 1/4" ,"DELETE","UPDATE"},
{ "MKTW", "MarketWatch", "29" ,"DELETE","UPDATE"},
{ "TGLO", "Theglobe.com", "4 15/16" ,"DELETE","UPDATE"},
{ "YHOO", "Yahoo!", "151 1/8" ,"DELETE","UPDATE"} };
Object columns[] = {"Sr.no", "Book Title", "Author Name", "Delete", "Edit"};
JTable table = new JTable(rows, columns);

You need to put all that data into defaultTableModel then do a

jTable1.setModel(defaultTableModel);

i am working on it now thanks i will tel u whether i remove the error or not

This question has already been answered. Start a new discussion instead.