FrameTableDemo1

package filechooser;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

public class FrameTableDemo extends JFrame
{
    private static final long serialVersionUID = 1L;
    
    private boolean debug = true;
    
    public FrameTableDemo()
    {
        super("职位表");
        MyTableModel myModel = new MyTableModel();
        JTable table = new JTable(myModel);
        
        table.setPreferredScrollableViewportSize(new Dimension(400, 100));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumnModel md7 = (TableColumnModel)table.getColumnModel();
        md7.getColumn(0).setMinWidth(30);
        md7.getColumn(1).setMaxWidth(50);
        md7.getColumn(2).setMaxWidth(60);
        JScrollPane scrollPane = new JScrollPane(table);
        
        table.setRowMargin(2);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        
    }
    
    class MyTableModel extends AbstractTableModel
    {
        private static final long serialVersionUID = 1L;
        
        final String[] columnNames = {"相片", "姓名", "职位", "电话", "工资", "婚否 "};
        
        ImageIcon apple = createImageIcon("apple.jpg", "天峰相片");
        
        ImageIcon anther = createImageIcon("anther.jpg", "其它相片");
        
        ImageIcon apple1 = createImageIcon("apple1.jpg", "天峰相片");
        
        ImageIcon anther2 = createImageIcon("anther2.jpg", "天峰相片");
        
        final Object[][] data = { {apple, "天峰", "自由作家 ", "02988225560", Integer.valueOf(2000), Boolean.valueOf(false)},
            {anther, "娜娜", "总经理秘书 ", "01068939397", Integer.valueOf(5000), Boolean.valueOf(true)},
            {anther2, "杨深刚", "科长 ", "0296887678", Integer.valueOf(6000), Boolean.valueOf(true)},
            {apple1, "李少恒", "财务科长 ", "02933541290", Integer.valueOf(4000), Boolean.valueOf(false)}};
        
        /** 创建图片对象 */
        public ImageIcon createImageIcon(String filename, String description)
        {
            String path = "/image/" + filename;
            System.out.println(path);
            return new ImageIcon(getClass().getResource(path), description);
        }
        
        /** 获取列长度 */
        public int getColumnCount()
        {
            return columnNames.length;
        }
        
        /** 获取行长度 */
        public int getRowCount()
        {
            return data.length;
        }
        
        /** 获取单元格值 */
        public Object getValueAt(int rowIndex, int columnIndex)
        {
            return data[rowIndex][columnIndex];
        }
        
        /** 获取列属性 */
        public Class<?> getColumnClass(int c)
        {
            return getValueAt(0, c).getClass();
        }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值