欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Swing - JTable

程序员文章站 2024-02-01 14:34:16
...

JTable

  • 元素不可编辑
  • 设置Table-Header和Table-Body
  • 使用List填充Table

Swing - JTable


一:元素不可编辑

Idea快捷键复习:Shift*2 + Ctrl+O

1> 自定义MyTable类,继承JTable,重写其中的isSellEditable方法

public class MyTable extends JTable {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
}

2> 使用子类向上转型初始化父类

public class FrameTest extends JFrame {

    private final JTable table;

    public FrameTest(){
        table = new MyTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // ...

二:设置表头和表内容

如果不将JTable与JScrollPane绑定,那么JTable的标题栏将不会显示

String[] tableHead = {"学号","姓名","性别","年龄","专业"};
Object[][] tableBody = new Object[stu.size()+1][5];

table.setModel(new DefaultTableModel(tableBody,tableHead));

// DefaultTableModel构造函数原型
    /**
     *  Constructs a <code>DefaultTableModel</code> and initializes the table
     *  by passing <code>data</code> and <code>columnNames</code>
     *  to the <code>setDataVector</code>
     *  method. The first index in the <code>Object[][]</code> array is
     *  the row index and the second is the column index.
     *
     * @param data              the data of the table
     * @param columnNames       the names of the columns
     * @see #getDataVector
     * @see #setDataVector
     */
    public DefaultTableModel(Object[][] data, Object[] columnNames) {
        setDataVector(data, columnNames);
    }

三:使用List填充Table

1> Student.java

package xyz.xx.pojo;

public class Student {
    private int sid;
    private String sname;
    private int sage;
    private int ssex;
    private String smajor;

    public Student() { }

    public Student(int sid, String sname, int sage, int ssex, String smajor) {
        this.sid = sid;
        this.sname = sname;
        this.sage = sage;
        this.ssex = ssex;
        this.smajor = smajor;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSage() {
        return sage;
    }

    public void setSage(int sage) {
        this.sage = sage;
    }

    public int getSsex() {
        return ssex;
    }

    public void setSsex(int ssex) {
        this.ssex = ssex;
    }

    public String getSmajor() {
        return smajor;
    }

    public void setSmajor(String smajor) {
        this.smajor = smajor;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", sage=" + sage +
                ", ssex=" + ssex +
                ", smajor='" + smajor + '\'' +
                '}';
    }
}

2> MyTable.java

package xyz.xx.demo;

import javax.swing.*;

public class MyTable extends JTable {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
}

3> FrameTest.java

package xyz.xx.demo;

import xyz.xx.pojo.Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FrameTest extends JFrame {

    private final JTable table;

    public FrameTest(){
        table = new MyTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        List<Student> aList = new ArrayList<>();
        aList.add(new Student(1,"小明",18,0,"软件工程"));
        aList.add(new Student(2,"小朋",19,0,"软件工程"));
        aList.add(new Student(3,"小明明",25,1,"软件工程"));
        aList.add(new Student(4,"小明朋",11,0,"软件工程"));


        this.fillTable(aList);


        this.setTitle("测试窗体");
        this.setBounds(100,100,300,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);

    }

    /**
     * 使用List<Student> 内容填充Table
     * @param stu
     */
    public void fillTable(List<Student> stu){
        String[] tableHead = {"学号","姓名","性别","年龄","专业"};
        Object[][] tableBody = new Object[stu.size()+1][5];

        Iterator<Student> iterator = stu.iterator();
        int current = 0;
        while(iterator.hasNext()){
            Student temp = iterator.next();
            tableBody[current][0] = temp.getSid();
            tableBody[current][1] = temp.getSname();
            if(temp.getSsex()==0) {
                tableBody[current][2] = "女";
            }else{
                tableBody[current][2] = "男";
            }
            tableBody[current][3] = temp.getSage();
            tableBody[current][4] = temp.getSmajor();

            current++;
        }
        // 如果不将JTable与JScrollPane绑定,那么JTable的标题栏将不会显示
        table.setModel(new DefaultTableModel(tableBody,tableHead));
        JScrollPane jsp = new JScrollPane(table);
        this.add(jsp);
    }

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