码迷,mamicode.com
首页 > 编程语言 > 详细

【Java-GUI】10 Swing06 JTable

时间:2021-03-12 12:50:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:edit   obj   lazy   bst   seo   实现   jta   edr   remove   

静态数据表格的样子:

package cn.dzz;

import javax.swing.*;
import java.awt.*;

public class JTable {

    JFrame jFrame = new JFrame("简单表格");

    Object[] titleList = {"姓名", "年龄", "性别"};
    Object[][] dataList = {
            {"李清照", 29, "女性"},
            {"苏格拉底", 56, "男性"},
            {"李白", 35, "男性"},
            {"弄玉", 18, "女性"},
            {"虎头", 2, "男性"},
    };

    public void init() {
        javax.swing.JTable jTable = new javax.swing.JTable(dataList, titleList);
        jFrame.add(new JScrollPane(jTable));

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new JTable().init();
    }


}

程序效果:

技术图片

简单的表格提供了一些基础功能:

1、可以选中某一条记录

2、可以对显示的记录的某一个字段进行修改

3、可以对列进行移动

然后是对Table的一些设置API:

package cn.dzz;

import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;

public class JTable {

    JFrame jFrame = new JFrame("简单表格");

    Object[] titleList = {"姓名", "年龄", "性别"};
    Object[][] dataList = {
            {"李清照", 29, "女性"},
            {"苏格拉底", 56, "男性"},
            {"李白", 35, "男性"},
            {"弄玉", 18, "女性"},
            {"虎头", 2, "男性"},
    };

    public void init() {
        javax.swing.JTable jTable = new javax.swing.JTable(dataList, titleList);

        // 默认的选择模式
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        // 设置列宽
        TableColumn jTableColumn1 = jTable.getColumn(titleList[0]);
        jTableColumn1.setMinWidth(40);
        TableColumn jTableColumn3 = jTable.getColumn(titleList[2]);
        jTableColumn3.setMaxWidth(50);
                

        jFrame.add(new JScrollPane(jTable));

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new JTable().init();
    }


}

 

使用TableModel来代替二维数组渲染数据:

可以自定义实现抽象表格模型类,监听获取选中的单元格:

package cn.dzz;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class TableModelDemo {

    JFrame jFrame = new JFrame("tableModel");
    Object[] titleList = {"姓名", "年龄", "性别"};
    Object[][] dataList = {
            {"李清照", 29, "女性"},
            {"苏格拉底", 56, "男性"},
            {"李白", 35, "男性"},
            {"弄玉", 18, "女性"},
            {"虎头", 2, "男性"},
    };
    private Vector titleVector = new Vector();
    private Vector<Vector> dataVector = new Vector();

    public void init() {

        for (int i = 0; i < titleList.length; i++) {
            titleVector.add(titleList[i]);
        }

        for (int i = 0; i < dataList.length; i++) {
            Vector t = new Vector();
            for (int j = 0; j < dataList[i].length; j++) {
                t.add(dataList[i][j]);
            }
            dataVector.add(t);
        }

// javax.swing.JTable jTable =  new javax.swing.JTable(dataVector, titleVector);

        MyTableModel myTableModel = new MyTableModel();
        javax.swing.JTable jTable =  new javax.swing.JTable(myTableModel);
        jFrame.add(new JScrollPane(jTable));

       // 选中监听
        JButton jButton = new JButton("获取选中的行的数据");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedColumn = jTable.getSelectedColumn();
                int selectedRow = jTable.getSelectedRow();
                System.out.println("当前选中行的索引 " + selectedRow);
                System.out.println("当前选中列的索引 " + selectedColumn);
                Object valueAt = myTableModel.getValueAt(selectedRow, selectedColumn);
                System.out.println("当前选中行的第一个单元格的内容为:" + valueAt);
            }
        });

        jFrame.add(jButton, BorderLayout.SOUTH);


        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new TableModelDemo().init();
    }


    private class MyTableModel extends AbstractTableModel {
        @Override
        public int getRowCount() {
            return dataVector.size();
        }

        @Override
        public int getColumnCount() {
            return titleVector.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return dataVector.get(rowIndex).get(columnIndex);
        }

        @Override
        public String getColumnName(int column) {
            return titleVector.get(column).toString();
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }
    }


}

 

Java也提供默认的TableModel可使用,DefaultTableModel

加行,加列,删除行,选中就都有了

package cn.dzz;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class TableModelDemo {

    JFrame jFrame = new JFrame("tableModel");
    Object[] titleList = {"姓名", "年龄", "性别"};
    Object[][] dataList = {
            {"李清照", 29, "女性"},
            {"苏格拉底", 56, "男性"},
            {"李白", 35, "男性"},
            {"弄玉", 18, "女性"},
            {"虎头", 2, "男性"},
    };
    private Vector titleVector = new Vector();
    private Vector<Vector> dataVector = new Vector();

    public void init() {

        for (int i = 0; i < titleList.length; i++) {
            titleVector.add(titleList[i]);
        }

        for (int i = 0; i < dataList.length; i++) {
            Vector t = new Vector();
            for (int j = 0; j < dataList[i].length; j++) {
                t.add(dataList[i][j]);
            }
            dataVector.add(t);
        }

        // javax.swing.JTable jTable =  new javax.swing.JTable(dataVector, titleVector);

        MyTableModel myTableModel = new MyTableModel();
        //javax.swing.JTable jTable =  new javax.swing.JTable(myTableModel);

        DefaultTableModel tableModel = new DefaultTableModel(dataVector, titleVector);
        javax.swing.JTable jTable =  new javax.swing.JTable(tableModel);
        jFrame.add(new JScrollPane(jTable));

        // 选中监听
        JButton jButton = new JButton("获取选中的行的数据");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedColumn = jTable.getSelectedColumn();
                int selectedRow = jTable.getSelectedRow();
                System.out.println("当前选中行的索引 " + selectedRow);
                System.out.println("当前选中列的索引 " + selectedColumn);
                Object valueAt = myTableModel.getValueAt(selectedRow, selectedColumn);
                System.out.println("当前选中行的第一个单元格的内容为:" + valueAt);
            }
        });

        JButton btnForAddNewRow = new JButton("新加一行");
        JButton btnForAddNewColumn = new JButton("新加一列");
        JButton btnForDelOneRow = new JButton("删除一行");

        btnForAddNewRow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tableModel.addRow(new Object[]{"柳岩", 18, "女性"});
            }
        });

        btnForAddNewColumn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tableModel.addColumn("职业");
            }
        });

        btnForDelOneRow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = jTable.getSelectedRow();
                tableModel.removeRow(selectedRow);
            }
        });

        // jFrame.add(jButton, BorderLayout.SOUTH);
        JPanel jPanel = new JPanel();
        jPanel.add(jButton);
        jPanel.add(btnForAddNewRow);
        jPanel.add(btnForAddNewColumn);
        jPanel.add(btnForDelOneRow);

        jFrame.add(jPanel, BorderLayout.SOUTH);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new TableModelDemo().init();
    }


    private class MyTableModel extends AbstractTableModel {
        @Override
        public int getRowCount() {
            return dataVector.size();
        }

        @Override
        public int getColumnCount() {
            return titleVector.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return dataVector.get(rowIndex).get(columnIndex);
        }

        @Override
        public String getColumnName(int column) {
            return titleVector.get(column).toString();
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }
    }


}

 

【Java-GUI】10 Swing06 JTable

标签:edit   obj   lazy   bst   seo   实现   jta   edr   remove   

原文地址:https://www.cnblogs.com/mindzone/p/14514131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!