使用setValueAt更新JTable

时间:2016-10-29 17:16:57

标签: java swing jtable

我想使用方法setValueAt更新我的JTable的列输出,但它不起作用,错误在哪里?这是我的班级。

public class TabellaQuery extends AbstractTableModel implements TableModel {

    private ArrayList<String> colonne = new ArrayList<String>();
    private ArrayList<ArrayList<String>> tabella = new ArrayList<ArrayList<String>>();

    /**
     * Crea una tabella per visualizzare un ResultSet
     * @param rs: ResultSet da visualizzare
     * @throws SQLException
     */
    public TabellaQuery(ResultSet rs) throws SQLException {
        ResultSetMetaData resultSetMetaData = rs.getMetaData();

        for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++){
            colonne.add(resultSetMetaData.getColumnName(i));
        }
        while (rs.next()){
            ArrayList<String> riga = new ArrayList<String>();
            for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++){
                riga.add(rs.getString(i));
            }
            tabella.add(riga);
        }
    }


    public void addTableModelListener(TableModelListener arg0) {

    }


    public Class<?> getColumnClass(int arg0) {
        return String.class;
    }


    public int getColumnCount() {
        return colonne.size();
    }


    public String getColumnName(int arg0) {
        return colonne.get(arg0);
    }


    public int getRowCount() {
        return tabella.size();
    }


    public Object getValueAt(int arg0, int arg1) {
        return tabella.get(arg0).get(arg1);
    }


    public boolean isCellEditable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }


    public void removeTableModelListener(TableModelListener arg0) {
        // TODO Auto-generated method stub

    }


    public void setValueAt(Object arg0, int arg1, int arg2) {
        tabella.get(arg1).set(arg1, (String)arg0);


    }

}

我在这里调用方法,但在单元格中双击时需要“编辑”:

JButton firList = new JButton("x");


    JPanel leftPnl = new JPanel();


    JTable tabPOI=      new JTable();
    TabellaQuery modPOI= null;

    JScrollPane leftPnl=    new JScrollPane(tabPOI);

    public Creation(){


        super("Creation List");

        Container c = this.getContentPane();

        tabPOI.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);    
        tabPOI.getSelectionModel().addListSelectionListener(new RowListener());




        appPnl.add(firList);

        firList.addActionListener(this);



        totalPnl.add(leftPnl, BorderLayout.CENTER);


        c.add(totalPnl);

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setSize((int)dim.getWidth()+1, (int)dim.getHeight()+1);
        //setSize(400,250);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        setLocation((int)(dim.getWidth()-this.getWidth())/2, (int)(dim.getHeight()-this.getHeight())/2);
        setVisible(true);


    }



    public void actionPerformed(ActionEvent e) {

            String s = e.getActionCommand();
            if(s.equals("x")){

                tabPOI.setValueAt("aa", 0, 0);

            }

    private void createTablePOI(String m){

            try {

                modPOI = PersonDAO.load(m);

            } catch (MyException e) {
                System.out.println("error");
                e.printStackTrace();
            }

            tabPOI.setModel(modPOI);
        }

0 个答案:

没有答案