• swing_tableModel 创建表格


      1 import java.awt.BorderLayout;
      2 import java.awt.EventQueue;
      3 import java.awt.Panel;
      4 import java.util.ArrayList;
      5 import java.util.List;
      6 
      7 import javax.swing.JFrame;
      8 import javax.swing.JPanel;
      9 import javax.swing.JScrollPane;
     10 import javax.swing.JTable;
     11 import javax.swing.border.EmptyBorder;
     12 import javax.swing.event.TableModelEvent;
     13 import javax.swing.event.TableModelListener;
     14 import javax.swing.plaf.basic.BasicTabbedPaneUI.TabbedPaneLayout;
     15 import javax.swing.table.TableModel;
     16 
     17 public class test extends JFrame {
     18 
     19     private JPanel contentPane;
     20 
     21     /**
     22      * Launch the application.
     23      */
     24     public static void main(String[] args) {
     25         EventQueue.invokeLater(new Runnable() {
     26             public void run() {
     27                 try {
     28                     test frame = new test();
     29                     frame.setVisible(true);
     30                 } catch (Exception e) {
     31                     e.printStackTrace();
     32                 }
     33             }
     34         });
     35     }
     36 
     37     /**
     38      * Create the frame.
     39      */
     40     public test() {
     41         List<Person> list = new ArrayList<>();
     42         list.add(new Person("1", "11", "111"));
     43         list.add(new Person("2", "22", "222"));
     44         list.add(new Person("3", "33", "333"));
     45         Model model = new Model();
     46         Panel parent = new Panel();
     47         JTable table = new JTable();
     48         table.setModel(model);// 为table 分配model
     49         JScrollPane jPane = new JScrollPane(table);// 将table放进滚动条 放进滚动条后可显示列名
     50         // getContentPane()得到JFrame最底层的JPanel 默认拥有一个不可直接向JFrame添加 需向JPanel添加
     51         getContentPane().add(jPane);
     52         table.setVisible(true);
     53 
     54         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     55         setBounds(100, 100, 450, 300);
     56 
     57     }
     58 
     59 }
     60 
     61 class Model implements TableModel {
     62     List<Person> List;
     63     Object[][] p = { { "11", "Taipei" }, { "22", "ChiaYi", }, { "33", "ChiaYi", }, { "44", "ChiaYi", } }; // 表格数据
     64     String n[] = { "AA", "BB" }; // 属性名
     65 
     66     @Override
     67     public Class<?> getColumnClass(int columnIndex) {
     68         return Object.class;
     69     }
     70 
     71     /*
     72      * 为表格传入列数
     73      * 
     74      * @see javax.swing.table.TableModel#getColumnCount()
     75      */
     76     @Override
     77     public int getColumnCount() {
     78 
     79         return n.length;
     80     }
     81 
     82     /*
     83      * 为表格传入属性名
     84      * 
     85      * @see javax.swing.table.TableModel#getColumnName(int)
     86      */
     87     @Override
     88     public String getColumnName(int columnIndex) {
     89         return n[columnIndex];
     90     }
     91 
     92     /*
     93      * 为表格传入行数
     94      * 
     95      * @see javax.swing.table.TableModel#getRowCount()
     96      */
     97     @Override
     98     public int getRowCount() {
     99         return p.length;
    100     }
    101 
    102     /*
    103      * 将数组中的数据传入表格
    104      * 
    105      * @see javax.swing.table.TableModel#getValueAt(int, int)
    106      */
    107     @Override
    108     public Object getValueAt(int rowIndex, int columnIndex) {
    109 
    110         return p[rowIndex][columnIndex];
    111     }
    112 
    113     /*
    114      * 设置表格是否可编辑
    115      * 
    116      * @see javax.swing.table.TableModel#isCellEditable(int, int)
    117      */
    118     @Override
    119     public boolean isCellEditable(int rowIndex, int columnIndex) {
    120         return true;
    121     }
    122 
    123     /*
    124      * 每当数据模型发生更改时,就将一个侦听器添加到被通知的列表中。
    125      * 
    126      * @see
    127      * javax.swing.table.TableModel#addTableModelListener(javax.swing.event.
    128      * TableModelListener)
    129      */
    130     @Override
    131     public void addTableModelListener(TableModelListener l) {
    132 
    133     }
    134 
    135     /*
    136      * 每当数据模型发生更改时,就从被通知的列表中移除一个侦听器。
    137      * 
    138      * @see
    139      * javax.swing.table.TableModel#removeTableModelListener(javax.swing.event.
    140      * TableModelListener)
    141      */
    142     @Override
    143     public void removeTableModelListener(TableModelListener l) {
    144 
    145     }
    146 
    147     /*
    148      * 将表格中的值设定为
    149      * 
    150      * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
    151      */
    152     @Override
    153     public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    154         p[rowIndex][columnIndex] = aValue;
    155 
    156     }
    157 
    158 }
  • 相关阅读:
    visualSVNYou don't have permission to access on this server
    怎样截取Http请求
    ASP.NET @page 指令详解
    我的XMLHelperC# XML操作基类(修改,删除,新增,创建)
    最新Visual Studio 2010 下载及学习资料
    redis主从哨兵和集群的区别
    uniapp nvue 支持iconfont
    Error (Error Code: 1175) during executing update command on table using MySQL
    org.apache.jasper.JasperException: Unable to compile class for JSP
    java.sql.SQLException: Access denied for user 'root'@'10.10.10.10' (using password: YES)
  • 原文地址:https://www.cnblogs.com/the-wang/p/7256935.html
Copyright © 2020-2023  润新知