简单实现JTable增删改

简单实现JTable增删改

本public类继承自JFrame,实现了ActionListener 接口。

简单实现JTable的增删改:

  1 import javax.swing.*;
  2 import javax.swing.event.TableModelEvent;
  3 import javax.swing.event.TableModelListener;
  4 import javax.swing.table.DefaultTableCellRenderer;
  5 import javax.swing.table.DefaultTableColumnModel;
  6 import javax.swing.table.DefaultTableModel;
  7 import java.awt.*;
  8 import java.awt.event.ActionEvent;
  9 import java.awt.event.ActionListener;
 10 import java.awt.event.MouseAdapter;
 11 import java.awt.event.MouseEvent;
 12 
 13 public class TableDemo extends JFrame implements ActionListener {
 14     //操作面板,存放增删改按钮
 15     private JPanel controlPanel;
 16     //定义表格的数据模型
 17     private DefaultTableModel model;
 18     //定义一个表格
 19     private  JTable table;
 20     //定义一个滚动面板,用来放置表格
 21     private  JScrollPane scrollPane;
 22     //增删改按钮
 23     private JButton addBtn, deleteBtn, updateBtn;
 24 
 25     public TableDemo() throws HeadlessException {
 26         //表头
 27         String[] head={"学号","姓名","成绩"};
 28         //初始化数据
 29         Object[][] datas  = new String[][] {
 30                 {"101","Jason","617"},
 31                 {"102","Nick","609"},
 32                 {"103","William","599"},
 33                 {"104","Charles","588"} };
 34         //初始化表格数据模型
 35         model = new DefaultTableModel(datas, head);
 36         //用表格模型初始化表格
 37         table = new JTable(model);
 38         //初始化滚动面板
 39         scrollPane = new JScrollPane(table);
 40         //初始化按钮
 41         addBtn = new JButton("增加");
 42         deleteBtn = new JButton("删除");
 43         updateBtn = new JButton("修改");
 44         //给按钮添加监听事件
 45         addBtn.addActionListener(this);
 46         deleteBtn.addActionListener(this);
 47         updateBtn.addActionListener(this);
 48         //初始化控制面板,添加相关按钮
 49         controlPanel = new JPanel();
 50         controlPanel.add(addBtn);
 51         controlPanel.add(deleteBtn);
 52         controlPanel.add(updateBtn);
 53         //设置面板布局,上面控制按钮,中间放表格列表
 54         add(controlPanel, BorderLayout.NORTH);
 55         add(scrollPane, BorderLayout.CENTER);
 56         //设置表格中的对齐方式
 57         DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
 58         renderer.setHorizontalAlignment(SwingConstants.CENTER);
 59         //通过列名获取表格的列对象,并将上面设好对齐方式应用到指定列上
 60         table.getColumn("学号").setCellRenderer(renderer);
 61         table.getColumn("姓名").setCellRenderer(renderer);
 62 
 63         //设置表格宽度,以及拖动的最大和最小宽度
 64         DefaultTableColumnModel dcm = (DefaultTableColumnModel)table.getColumnModel();
 65         dcm.getColumn(0).setPreferredWidth(60);
 66         dcm.getColumn(0).setMinWidth(45);
 67         dcm.getColumn(0).setMaxWidth(75);
 68         //设置行高
 69         //table.setRowHeight(25);
 70 
 71         //加上鼠标监听事件
 72         table.addMouseListener(new MouseAdapter() {
 73             @Override
 74             public void mouseClicked(MouseEvent e) {
 75                 int row = table.getSelectedRow();
 76                 int col = table.getSelectedColumn();
 77                 System.out.println("选中的=>" + table.getValueAt(row, col));
 78             }
 79         });
 80         //加上表格数据模型监听事件
 81         table.getModel().addTableModelListener(new TableModelListener() {
 82             @Override
 83             public void tableChanged(TableModelEvent e) {
 84                 System.out.println("表格模型事件e=>" + e);
 85                 if (e.getType() == TableModelEvent.UPDATE){
 86                     int row = table.getSelectedRow();
 87                     int col = table.getSelectedColumn();
 88                     System.out.println("修改后=>" + table.getValueAt(row, col));
 89                 }
 90             }
 91         });
 92         //允许点击JTable的表头进行升降排序
 93         table.setAutoCreateRowSorter(true);
 94 
 95         //setBounds(100,100,600,400);
 96         setSize(600,400);
 97         //居中显示窗口
 98         setLocationRelativeTo(null);
 99         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
100         //设为可见,后面弹出对话框窗口时,可能需要放到别的函数里
101         setVisible(true);
102     }
103 
104     @Override
105     public void actionPerformed(ActionEvent e) {
106         if (e.getSource() == addBtn){
107             model.addRow(new Object[]{"105","Martin","603"});
108         }
109         else if(e.getSource() == deleteBtn){
110             int i = table.getSelectedRow();
111             if (i == -1) {
112                 JOptionPane.showMessageDialog(this, "请选中一条记录");
113                 return;
114             }
115             model.removeRow(i);
116         }
117         else if (e.getSource() == updateBtn){
118             int row = table.getSelectedRow();
119             if (row == -1){
120                 JOptionPane.showMessageDialog(this,"请选中一条记录");
121                 return;
122             }
123             //自己闹着玩的,给表格中的第3列分数自动修改加1
124             int newValue = Integer.parseInt( model.getValueAt(row, 2).toString() ) + 1;
125             model.setValueAt(Integer.toString(newValue) , row, 2);
126         }
127     }
128     
129     public static void main(String[] args) {
130         new TableDemo();
131     }
132 }

结束。

简单实现JTable增删改

上一篇:浅谈Mybatis


下一篇:PS中最容易犯的9种错误