• JTable表格案例


      1 package com.szht.gpy.frame;
      2 import java.awt.BorderLayout;
      3 import java.awt.event.ActionEvent;
      4 import java.awt.event.ActionListener;
      5 import java.awt.event.MouseAdapter;
      6 import java.awt.event.MouseEvent;
      7 import javax.swing.JButton;
      8 import javax.swing.JFrame;
      9 import javax.swing.JLabel;
     10 import javax.swing.JPanel;
     11 import javax.swing.JScrollPane;
     12 import javax.swing.JTable;
     13 import javax.swing.JTextField;
     14 import javax.swing.ListSelectionModel;
     15 import javax.swing.table.DefaultTableModel;
     16 //维护表格
     17 public class JTableDefaultTableModelTest extends JFrame{
     18 
     19     private DefaultTableModel tableModel;   //表格模型对象
     20     private JTable table;
     21     private JTextField aTextField;
     22     private JTextField bTextField;
     23     
     24     public JTableDefaultTableModelTest()
     25     {
     26         super();
     27         setTitle("表格");
     28         setBounds(100,100,500,400);
     29         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     30         String[] columnNames = {"A","B"};   //列名
     31         String [][]tableVales={{"A1","B1"},{"A2","B2"},{"A3","B3"},{"A4","B4"},{"A5","B5"}}; //数据
     32         tableModel = new DefaultTableModel(tableVales,columnNames);
     33         table = new JTable(tableModel);
     34         JScrollPane scrollPane = new JScrollPane(table);   //支持滚动
     35         getContentPane().add(scrollPane,BorderLayout.CENTER);
     36         //jdk1.6
     37         //排序:
     38         //table.setRowSorter(new TableRowSorter(tableModel));
     39         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  //单选
     40         table.addMouseListener(new MouseAdapter(){    //鼠标事件
     41             public void mouseClicked(MouseEvent e){
     42                 int selectedRow = table.getSelectedRow(); //获得选中行索引
     43                 Object oa = tableModel.getValueAt(selectedRow, 0);
     44                 Object ob = tableModel.getValueAt(selectedRow, 1);
     45                 aTextField.setText(oa.toString());  //给文本框赋值
     46                 bTextField.setText(ob.toString());
     47             }
     48         });
     49         scrollPane.setViewportView(table);
     50         final JPanel panel = new JPanel();
     51         getContentPane().add(panel,BorderLayout.SOUTH);
     52         panel.add(new JLabel("A: "));
     53         aTextField = new JTextField("A4",10);
     54         panel.add(aTextField);
     55         panel.add(new JLabel("B: "));
     56         bTextField = new JTextField("B4",10);
     57         panel.add(bTextField);
     58         final JButton addButton = new JButton("添加");   //添加按钮
     59         addButton.addActionListener(new ActionListener(){//添加事件
     60             public void actionPerformed(ActionEvent e){
     61                 String []rowValues = {aTextField.getText(),bTextField.getText()};
     62                 tableModel.addRow(rowValues);  //添加一行
     63                 int rowCount = table.getRowCount() +1;   //行数加上1
     64                 aTextField.setText("A"+rowCount);
     65                 bTextField.setText("B"+rowCount);
     66             }
     67         });
     68         panel.add(addButton);  
     69         
     70         final JButton updateButton = new JButton("修改");   //修改按钮
     71         updateButton.addActionListener(new ActionListener(){//添加事件
     72             public void actionPerformed(ActionEvent e){
     73                 int selectedRow = table.getSelectedRow();//获得选中行的索引
     74                 if(selectedRow!= -1)   //是否存在选中行
     75                 {
     76                     //修改指定的值:
     77                     tableModel.setValueAt(aTextField.getText(), selectedRow, 0);
     78                     tableModel.setValueAt(bTextField.getText(), selectedRow, 1);
     79                     //table.setValueAt(arg0, arg1, arg2)
     80                 }
     81             }
     82         });
     83         panel.add(updateButton);
     84         
     85         final JButton delButton = new JButton("删除");
     86         delButton.addActionListener(new ActionListener(){//添加事件
     87             public void actionPerformed(ActionEvent e){
     88                 int selectedRow = table.getSelectedRow();//获得选中行的索引
     89                 if(selectedRow!=-1)  //存在选中行
     90                 {
     91                     tableModel.removeRow(selectedRow);  //删除行
     92                 }
     93             }
     94         });
     95         panel.add(delButton);
     96     }
     97     /**
     98      * @param args
     99      */
    100     public static void main(String[] args) {
    101         // TODO Auto-generated method stub
    102         JTableDefaultTableModelTest jTableDefaultTableModelTest = new JTableDefaultTableModelTest();
    103         jTableDefaultTableModelTest.setVisible(true);
    104     }
    105 
    106 }
  • 相关阅读:
    axios核心技术---1.HTTP相关
    Leetcode刷题
    Java入门13---Optional处理null对象
    Java入门12---Stream
    谈谈前后端分离及认证选择
    react监控props的变化
    如何使用懒加载
    CSS文字超出省略
    React的slot插槽
    React 的函数组件和类组件中的props
  • 原文地址:https://www.cnblogs.com/sunBinary/p/9870939.html
Copyright © 2020-2023  润新知