Jumat, 04 November 2016

Java Error : Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1





TERIMA KASIH TELAH BERKUNJUNG TEMAN !
SEBELUM MELANJUTKAN MEMBACA, TOLONG SUBSCRIBE KE CHANNEL YOUTUBE SAYA YA KAWAN.
CARANYA KLIK TOMBOL MERAH DIATAS.

=====================================================================================================================================



Pada saat menggunakan JTable dengan menambahkan event "addListSelectionListener" kita mendapatkan exception "java.lang.ArrayIndexOutOfBoundsException: -1" ketika akan menghapus baris yang ada di dalam JTable. Solusinya akan saya berikan dalam artikel ini berdasarkan kasus yang telah ditanyakan di stackoverflow.com.

Berikut ini adalah kode java yang menghasilkan exception "java.lang.ArrayIndexOutOfBoundsException: -1" :

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class App {

    JFrame frame = new JFrame();
    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel(new Object[][] {},
            new String[] { "Country", "City", "Street" });
    JButton button = new JButton("Remove");
    JTextField countryTxt = new JTextField();

    int row;

    public App() {
        table.setModel(model);
        data();
        table.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (!e.getValueIsAdjusting()) {
                            row = table.getSelectedRow();
                            countryTxt.setText((String) model
                                    .getValueAt(row, 0));
                        }
                    }
                });
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                model.removeRow(row);
            }
        });
        frame.add(countryTxt,BorderLayout.NORTH);
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public void data() {
        model.addRow(new String[] { "USA", "New York", "First street" });
        model.addRow(new String[] { "Russia", "Moscow", "Second street" });
        model.addRow(new String[] { "Japan", "Osaka", "Osaka street" });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new App();
            }
        });
    }
}