zoukankan      html  css  js  c++  java
  • 定制表格

    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;
                
            }
        }
    
    }
  • 相关阅读:
    每天改进一点点之改进日志收集系统 原创: 赵建鹏 雪球工程师团队 2018-03-23
    Locust
    ('' or 60)
    python动态获取对象的属性和方法 (转载)
    MySQL 8.0: From SQL Tables to JSON Documents (and back again)
    词典型 遍历键 顺序
    子系统权限栏目 自己生成 自己控制
    Redis 单线程却能支撑高并发
    OPPO数据中台之基石:基于Flink SQL构建实数据仓库
    技术干货丨如何在VIPKID中构建MQ服务
  • 原文地址:https://www.cnblogs.com/dulute/p/10675487.html
Copyright © 2011-2022 走看看