简体   繁体   中英

Issue with Listener on a JTable (executes code too many times)

I am currently trying to use a listener on my Jtable but as it turns out, the Event gets called once for every item on the table, which makes it very slow. Here's the code I'm using:

tabProductos.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            int idProducto = (int) tabProductos.getValueAt(tabProductos.getSelectedRow(), 0);
            System.out.println(idProducto);
            asignarProdcuto(idProducto);
        }
    }
});

The same Thing happens with this other Listener:

tabProductos.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (!tabProductos.getSelectionModel().isSelectionEmpty()) {
            int idProducto = (int) tabProductos.getValueAt(tabProductos.getSelectedRow(), 0);
            System.out.println(idProducto);
            asignarProdcuto(idProducto);
        }
    }
});

The variable "idProducto" is printed exactly as many times as there are items on the table. The number is correct.

I've tried with "e.getValueIsAdjusting()" for the Second one but it has been of no use.

here's the complete Method:

   public void cargarProductos(ArrayList<Producto> productos) {

        DefaultTableModel tm = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        String[] cabecera = {"ID", "Nombre", "Detalle", "Precio Unit.", "idP", "Proveedor"};
        tm.setColumnIdentifiers(cabecera);
        for (Producto p : productos) {
            Object[] row = {p.getId(), p.getNombre(), p.getTamaño(), p.getPrecioUnitario(), p.getProveedor().getId(), p.getProveedor().getNombre()};
            tm.addRow(row);
            tabProductos.setModel(tm);
            tabProductos.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                  int idProducto = (int) tabProductos.getValueAt(tabProductos.getSelectedRow(), 0);
                        System.out.println(idProducto);
                    }
                }
            });
            tabProductos.getColumn("ID").setMaxWidth(36);
            tabProductos.setAutoCreateRowSorter(true);
            tabProductos.getTableHeader().setReorderingAllowed(false);
        }
    }

Thanks

You're adding your MouseListener multiple times to the JTable inside of the for loop -- bad news since multiple listeners will fire when the table is clicked on. The solution is don't do this . Instead create your JTable, set its model and then after doing this, add the MouseListener to the JTable, but only once.

Perhaps something like (code not tested):

public void cargarProductos(ArrayList<Producto> productos) {

    DefaultTableModel tm = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    String[] cabecera = {"ID", "Nombre", "Detalle", "Precio Unit.", "idP", "Proveedor"};
    tm.setColumnIdentifiers(cabecera);
    for (Producto p : productos) {
        Object[] row = { p.getId(), p.getNombre(), p.getTamaño(), p.getPrecioUnitario(), 
                p.getProveedor().getId(), p.getProveedor().getNombre()};
        tm.addRow(row);
    }

    // do all of this **after** the for loop, not inside it
    tabProductos.setModel(tm);
    tabProductos.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int idProducto = (int) tabProductos.getValueAt(tabProductos.getSelectedRow(), 0);
                System.out.println(idProducto);
            }
        }
    });
    tabProductos.getColumn("ID").setMaxWidth(36);
    tabProductos.setAutoCreateRowSorter(true);
    tabProductos.getTableHeader().setReorderingAllowed(false);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM