⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytablemodel.java

📁 基于jxta的局域网P2P文件共享,可以实现局域网中的文件p2p共享,实现文件快速传输及交流
💻 JAVA
字号:
package connex.app.utils.TableUtils;


import javax.swing.table.AbstractTableModel;
import java.util.Vector;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MyTableModel extends AbstractTableModel {
    private boolean DEBUG = true;
    private String[] columnNames = null;

    public MyTableModel(String[] columnNames) {
        this.columnNames = columnNames;
    }


    private Vector data = new Vector();
    public int getColumnCount() {
        return columnNames.length;
    }

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

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return ((TableRow) data.elementAt(row)).get(col);
    }

    public void addRow(TableRow ob) {
        ob.setModel(this);
        data.addElement(ob);
        fireTableRowsInserted(0, data.size());
    }

    public void removeRow(int i) {

        data.removeElementAt(i);
        this.fireTableRowsDeleted(0, data.size());
    }
    public void removeRow(TableRow ob){
      data.removeElement(ob);
      this.fireTableRowsDeleted(0, data.size());
    }

    public void clear() {
        data.removeAllElements();
        this.fireTableRowsDeleted(0, data.size());
    }

    public TableRow getRow(int row) {
        return (TableRow) data.elementAt(row);
    }

    public int getRowPosition(TableRow row) {
        return data.indexOf(row);
    }

    public TableRow getRow(String id) {
        TableRow tr = null;
        for (int i = 0; i < this.getRowCount(); i++) {
            if (id.equals(((TableRow) data.elementAt(i)).getID())) {

                tr = (TableRow) data.elementAt(i);
                break;
            }
        }
        return tr;
    }


    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
  /*  public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }*/

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 4) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            /* System.out.println("Setting value at " + row + "," + col
                                + " to " + value
                                + " (an instance of "
                                + value.getClass() + ")");*/
        }
        if (data.size() <= row) {
            data.addElement(new TableRow());
            fireTableRowsInserted(0, data.size());
        }

        if (((TableRow) data.elementAt(0)).get(col) instanceof Integer
            && !(value instanceof Integer)) {

            try {
                ((TableRow) data.elementAt(row)).add(col, new Integer(value.
                        toString()));
                fireTableCellUpdated(row, col);
            } catch (NumberFormatException e) {

            }
        } else {
            ((TableRow) data.elementAt(row)).add(col, value);
            fireTableCellUpdated(row, col);
        }

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -