`

FrameTableDemo2

J# 
阅读更多
 /** 设置是否可更改 */
        public boolean isCellEditable(int rowIndex, int columnIndex)
        {
            if (columnIndex < 2)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        
        /** 设置单元格值 */
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)
        {
            if (debug)
            {
                System.out.println("设置值在 " + rowIndex + " , " + columnIndex + " to" + aValue + "(an instance of "
                    + aValue.getClass() + " )");
            }
            if (data[0][columnIndex] instanceof Integer && !(aValue instanceof Integer))
            {
                try
                {
                    data[rowIndex][columnIndex] = Integer.valueOf(aValue.toString());
                    fireTableCellUpdated(rowIndex, columnIndex);
                }
                catch (Exception e)
                {
                    JOptionPane.showMessageDialog(FrameTableDemo.this, "the \"" + getColumnName(columnIndex)
                        + "\"列只接受整形数据.");
                }
            }
            else
            {
                data[rowIndex][columnIndex] = aValue;
                fireTableCellUpdated(rowIndex, columnIndex);
                
            }
            if (debug)
            {
                System.out.println("数据的最新改变值");
                printDebugDate();
            }
        }
        
        private void printDebugDate()
        {
            int numRows = getRowCount();
            int numCols = getColumnCount();
            for (int i = 0; i < numRows; i++)
            {
                System.out.print(" row " + i + ":");
                for (int j = 0; j < numCols; j++)
                {
                    System.out.println("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("-----------------------------");
        }
        
        /** 设置列名 */
        public String getColumnName(int column)
        {
            return columnNames[column];
        }
    }
    
    public static void main(String[] args)
    {
        FrameTableDemo frame = new FrameTableDemo();
        frame.pack();
        frame.setVisible(true);
    }
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics