Java+Swing: jTable和tableModel以及表格内的样式渲染 整理7

2023-12-13 04:10:49
package com.test;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

/**
 * @Author:xiexu
 * @Date:2023/12/8 14:53
 */
public class JtableTest extends JFrame {

    public JtableTest() {
        super("测试Jtable");

        Vector<String> column = new Vector<>();
        column.addElement("编号");
        column.addElement("姓名");
        column.addElement("学号");
        column.addElement("家乡");
        column.addElement("语文");
        column.addElement("数学");
        column.addElement("英语");
        column.addElement("总分");

        Vector<Vector<Object>> data = new Vector<>();
        Vector<Object> rowVector1 = new Vector<>();
        rowVector1.addElement("1");
        rowVector1.addElement("张三");
        rowVector1.addElement("2112001136");
        rowVector1.addElement("江苏");
        rowVector1.addElement("89");
        rowVector1.addElement("92");
        rowVector1.addElement("80");
        rowVector1.addElement("261");

        Vector<Object> rowVector2 = new Vector<>();
        rowVector2.addElement("2");
        rowVector2.addElement("李四");
        rowVector2.addElement("2112001137");
        rowVector2.addElement("北京");
        rowVector2.addElement("89");
        rowVector2.addElement("92");
        rowVector2.addElement("85");
        rowVector2.addElement("266");

        data.addElement(rowVector1);
        data.addElement(rowVector2);

        // tableModel : 和 jtable 关联后, 只需要更新 model 就能把数据的变化反应到jtable中;
        DefaultTableModel tableModel = new DefaultTableModel();
        tableModel.setDataVector(data, column);


        // Jtable
        JTable jTable = new JTable(tableModel);
        // 设置表头的样式
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setFont(new Font("微软雅黑", Font.BOLD, 18));
        tableHeader.setForeground(Color.red);
        // 设置表格体的样式
        jTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.black);  // 设置表格线颜色
        jTable.setRowHeight(30);  // 设置行高
        // 设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        
        // 获取内容面板
        Container contentPane = getContentPane();
        JScrollPane jScrollPane = new JScrollPane(jTable);  // jtable 需要放在JScrollPane面板上,放在内容面板上默认是不展示列头部的
        contentPane.add(jScrollPane);

        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        new JtableTest();
    }
}

?

上述的代码已经可以实现表格以及数据在内容面板上的展现,但还有很多可以优化的地方

1. 表格中的内容不能修改? 并且 每一列的头部 不会因为数据的变化而变化,是固定不变的?

在外部重新自定义一个方法?

// 自定义方法
class StudentTableModel extends DefaultTableModel {
    static Vector<String> column = new Vector<>();

    static {
        column.addElement("编号");
        column.addElement("编号");
        column.addElement("姓名");
        column.addElement("学号");
        column.addElement("家乡");
        column.addElement("语文");
        column.addElement("数学");
        column.addElement("英语");
        column.addElement("总分");
    }

    public StudentTableModel() {
        super(null, column);
    }

    private static StudentTableModel studentTableModel = new StudentTableModel();  // 饿汉式
    public static StudentTableModel assembleModel(Vector<Vector<Object>> data){ // 组装数据  -> 肯定有返回对象,即要设置返回类型
        studentTableModel.setDataVector(data,column);
        return studentTableModel;
    }
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

}

基于上述自定义的方法,那么之前的代码就可以修改下了

package com.test;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

/**
 * @Author:xiexu
 * @Date:2023/12/8 14:53
 */
public class JtableTest extends JFrame {

    public JtableTest() {
        super("测试Jtable");

//        Vector<String> column = new Vector<>();
//        column.addElement("编号");
//        column.addElement("姓名");
//        column.addElement("学号");
//        column.addElement("家乡");
//        column.addElement("语文");
//        column.addElement("数学");
//        column.addElement("英语");
//        column.addElement("总分");

        Vector<Vector<Object>> data = new Vector<>();
        Vector<Object> rowVector1 = new Vector<>();
        rowVector1.addElement("1");
        rowVector1.addElement("张三");
        rowVector1.addElement("2112001136");
        rowVector1.addElement("江苏");
        rowVector1.addElement("89");
        rowVector1.addElement("92");
        rowVector1.addElement("80");
        rowVector1.addElement("261");

        Vector<Object> rowVector2 = new Vector<>();
        rowVector2.addElement("2");
        rowVector2.addElement("李四");
        rowVector2.addElement("2112001137");
        rowVector2.addElement("北京");
        rowVector2.addElement("89");
        rowVector2.addElement("92");
        rowVector2.addElement("85");
        rowVector2.addElement("266");

        data.addElement(rowVector1);
        data.addElement(rowVector2);

        // tableModel : 和 jtable 关联后, 只需要更新 model 就能把数据的变化反应到jtable中;
//        DefaultTableModel tableModel = new DefaultTableModel();
//        tableModel.setDataVector(data, column);

        // 调用静态的方法
        StudentTableModel studentTableModel = StudentTableModel.assembleModel(data);

        // Jtable
        JTable jTable = new JTable(studentTableModel);
        // 设置表头的样式
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setFont(new Font("微软雅黑", Font.BOLD, 18));
        tableHeader.setForeground(Color.red);
        // 设置表格体的样式
        jTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.black);  // 设置表格线颜色
        jTable.setRowHeight(30);  // 设置行高
        // 设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        // 获取内容面板
        Container contentPane = getContentPane();
        JScrollPane jScrollPane = new JScrollPane(jTable);  // jtable 需要放在JScrollPane面板上,放在内容面板上默认是不展示列头部的
        contentPane.add(jScrollPane);

        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        new JtableTest();
    }
}


// 自定义方法
class StudentTableModel extends DefaultTableModel {
    static Vector<String> column = new Vector<>();

    static {
        column.addElement("编号");
        column.addElement("编号");
        column.addElement("姓名");
        column.addElement("学号");
        column.addElement("家乡");
        column.addElement("语文");
        column.addElement("数学");
        column.addElement("英语");
        column.addElement("总分");
    }

    public StudentTableModel() {
        super(null, column);
    }

    private static StudentTableModel studentTableModel = new StudentTableModel();  // 饿汉式
    public static StudentTableModel assembleModel(Vector<Vector<Object>> data){ // 组装数据  -> 肯定有返回对象,即要设置返回类型
        studentTableModel.setDataVector(data,column);
        return studentTableModel;
    }
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    // 添加get方法 获取列名

    public static Vector<String> getColumn() {
        return column;
    }
}

?

?2.?设置表格列的渲染方式

自定义一个方法并继承??DefaultTableCellRenderer

class StudentCellRender extends DefaultTableCellRenderer {

    // 在每一行每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (row % 2 == 0 ) {
            setBackground(Color.LIGHT_GRAY);
        } else {
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);  // 设置水平居中
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

?设置渲染样式

//设置表格列的渲染方式
        Vector<String> column = StudentTableModel.getColumn();  // 需要添加get方法

        StudentCellRender studentCellRender = new StudentCellRender();


        for (int i=0; i<column.size(); i++) {
            TableColumn c = jTable.getColumn(column.get(i));
            c.setCellRenderer(studentCellRender);

            if (i == 0) {
                c.setPreferredWidth(50);  
                c.setResizable(false);  // 设置第一列宽度不可变
            }
        }

?

完整代码:

package com.test;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

/**
 * @Author:xiexu
 * @Date:2023/12/8 14:53
 */
public class JtableTest extends JFrame {

    public JtableTest() {
        super("测试Jtable");

//        Vector<String> column = new Vector<>();
//        column.addElement("编号");
//        column.addElement("姓名");
//        column.addElement("学号");
//        column.addElement("家乡");
//        column.addElement("语文");
//        column.addElement("数学");
//        column.addElement("英语");
//        column.addElement("总分");

        Vector<Vector<Object>> data = new Vector<>();
        Vector<Object> rowVector1 = new Vector<>();
        rowVector1.addElement("1");
        rowVector1.addElement("张三");
        rowVector1.addElement("2112001136");
        rowVector1.addElement("江苏");
        rowVector1.addElement("89");
        rowVector1.addElement("92");
        rowVector1.addElement("80");
        rowVector1.addElement("261");

        Vector<Object> rowVector2 = new Vector<>();
        rowVector2.addElement("2");
        rowVector2.addElement("李四");
        rowVector2.addElement("2112001137");
        rowVector2.addElement("北京");
        rowVector2.addElement("89");
        rowVector2.addElement("92");
        rowVector2.addElement("85");
        rowVector2.addElement("266");

        data.addElement(rowVector1);
        data.addElement(rowVector2);

        // tableModel : 和 jtable 关联后, 只需要更新 model 就能把数据的变化反应到jtable中;
//        DefaultTableModel tableModel = new DefaultTableModel();
//        tableModel.setDataVector(data, column);

        // 调用静态的方法
        StudentTableModel studentTableModel = StudentTableModel.assembleModel(data);

        // Jtable
        JTable jTable = new JTable(studentTableModel);
        // 设置表头的样式
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setFont(new Font("微软雅黑", Font.BOLD, 18));
        tableHeader.setForeground(Color.red);
        // 设置表格体的样式
        jTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.black);  // 设置表格线颜色
        jTable.setRowHeight(30);  // 设置行高
        // 设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        //设置表格列的渲染方式
        Vector<String> column = StudentTableModel.getColumn();

        StudentCellRender studentCellRender = new StudentCellRender();


        for (int i=0; i<column.size(); i++) {
            TableColumn c = jTable.getColumn(column.get(i));
            c.setCellRenderer(studentCellRender);

            if (i == 0) {
                c.setPreferredWidth(50);
                c.setResizable(false);
            }
        }

        // 获取内容面板
        Container contentPane = getContentPane();
        JScrollPane jScrollPane = new JScrollPane(jTable);  // jtable 需要放在JScrollPane面板上,放在内容面板上默认是不展示列头部的
        contentPane.add(jScrollPane);

        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        new JtableTest();
    }
}

class StudentCellRender extends DefaultTableCellRenderer {

    // 在每一行每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (row % 2 == 0 ) {
            setBackground(Color.LIGHT_GRAY);
        } else {
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);  // 设置水平居中
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

// 自定义方法
class StudentTableModel extends DefaultTableModel {
    static Vector<String> column = new Vector<>();

    static {
        column.addElement("编号");
        column.addElement("编号");
        column.addElement("姓名");
        column.addElement("学号");
        column.addElement("家乡");
        column.addElement("语文");
        column.addElement("数学");
        column.addElement("英语");
        column.addElement("总分");
    }

    public StudentTableModel() {
        super(null, column);
    }

    private static StudentTableModel studentTableModel = new StudentTableModel();  // 饿汉式
    public static StudentTableModel assembleModel(Vector<Vector<Object>> data){ // 组装数据  -> 肯定有返回对象,即要设置返回类型
        studentTableModel.setDataVector(data,column);
        return studentTableModel;
    }
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    // 添加get方法 获取列名

    public static Vector<String> getColumn() {
        return column;
    }
}

?

?

?

文章来源:https://blog.csdn.net/m0_61495539/article/details/134881037
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。