方法 作用
public JTable(Object[][] rowData,Object[] columnNames) 创建一个JTable对象,设置显示数据和表格的标题
public JTable (Vector rowData,Vector columnNames) 创建一个JTable对象,通过Vector设置显示数据和显示的标题
public JTable(TableModel dm) 使用TableModel创建表格
public TableColumnModel getColumnModel() 返回TableColumModel对象
import




    
 javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        String[] titles ={"姓名","班级","学院"};
        Object[][] userInfo = {
                {"A","1","计算机"},
                {"B","2","计算机"},
        JTable table = new JTable(userInfo,titles);
        JScrollPane scr = new JScrollPane(table);
        frame.add(scr);
        frame.setSize(300,200);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);

JTable使用时要加入到JScrollPane中。在使用JTable时,如果不将一个JTable加入到JScrollPane中,则表格的标题将无法显示

使用TableModel构建表格

在表格上加入一些单选钮或下拉列表框,则要借助于TableModel接口

TableModel接口

方法作用
public Class<?> getColumnClass(int columnIndex)得到表格中的每一列的数据类型
public int getColumnCount()返回表格中的列数
public String getColumnName(int columnIndex)返回表格中列的名字
public int getRowCount()返回表格中的行数
public Object getValueAt(int rowIndex,int columnIndex)根据行和列取得指定位置的元素
public boolean isCellEditable(int rowIndex,int columnIndex)返回单元格是否可编辑
public void setValueAt(Object aValue,int rowIndex,int columnIndex)设置表格内容

在一般的开发中很少直接实现以上的接口,而是使用其接口的子类AbstractTableModel和DefaultTableModel

TableColumnModel接口定义了许多与表格的行或列有关的方法

通常不会直接实现TableColumnModel接口,而是会通过JTable中的getColumnModel()方法取得TableColumnModel的实例

TableColumnModel接口

方法作用
public void addColumn(TableColumn aColumn)添加列
public void removeColumn(TableCoumn column)删除指定列
public TableColumn getColumn(int columnIndex)根据索引取得指定列
public int getColumnCount()取得全部列数
public Enumeration<TableColumn> getColumns()返回全部列

使用AbstractTableModel构建表格

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class DefaultTable extends AbstractTableModel{
    private String [] titles = {"姓名","年龄","性别"};
    private Object[][] userInfo = {
            {"A",20,"男"},
            {"A",25,"女"}
    @Override
    public int getRowCount() {
        return this.userInfo.length;
    @Override
    public int getColumnCount() {
        return this.titles.length;
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return this.userInfo[rowIndex][columnIndex];
    @Override
    public String getColumnName(int column) {
        return this.titles[column];
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return this.getValueAt(0,columnIndex).getClass();
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    public void setValueAt(Object newValue,int row,int col){
        this.userInfo[row][col] = newValue;
class TableColumnModelDemo{
    private JFrame frame = new JFrame("一");
    private JTable table = null;
    private DefaultTable defaultTable = new DefaultTable();
    private JComboBox sexList = new JComboBox();
    public TableColumnModelDemo(){
        this.table = new JTable(this.defaultTable);
        this.sexList.addItem("男");
        this.sexList.addItem("女");
        this.table.getColumnModel().getColumn(2).setCellEditor(
                new DefaultCellEditor(this.sexList)
        JScrollPane scr = new JScrollPane(this.table);
        JPanel toolBar = new JPanel();
        this.frame.add(toolBar, BorderLayout.NORTH);
        this.frame.add(scr,BorderLayout.CENTER);
        this.frame.setSize(400,200);
        this.frame.setVisible(true);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
public class Hello {
    public static void main(String[] args) {
        new TableColumnModelDemo();

在DefaultTable类中覆写了isCellEditable()方法,所以表格的每一项都是可编辑的

性别的选项使用了一个下拉列表的形式进行显示

使用DefaultTableModel可以对表格进行动态的操作,例如,增加行(列)、删除行(列)等

DefaultTableModel类

方法作用
public void addColumn(Object columnName)添加列,并指定列名称
public void addRow(Object[] rowData)添加行
public int getRowCount()返回表格的行数
public int getColumnCount()返回表格的列数
public void setRowCount(int rowCount)设置表格中的行数
public void setColumnCount(int columnCount)设置表格中的列数
public void removeRow(int row)删除表格中的指定行
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class ChangeTable implements ActionListener{
    private JFrame frame = new JFrame("一");
    private JTable table = null;
    private DefaultTableModel tableModel = null;
    private String[] titles = {"姓名","年龄","性别"};
    private Object[][] userInfo ={
            {"A","21","男"},
            {"B","21","女"}
    private JButton addRowBtn = new JButton("添加行");
    private JButton removeRowBtu = new JButton("删除行");
    private JButton addColBtn = new JButton("添加列");
    private JButton removeColBtn = new JButton("删除列");
    public ChangeTable(){
        this.tableModel = new DefaultTableModel(this.userInfo,this.titles);
        this.table = new JTable(this.tableModel);
        JScrollPane scr = new JScrollPane(this.table);
        JPanel toolBar = new JPanel();
        toolBar.add(this.addRowBtn);
        toolBar.add(this.removeRowBtu);
        toolBar.add(this.addColBtn);
        toolBar.add(this.removeColBtn);
        this.frame.add(toolBar, BorderLayout.NORTH);
        this.frame.add(scr,BorderLayout.CENTER);
        this.frame.setSize(400,200);
        this.frame.setVisible(true);
        this.addRowBtn.addActionListener(this);
        this.removeRowBtu.addActionListener(this);
        this.addColBtn.addActionListener(this);
        this.removeColBtn.addActionListener(this);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==this.addColBtn){
            this.tableModel.addColumn("新增列");
        if(e.getSource()==this.addRowBtn){
            this.tableModel.addRow(new Object[]{});
        if(e.getSource()==this.removeColBtn){
            int colCount = this.tableModel.getColumnCount();
            if(colCount>=0){
                TableColumnModel columnModel = this.table.getColumnModel();
                TableColumn tableColumn = columnModel.getColumn(colCount);
                columnModel.removeColumn(tableColumn);
                this.tableModel.setColumnCount(colCount);
        if(e.getSource()==this.removeRowBtu){
            int rowCount = this.tableModel.getRowCount()-1;
            if(rowCount>=0){
                this.tableModel.removeRow(rowCount);
                this.tableModel.setRowCount(rowCount);
public class Hello




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

image-20220219203655198

  • 两行CSS让页面提升了近7倍渲染性能!
  • 前端怎么样限制用户截图?
  •