1

I have a JButton which, once it is pressed, adds a row into a JTable. I have tried to make this by implementing the following code.

columNames = new Vector<>();
columNames.addElement("Name");
columNames.addElement("CC");
columNames.addElement("Age");
columNames.addElement("PhoneNumber");
columNames.addElement("Date");
columNames.addElement("Amount$");

Object[] dataList = {"name", "cc", "age", "phone", "date", "amount"};

data = new DefaultTableModel(columNames, 0);
data.addRow(dataList);

table = new JTable(data);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setBounds(22, 78, 764, 177);
scrollTable.setViewportView(table);

//ActionListener method!.

if(e.getActionCommand().equals("Add client"))
{
    Object[] dataList = {"name", "cc", "age", "phone", "date", "amount"};
    data.addRow(dataList);

    DefaultTableModel defaut = (DefaultTableModel) table.getModel();
    defaut.addRow(dataList);
}

It throws Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:-1

How can I solve it?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Cohen
  • 79
  • 1
  • 9
  • 2
    Your ActionListener appears to be trying to add the dataList array to the table model **twice**, once via the data object, once via the extracted table model -- why? Why not try to simply add it once? – Hovercraft Full Of Eels Aug 12 '16 at 21:08
  • 2
    If TrashGod's answer doesn't help you solve the problem, then please consider creating and posting a valid [mcve], a small self-contained program that demonstrates your problem, similar to what trashgod has already posted below. – Hovercraft Full Of Eels Aug 12 '16 at 21:09
  • @HovercraftFullOfEels i trying to adding twice cuz the second one is a Action listener wuich will add a new Row. – Cohen Aug 12 '16 at 21:41
  • 2
    @HovercraftFullOfEels is referring to the redundant calls to `addRow()` in your `ActionListener`. Which one gives you the `ArrayIndexOutOfBoundsException`? – trashgod Aug 12 '16 at 21:55

1 Answers1

3

As shown in the complete example below, your fragments appear to work correctly. The example may help you isolated the problem in your full code. In addition,

image

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * @see https://stackoverflow.com/a/38926460/230513
 */
public class Test {

    private final Object[] dataList = {"name", "cc", "age", "phone", "date", "amount"};

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Vector columNames = new Vector<>();
        columNames.addElement("Name");
        columNames.addElement("CC");
        columNames.addElement("Age");
        columNames.addElement("Phone");
        columNames.addElement("Date");
        columNames.addElement("Amount$");
        DefaultTableModel data = new DefaultTableModel(columNames, 0);
        data.addRow(dataList);
        JTable table = new JTable(data);
        JScrollPane scrollTable = new JScrollPane(table);
        scrollTable.setViewportView(table);
        f.add(new JScrollPane(table));
        f.add(new JButton(new AbstractAction("Add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                data.addRow(dataList);
            }
        }), BorderLayout.PAGE_END);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045