定制表格

Posted dulute

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定制表格相关的知识,希望对你有一定的参考价值。

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;
            
        }
    }

}

 

以上是关于定制表格的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 引入本地定制 jar 包

VsCode 代码片段-提升研发效率

片段内的表格布局

定制表格

一个表格中选定的tr,显示在另一个表格中

实战再见Excel,我能自由定制表格