1

I'd like to say i'm not expert about java, i'm just joking with a Scholastic exercise. so let's explain. i want make a table where i can display my car, my bike, and both together. for now i've only a car class so i'm working with it. i'm stuck in a problem with a listener so let's paste my code and the error first, my tablemodel.

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import java.util.List;

public class AutomobileTableModel extends AbstractTableModel
{



private static final String colonne[] = {"Marca", "Modello", "Stato", "Prezzo", "Tipo", "Colore", "KM", "Anno Imm.", "Scadenza Bollo", "Targa", "Posti", "Portiere", "Bagagliaio", "Cilindrata", "Cilindri", "Cavalli", "Peso/Potenza", "Alimentazione", "Consumo", "Trazione", "Cambio", "Marce", "Lunghezza", "Massa a Vuoto", "Altezza Car.", "Tettuccio"};

    private static final Class<?>[] tipiColonne = {String.class, String.class, String.class, Integer.class, String.class, String.class, Integer.class, String.class, Integer.class, String.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Double.class, String.class, Integer.class, String.class, Integer.class, Integer.class, Double.class, Integer.class, Double.class, String.class};

    private List<Automobili> automobili;

    public AutomobileTableModel(List<Automobili> automobili)
    {
        this.automobili = automobili;
    }

    @Override
    public int getRowCount()
    {
        return automobili.size();
    }

    @Override
    public int getColumnCount()
    {
        return colonne.length;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
         return tipiColonne[columnIndex];
    }

    @Override
    public String getColumnName (int columnIndex)
    {
        return colonne[columnIndex];
    }

    public void setRow(int rowIndex)
    {
        if (automobili.size() > rowIndex)
        {
            automobili.removeRange(automobili.size(), rowIndex);
            fireTableRowsDeleted(automobili.size(), rowIndex);
        }
        else
        {
            automobili.add(new Automobili());
            fireTableRowsInsert(automobili.size(), rowIndex);
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        Automobili automobile = (Automobili)automobili.get(rowIndex);

        switch(columnIndex)
        {
            case 0: return automobile.getMarca();
            case 1: return automobile.getModello();
            case 2: return automobile.getStato();
            case 3: return automobile.getPrezzo();
            case 4: return automobile.getTipo();
            case 5: return automobile.getColore();
            case 6: return automobile.getKm();
            case 7: return automobile.getAnnoImmatricolazione();
            case 8: return automobile.getScadenzaBollo();
            case 9: return automobile.getTarga();
            case 10: return automobile.getPosti();
            case 11: return automobile.getPortiere();
            case 12: return automobile.getBagagliaio();
            case 13: return automobile.getCilindrata();
            case 14: return automobile.getCilindri();
            case 15: return automobile.getCavalli();
            case 16: return automobile.getRapportoPP();
            case 17: return automobile.getMotore();
            case 18: return automobile.getConsumo();
            case 19: return automobile.getTrazione();
            case 20: return automobile.getCambio();
            case 21: return automobile.getMarce();
            case 22: return automobile.getLunghezza();
            case 23: return automobile.getMassaVuoto();
            case 24: return automobile.getAltezzaTerra();
            case 25: return automobile.getTettuccio();
            default: return null;
        }
    }
}

second my frame

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class FrameTabella extends JPanel 
{
    private AutomobileTableModel atb;
    private JTable tb;
    private JScrollPane p;

    public FrameTabella()
    {
        super();

        ArrayList<Automobili> automobile = new ArrayList<Automobili>();
        automobile.add(new Automobili());
        JPanel header = new JPanel();

        atb = new AutomobileTableModel(automobile);
        tb = new JTable(atb);
        p = new JScrollPane(tb);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JLabel jl = new JLabel("Inserisci il numero di veicoli da visualizzare");

        JLabel jl1 = new JLabel("Scegli cosa visualizzare");


        JRadioButton auto = new JRadioButton("Auto");
        JRadioButton moto = new JRadioButton("Moto");
        JRadioButton both = new JRadioButton("Entrambi");
        auto.setSelected(true);

        class RadioButtonActionListener implements ActionListener 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                JRadioButton button = (JRadioButton)e.getSource();
                if (button == auto) JOptionPane.showMessageDialog(null, "auto");
                else if (button == moto) JOptionPane.showMessageDialog(null, "moto");
                else if (button == both) JOptionPane.showMessageDialog(null, "entrambi");
            }
        }

        RadioButtonActionListener al = new RadioButtonActionListener();
        auto.addActionListener(al);
        moto.addActionListener(al);
        both.addActionListener(al);

        ButtonGroup group = new ButtonGroup();
        group.add(auto);
        group.add(moto);
        group.add(both);


        JCheckBox cb = new JCheckBox("Mostra in kw");
        cb.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JCheckBox c = (JCheckBox)e.getSource();
                if(c.isSelected())
                JOptionPane.showMessageDialog(null, "selezionato");
                else
                JOptionPane.showMessageDialog(null, "deselezionato");
            }
        }); 



        SpinnerModel jsm = new SpinnerNumberModel(1, 0, 1000, 1);
        final JSpinner js = new JSpinner(jsm);
        JButton b = new JButton("Visualizza");
        b.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                Integer i = (Integer)js.getValue();
                atb.setRow(i);
            }
        });


        //setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        header.add(jl);
        header.add(js);
        header.add(b);
        header.add(jl1);
        header.add(auto);
        header.add(moto);
        header.add(both);
        header.add(cb);

        add(header);
        add(p);
        setVisible(true);
    }
}

than the error i get:

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: AutomobileTableModel.setRow(I)V
        at FrameTabella$2.actionPerformed(FrameTabella.java:96)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

so i just want make this. when i change the value on the spinner, i want add or delete rows on my table, when i press the JButton, and i'll do this with an edit on my list. so why i get this error? and why when i edit Frame Tabella, i get the following 2 error, while i compile, on the AutomobileTableModel? error:

.\AutomobileTableModel.java:48: error: cannot find symbol
                        automobili.removeRange(automobili.size(), rowIndex);
                                  ^
  symbol:   method removeRange(int,int)
  location: variable automobili of type List<Automobili>
.\AutomobileTableModel.java:54: error: cannot find symbol
                        fireTableRowsInsert(automobili.size(), rowIndex);
                        ^
  symbol:   method fireTableRowsInsert(int,int)
  location: class AutomobileTableModel
2 errors

to solve this i just delete and write again a letter i've deleted, than i recompile and it work. why i get this error too?

Previnkumar
  • 107
  • 3
  • 13
KuroKami69
  • 11
  • 2
  • 1
    `why when i edit Frame Tabella, i get the following 2 error,` - for the first error the first method doesn't exist, so I'm not sure what you are trying to do. For the second error the method exists but you have a typing error in the method name. Read the AbstractTableModel API for the proper method name. This is basic problem solving. Pay attention to the method names in question and Read the API and your custom class to make sure the method name exists. – camickr Nov 12 '17 at 02:18

0 Answers0