JTable 學習總結

來源:互聯網
上載者:User

// 示範JTable功能

package Table;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.table.AbstractTableModel;import javax.swing.table.TableModel;import javax.swing.table.TableRowSorter;/* * To change this template, choose Tools | Templates and open the template in * the editor. *//** * * @author HuFeiyan */public class JTableDemo extends JPanel {    JTable tableView;    JScrollPane scrollpane;    Dimension origin = new Dimension(0, 0);    JButton toUp;    String[] columnNames = {"C1", "C2", "C3", "C4", "C5"};    private final static int PREFERRED_WIDTH = 680;    private final static int PREFERRED_HEIGHT = 600;    private Object[][] data = {        {"Mary", "Campione",            "Snowboarding", new Integer(5), new Boolean(false)},        {"Alison", "Huml",            "Rowing", new Integer(3), new Boolean(true)},        {"Kathy", "Walrath",            "Knitting", new Integer(2), new Boolean(false)},        {"Sharon", "Zakhour",            "Speed reading", new Integer(20), new Boolean(true)},        {"Philip", "Milne",            "Pool", new Integer(10), new Boolean(false)},};    private boolean DEBUG = false;    public static void main(String[] args) {        JTableDemo t = new JTableDemo();        JFrame frame = new JFrame(JTableDemo.class.getName());        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.getContentPane().setLayout(new BorderLayout());        frame.getContentPane().add(t, BorderLayout.CENTER);        t.setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));        frame.pack();        frame.setLocationRelativeTo(null);        frame.setVisible(true);    }    public JTableDemo() {        init();    }    private void init() {        this.setLayout(new BorderLayout());        TableModel dataModel = new AbstractTableModel() {            @Override            public int getColumnCount() {                return columnNames.length;            }            @Override            public int getRowCount() {                return data.length;            }            @Override            public Object getValueAt(int row, int col) {                return data[row][col];            }            @Override            public String getColumnName(int column) {                return columnNames[column];            }            @Override            public Class getColumnClass(int c) {                return getValueAt(0, c).getClass();            }            @Override            public boolean isCellEditable(int row, int col) {                // 返回false則儲存格不可編輯, 前兩列不能編輯                // 介面上拖動列的位置不影響列的編號                return col > 2;            }            @Override            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() + ")");                }                data[row][col] = value;                fireTableCellUpdated(row, col);                if (DEBUG) {                    System.out.println("New value of data:");                    printDebugData();                }            }        };        tableView = new JTable(dataModel);        TableRowSorter sorter = new TableRowSorter(dataModel);        tableView.setRowSorter(sorter);        tableView.setRowHeight(2, 100);        scrollpane = new JScrollPane(tableView);        tableView.setFillsViewportHeight(true);        this.add(scrollpane, BorderLayout.CENTER);        toUp = new JButton("UP");        // 選中某一行,點擊UP按鈕上移        toUp.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                int[] rowIndexs = JTableDemo.this.tableView.getSelectedRows();                if (rowIndexs == null || rowIndexs.length == 0) {                    return;                }                int index = rowIndexs[0];                if (index != 0) {                    Object[] temp = JTableDemo.this.data[index];                    JTableDemo.this.data[index] = JTableDemo.this.data[index - 1];                    JTableDemo.this.data[index - 1] = temp;                    JTableDemo.this.tableView.setRowSelectionInterval(index - 1, index - 1);                    for (int i = 1; i < rowIndexs.length; i++) {                        JTableDemo.this.tableView.addRowSelectionInterval(rowIndexs[i], rowIndexs[i]);                    }                }                printDebugData();            }        });        JPanel u = new JPanel();        final JCheckBox debug = new JCheckBox("DEBUG", DEBUG);        debug.addChangeListener(new ChangeListener() {            @Override            public void stateChanged(ChangeEvent e) {                DEBUG = debug.isSelected();            }        });        u.add(toUp);        u.add(debug);        this.add(u, BorderLayout.NORTH);    }    public int getColumnCount() {        return columnNames.length;    }    public int getRowCount() {        return data.length;    }    private void printDebugData() {        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.print("  " + data[i][j]);            }            System.out.println();        }        System.out.println("--------------------------");    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.