0

这段代码的目的是,当单元格[0,1]中写入某些内容时,用随机数填充表格;问题是表格不会自动刷新。仅当我移动边框或单击单元格时,表格才会刷新内容。

import javax.swing.table.TableModel;

public abstract class AbstractTableData implements TableModel{

    public static AbstractTableData getTabellaDati(){

    throw new UnsupportedOperationException("");

};




    @Override

    abstract public boolean isCellEditable(int rowIndex, int columnIndex);

}

/---------------------------------------------------- --/

import java.util.Random;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;


public class TableData extends AbstractTableData{
    public static final int N_C = 10;
    public static final int N_R = 10;
    public String[] heading;
    public String[][] data;

    public TableData(String[] h, String[][] data) {
           this.heading = h;
        this.data = data;
    }

    @Override
    public int getRowCount() {
         return heading.length;
    }

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

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

    @Override
    public Class<?> getColumnClass(int columnIndex) {
       return "".getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnsIndex) {
        if (rowIndex == 0) {
            if (columnsIndex == 0) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
       return data[rowIndex][columnIndex];
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        data[rowIndex][columnIndex] = (String)aValue;
        fireTableUpadate(rowIndex, columnIndex);
    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void fireTableUpadate(int rowIndex, int columnIndex) 
    {
        Random ran =new Random();
        for(int i=1;i<N_R;i++)
            for(int j=0;j<N_R;j++)
                data[i][j]=""+ran.nextInt();
    }
    }

/------------------------------------------------------/

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;


public class Test {

    public static void main(String[] args) {
        String[] head = new String[TableData.N_C];
        String[][] data = new String[TableData.N_R][TableData.N_C];

        for (int i = 0; i < TableData.N_C; i++) {
            head[i] = "" + i;
        }
        for (int i = 0; i < TableData.N_R; i++) {
            for (int j = 0; j < TableData.N_C; j++) {
                data[i][j] = "";
            }
        }

        data[0][0]="->";
        JTable table = new JTable(data, head);
        table.setModel(new TableData(head,data));
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        frame.add(panel.add(table));
        frame.setSize(1000, 380);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

AbstractTableData 必须保持不变。我希望有人能找出问题所在!

4

0 回答 0