码迷,mamicode.com
首页 > 其他好文 > 详细

定制表格

时间:2019-04-09 11:07:29      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:eval   const   over   cto   tde   ati   for   jtable   final   

public class ExampleFrame_03 extends JFrame {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ExampleFrame_03 exampleFrame_02 = new ExampleFrame_03();
        exampleFrame_02.setVisible(true);
    }

    public ExampleFrame_03() {
        // TODO Auto-generated constructor stub
        super();
        setTitle("定义表格");
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        String[] columnNames = { "A", "B", "C", "D", "E", "F", "G" };
        Vector<String> columnNameV = new Vector<>();
        for (int i = 0; i < columnNames.length; i++) {
            columnNameV.add(columnNames[i]);
        }
        Vector<Vector<String>> tableValueV = new Vector<>();
        for (int i = 0; i < 21; i++) {
            Vector<String> iV = new Vector<>();
            for (int j = 0; j < columnNames.length; j++) {
                iV.add(columnNames[j] + i);
            }
            tableValueV.add(iV);
        }
        JTable table = new MTable(tableValueV, columnNameV);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        table.setSelectionBackground(Color.YELLOW);
        table.setSelectionForeground(Color.RED);
        table.setRowHeight(30);
        scrollPane.setViewportView(table);
    }

    private class MTable extends JTable {

        private static final long serialVersionUID = 1L;

        public MTable(Vector<Vector<String>> rowData, Vector<String> columnNames) {
            super(rowData, columnNames);
        }

        @Override
        public JTableHeader getTableHeader() {
            // TODO Auto-generated method stub
            JTableHeader tableHeader = super.getTableHeader();
            tableHeader.setReorderingAllowed(false);
            DefaultTableCellRenderer hr = (DefaultTableCellRenderer) tableHeader.getDefaultRenderer();
            hr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
            return tableHeader;
        }

        public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
            DefaultTableCellRenderer cr = (DefaultTableCellRenderer)super.getDefaultRenderer(columnClass);
            cr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
            return cr;
        }
        
        public  boolean isCellEditable(int row ,int column) {
            return false;
            
        }
    }

}

 

定制表格

标签:eval   const   over   cto   tde   ati   for   jtable   final   

原文地址:https://www.cnblogs.com/dulute/p/10675487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!