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

📄 jmtable.java

📁 梦界家园程序开发基底框架
💻 JAVA
字号:
package jm.framework.gui.module.jmtable;

import java.awt.Color;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;

import jm.framework.gui.module.JMItemCode;
import jm.framework.gui.module.jmtable.model.JMTableColumn;
import jm.framework.gui.module.jmtable.model.JMTableColumnModel;
import jm.framework.gui.module.jmtable.model.JMTableModel;
import jm.framework.gui.module.render.JMCellRenderer;
import jm.util.JM2DArray;
import jm.util.JMVList;
import jm.util.JMVector;
import jm.util.JMVal;

/**
 * <p>Title: </p>
 *
 * <p>@author Spook</p>
 *
 * <p>@version 0.1.0</p>
 *
 * <p> </p> not attributable
 */
public class JMTable extends JTable {
    /**
     *
     */
    private static final long serialVersionUID = -645167041896370707L;
    public static int SELECTION_MULTIPLE_INTERVAL = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
    // selectionMode 僾儘僷僥傿偺抣偱偡丅
    public static int SELECTION_SINGLE_INTERVAL = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
    //selectionMode 僾儘僷僥傿偺抣偱偡丅
    public static int SELECTION_SINGLE = ListSelectionModel.SINGLE_SELECTION;
    //selectionMode 僾儘僷僥傿偺抣偱偡丅

    private JMTableModel tbm = null;
    private JMTableColumnModel tcm = null;

    private Color[] bkcolor = {
            Color.decode(JMItemCode.CELL_COLOR), Color.white};
    private JMCellRenderer bk = null;

    int columnsWidth = 0;
    public JMTable () {
        //super () ;
        tbm = new JMTableModel(new JM2DArray());
        tcm = new JMTableColumnModel();
         init();
    }

    public JMTable (JMTableModel model) {
        //super () ;
        tbm = model;
        tcm = new JMTableColumnModel();
         init();
    }

    public JMTable (JMTableColumnModel model) {
        //super () ;
        tcm = model;
        tbm = new JMTableModel(new JM2DArray());
        init();
    }

    public JMTable (JM2DArray datasSource) {
        //super() ;
        tbm = new JMTableModel(datasSource);
        tcm = new JMTableColumnModel();
        init();
    }

    public void init(){
        autoCreateColumnsFromModel = true;
        tbm.setJMTable(this);
        this.setModel(tbm);
        this.setColumnModel(tcm);
        setCol(tbm.getRowData());
        initializeLocalVars();
        updateUI();
    }

    public void setCellEditers () {
        tbm.setCellEditers();
    }

    private void setCol (JM2DArray datasSource) {

        if (datasSource != null && datasSource.colCount() > 0) {
            JMVector<String> colnames = (JMVector<String>) datasSource
                    .getColNames();
            String[] type = {""};
            String[] colNames = ((String[]) colnames.toArray(type));
            if (this.getModel().getColData()==null||this.getModel().getColData().size()==0){
                setColNames(colNames);
            }
        }
    }

    public void setColNames (String[] colnames) {
        try {
            if (colnames != null && colnames.length > 0) {
                this.getModel().setColNames(new JMVector<String>(colnames));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean addRow (JMVector<JMVal> row) {
        try {
            if (tbm.addRow(row)) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean addColumn (JMTableColumn tcol, String colName, JMVList columnData) {
        try {
            if (tbm.addColumn(colName, columnData)) {
                tcol.setHeaderValue(colName);
                super.addColumn(tcol);
//                tableChanged(new TableModelEvent(tbm));
                return true;
            }
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean removeColumn (int colnum) {
        try {
            tbm.getRowData().delCol(colnum);
            this.removeColumn(this.getColumn(colnum));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public void setHiddenColumn (int colnum) {
        this.removeColumn(this.getColumn(colnum));
    }

    public JMTableModel getModel () {
        return tbm;
    }

    public void setBackgroundColor (Color[] background) {
        if (background != null)
            bkcolor = background;
        bk = new JMCellRenderer(bkcolor);
        for (int i = 0; i < this.getColumnNumber(); i++) {
            getColumn(i).setCellRenderer(bk);
        }
    }

    public Color[] getBackgroundColor () {
        return bkcolor;
    }

    public void showHorScroll (boolean show) {
        if (show) {
            setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        } else {
            setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        }
    }

    public TableColumn getColumn (int colnum) {
        return getColumnModel().getColumn(colnum);
    }

//    /**
//     * Returns the <code>TableColumnModel</code> that contains all column information
//     * of this table.
//     *
//     * @return  the object that provides the column state of the table
//     * @see     #setColumnModel
//     */
//    public TableColumnModel getColumnModel() {
//        return tcm;
//    }

    public int getColumnNumber () {
        return this.tbm.getColData().size();
    }

    public void setColResizable (boolean resize) {
        for (int i = 0; i < this.getColumnNumber(); i++) {
            setColResizable(i, resize);
        }
    }

    public void setColResizable (int colnum, boolean resize) {
        getColumn(colnum).setResizable(resize);
    }

    public void setColWidth (int Width) {
        for (int i = 0; i < this.getColumnNumber(); i++) {
            setColWidth(i, Width);
        }
    }

    public void setColumnsWidth (int Width) {
        columnsWidth = Width;
    }

    public int getColumnsWidth () {
        return columnsWidth;
    }

    public void setColWidth (int colnum, int Width) {
        getColumn(colnum).setPreferredWidth(Width);
    }

    public Object getCellData (int colnum, int Width) {
        return this.tbm.getValueAt(colnum, Width);
    }

    public int getColWidth (int colnum) {
        return getColumn(colnum).getPreferredWidth();
    }

    public void setColMaxWidth (int colnum, int Width) {
        getColumn(colnum).setMaxWidth(Width);
    }

    public int getColMaxWidth (int colnum) {
        return getColumn(colnum).getMaxWidth();
    }

    public void setColMinWidth (int colnum, int Width) {
        getColumn(colnum).setMinWidth(Width);
    }

    public int getColMinWidth (int colnum) {
        return getColumn(colnum).getMinWidth();
    }

    public int getRowNumber () {
        int count = 0;
        if (tbm.getRowData() != null) {
            count = tbm.getRowData().rowCount();
        }
        return count;
    }

}

⌨️ 快捷键说明

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