-3
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
    model.addColumn("ABC");
}

Button works but if I click the newly added cell of jtable it shows java.lang.ArrayIndexOutOfBoundsException:n  //n depends on column's cell I click if I select 2nd column's cell then it will be 2

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
  • 1
    CodeMessyFormattedException. Man, you are asking us for help; but you don't mind putting up such a mess? – GhostCat Mar 24 '17 at 20:14
  • 1
    Post a proper [mcve] that demonstrates the problem is your need more help. I posted my `MCVE` that works without any problems. – camickr Mar 24 '17 at 20:21

1 Answers1

0

The addColumn() method works fine for me:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    SSCCE()
    {
        DefaultTableModel model = new DefaultTableModel(3, 5);
        JTable table = new JTable(model );
        JScrollPane scrollPane = new JScrollPane( table );
        add(scrollPane);

        model.addColumn("abc");
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

Post your demo code that demonstrates the problem if you need more help.

camickr
  • 321,443
  • 19
  • 166
  • 288