整理swing之JTable渲染(1)

数码户外 出游全攻略

  

JTable(TableModel dm, TableColumnModel cm) 
          构造 JTable,使用 dm 作为数据模型、cm 作为列模型和默认的选择模型对其进行初始化。

 

采用数据模型与列模型进行构造JTable.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lgh.j2se.fuxi;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * 测试学习jtable
 * @author lgh
 */
public class JTableTest extends JFrame {

    private List<User> list;

    public JTableTest(List<User> list) {
        this.list = list;
        init();
    }

    private void init() {
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.add(new JScrollPane(createJTable()), BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    /**
     * 创建JTable
     * @return
     */
    private JTable createJTable() {
        String[] strs = new String[]{"用户名", "密码"};
        int[] widths = new int[]{100, 100};
        JTable jtable = new JTable(new MyTableModel(), new MyTableColumnModel(strs, widths));
        jtable.setRowHeight(50);
        jtable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        return jtable;
    }

    public static void main(String[] args) {
        List<User> list = new ArrayList<User>();
        list.add(new User("lgh", "lgh"));
        list.add(new User("lgh", "lgh"));
        list.add(new User("lgh", "lgh"));
        list.add(new User("lgh", "lgh"));
        list.add(new User("lgh", "lgh"));
        list.add(new User("lgh", "lgh"));
        JTableTest test = new JTableTest(list);

    }

    private class ColumnCellRenderer extends JLabel implements TableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            this.setHorizontalAlignment(JLabel.CENTER);
            this.setFont(new Font("宋体", Font.PLAIN, 12));
            this.setOpaque(true);
            if (row % 2 == 0) {
                setBackground(new Color(36, 38, 57));
            } else {
                setBackground(new Color(42, 43, 63));
            }
            if (isSelected) {
                setBackground(new Color(96, 99, 131));
            } else {
               setBackground(Color.WHITE);
            }
            if (value == null) {
                setText("");
            } else {
                if (value instanceof Image) {
                    setIcon(new ImageIcon((Image) value));
                } else {
                    setText(value.toString());
                }
            }
            return this;
        }
    }

    /**
     * 列模型
     */
    private class MyTableColumnModel extends DefaultTableColumnModel {

        private String[] strs;
        private int[] widths;
        private ColumnCellRenderer head;
        private ColumnCellRenderer cell;

        public MyTableColumnModel(String[] strs, int[] widths) {
            this.strs = strs;
            this.widths = widths;
            head = new ColumnCellRenderer();
            cell = new ColumnCellRenderer();
            init();
        }

        private void init() {
            for (int i = 0; i < strs.length; i++) {
                TableColumn tc = new TableColumn(i);
                tc.setHeaderRenderer(head);
                tc.setCellRenderer(cell);
                tc.setHeaderValue(strs[i]);
                tc.setPreferredWidth(widths[i]);
                this.addColumn(tc);
            }
        }
    }

    /**
     * 数据模型
     */
    private class MyTableModel extends AbstractTableModel {

        /**返回当前的行数**/
        public int getRowCount() {
            if (list != null) {
                return list.size();
            } else {
                return 0;
            }
        }

        /**返回当前的列数**/
        public int getColumnCount() {
            return 2;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            User user = list.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return user.getUsername();
                case 1:
                    return user.getPassword();
                default:
                    return "";
            }
        }
    }
}

 

你可能感兴趣的:(swing,J2SE)