歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java表格模型事件示例

Java表格模型事件示例:

  1. package com.han;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Container;  
  5. import java.awt.Point;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.awt.event.InputEvent;  
  9. import java.awt.event.MouseAdapter;  
  10. import java.awt.event.MouseEvent;  
  11. import java.awt.event.WindowAdapter;  
  12. import java.awt.event.WindowEvent;  
  13. import java.util.Arrays;  
  14. import java.util.Vector;  
  15.   
  16. import javax.swing.JButton;  
  17. import javax.swing.JFrame;  
  18. import javax.swing.JLabel;  
  19. import javax.swing.JOptionPane;  
  20. import javax.swing.JPanel;  
  21. import javax.swing.JScrollPane;  
  22. import javax.swing.JTable;  
  23. import javax.swing.JTextField;  
  24. import javax.swing.ListSelectionModel;  
  25. import javax.swing.event.TableModelEvent;  
  26. import javax.swing.event.TableModelListener;  
  27. import javax.swing.table.DefaultTableModel;  
  28. import javax.swing.table.JTableHeader;  
  29. import javax.swing.table.TableColumnModel;  
  30.   
  31. /** 
  32.  * 表格模型事件示例 
  33.  * <p> 
  34.  * <li>A component generally gains the focus when the user clicks it,  
  35.  * or when the user tabs between components, or otherwise interacts  
  36.  * with a component. A component can also be given the focus programmatically, 
  37.  * such as when its containing frame or dialog-box is made visible.  
  38.  * The snippet of the codes below shows how to give a particular component   
  39.  * the focus every time the window gains the focus. 
  40.  *  
  41.  * @author HAN 
  42.  *  
  43.  */  
  44. public class TableModelEvent_1 extends JFrame {  
  45.   
  46.     /** 
  47.      *  
  48.      */  
  49.     private static final long serialVersionUID = -8581492063632813033L;  
  50.   
  51.     public TableModelEvent_1() {  
  52.         // TODO Auto-generated constructor stub   
  53.         final Container container = getContentPane();  
  54.         Vector<String> tableColumnNames = new Vector<String>();  
  55.         tableColumnNames.add("A");  
  56.         tableColumnNames.add("B");  
  57.         Vector<Vector<String>> tableValues = new Vector<Vector<String>>();  
  58.         for (int i = 1; i < 5; i++) {  
  59.             Vector<String> vector = new Vector<String>();  
  60.             vector.add("A" + i);  
  61.             vector.add("B" + i);  
  62.             tableValues.add(vector);  
  63.         }  
  64.         final DefaultTableModel defaultTableModel = new DefaultTableModel(  
  65.                 tableValues, tableColumnNames);  
  66.         final JTable table = new JTable(defaultTableModel);  
  67.         JScrollPane scrollPane = new JScrollPane();  
  68.         scrollPane.setViewportView(table);  
  69.         container.add(scrollPane, BorderLayout.CENTER);  
  70.         JPanel panel = new JPanel();  
  71.         container.add(panel, BorderLayout.SOUTH);  
  72.         JLabel labelA = new JLabel("A: ");  
  73.         final JTextField textFieldA = new JTextField(15);  
  74.         JLabel labelB = new JLabel("B: ");  
  75.         final JTextField textFieldB = new JTextField(15);  
  76.         JButton buttonAdd = new JButton("添加");  
  77.         JButton buttonDel = new JButton("刪除");  
  78.         JButton buttonDeselected = new JButton("取消選擇");  
  79.         panel.add(labelA);  
  80.         panel.add(textFieldA);  
  81.         panel.add(labelB);  
  82.         panel.add(textFieldB);  
  83.         panel.add(buttonAdd);  
  84.         panel.add(buttonDel);  
  85.         panel.add(buttonDeselected);  
  86.         buttonAdd.addActionListener(new ActionListener() {  
  87.   
  88.             @Override  
  89.             public void actionPerformed(ActionEvent e) {  
  90.                 // TODO Auto-generated method stub   
  91.                 int[] selectedRows = table.getSelectedRows(); // table   
  92.                                                                 // 默認情況容許多行選擇   
  93.                 Vector<String> rowData = new Vector<String>();  
  94.                 rowData.add(textFieldA.getText());  
  95.                 rowData.add(textFieldB.getText());  
  96.                 if (selectedRows.length == 0) {  
  97.                     defaultTableModel.addRow(rowData);  
  98.                     textFieldA.setText(null);  
  99.                     textFieldB.setText(null);  
  100.                 } else if (selectedRows.length == 1) {  
  101. //                  System.out.println(selectedRows[0]);   
  102.                     defaultTableModel.insertRow(selectedRows[0] + 1, rowData);  
  103.                     textFieldA.setText(null);  
  104.                     textFieldB.setText(null);  
  105.                 } else {  
  106.                     JOptionPane.showMessageDialog(container,  
  107.                             "Your operation is forbidden""Warning",  
  108.                             JOptionPane.WARNING_MESSAGE);  
  109.                 }  
  110.             }  
  111.   
  112.         });  
  113.         buttonDel.addActionListener(new ActionListener() {  
  114.   
  115.             @Override  
  116.             public void actionPerformed(ActionEvent e) {  
  117.                 // TODO Auto-generated method stub   
  118.                 int[] selectedRows = table.getSelectedRows(); // table   
  119.                                                                 // 默認情況容許多行選擇   
  120.                 for (int i = 0; i < selectedRows.length; i++) {  
  121. //                  System.out.println(selectedRows[i]);   
  122.                     defaultTableModel.removeRow(selectedRows[i] - i);  
  123.                 }  
  124.             }  
  125.   
  126.         });  
  127.         buttonDeselected.addActionListener(new ActionListener() {  
  128.   
  129.             @Override  
  130.             public void actionPerformed(ActionEvent e) {  
  131.                 // TODO Auto-generated method stub   
  132.                 table.clearSelection();  
  133.             }  
  134.   
  135.         });  
  136.         scrollPane.addMouseListener(new MouseAdapter() {  
  137.             @Override  
  138.             public void mouseClicked(MouseEvent e) {  
  139. //               System.out.println("here");   
  140.                 if (e.getClickCount() == 1  
  141.                         && e.getButton() == MouseEvent.BUTTON1) {  
  142.                     table.clearSelection();  
  143.                 }  
  144.             }  
  145.         });  
  146.           
  147.         // make the text field focused every time the window is activated   
  148.         addWindowFocusListener(new WindowAdapter() {  
  149.   
  150.             @Override  
  151.             public void windowGainedFocus(WindowEvent e) {  
  152.                 // TODO Auto-generated method stub   
  153.                 textFieldA.requestFocus();   
  154.             }  
  155.               
  156.         });  
  157.   
  158.         // **************************************************************   
  159.         // This is a standard snippet to realize the desired column selection as in   
  160.         // Excel   
  161.         // **************************************************************   
  162.   
  163.         // row selection mode   
  164.         table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);  
  165.         // column selection mode   
  166.         TableColumnModel tableColumnModel = table.getColumnModel();  
  167.         tableColumnModel.getSelectionModel().setSelectionMode(  
  168.                 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);  
  169.         // allow the column selection (the row selection is allowed by default)   
  170.         table.setColumnSelectionAllowed(true);  
  171.         final JTableHeader tableHeader = table.getTableHeader();  
  172.         tableHeader.addMouseListener(new MouseAdapter() {  
  173.             @Override  
  174.             public void mouseClicked(MouseEvent e) {  
  175.                 // TODO Auto-generated method stub   
  176.                 // Extended modifiers represent the state of all modal keys,   
  177.                 // such as ALT, CTRL, META.   
  178.                 if (e.getClickCount() == 1  
  179.                         && e.getButton() == MouseEvent.BUTTON1) {  
  180.                     // Point point = new Point(e.getX(), e.getY());   
  181.                     Point point = new Point(e.getPoint());  
  182.                     int columnNum = tableHeader.columnAtPoint(point);  
  183.                     // System.out.println(columnNum);   
  184.                     int[] selectedColumns = table.getSelectedColumns();  
  185.                     if (selectedColumns.length != 0) {  
  186.                         // System.out.println("here1");   
  187.                         // System.out.println(InputEvent.getModifiersExText(e.getModifiersEx()));   
  188.                         if (e.getModifiersEx() == (InputEvent.CTRL_DOWN_MASK)) {  
  189. //                          System.out.println("ctrl");   
  190.                             if (Arrays.binarySearch(selectedColumns, columnNum) >= 0) {  
  191.                                 table.removeColumnSelectionInterval(columnNum,  
  192.                                         columnNum);  
  193.                             } else {  
  194.                                 table.addColumnSelectionInterval(columnNum,  
  195.                                         columnNum);  
  196.                             }  
  197.                         } else if (e.getModifiersEx() == (InputEvent.SHIFT_DOWN_MASK)) {  
  198.                             // System.out.println("shift");   
  199.                             table.setColumnSelectionInterval(  
  200.                                     selectedColumns[0], columnNum);  
  201.                         } else {  
  202.                             table.setColumnSelectionInterval(columnNum,  
  203.                                     columnNum);  
  204.                         }  
  205.                     } else {  
  206.                         // System.out.println("here2");   
  207.                         table.setColumnSelectionInterval(columnNum, columnNum);  
  208.                     }  
  209.                     table.setRowSelectionInterval(0, table.getRowCount() - 1);  
  210.                 }  
  211.             }  
  212.         });  
  213.           
  214.         defaultTableModel.addTableModelListener(new TableModelListener() {  
  215.   
  216.             @Override  
  217.             public void tableChanged(TableModelEvent e) {  
  218.                 // TODO Auto-generated method stub   
  219.                 int type = e.getType();  
  220.                 int firstRow = e.getFirstRow();  
  221. //              int lastRow = e.getLastRow(); // the last row seems to be always equal to the first row   
  222.                 int column = e.getColumn();  
  223.                 switch (type) {  
  224.                 case TableModelEvent.DELETE:  
  225.                     System.out.print("此次事件由 刪除 行觸發:");  
  226.                     System.out.println("此次刪除的是第 " + firstRow + " 行");  
  227.                     break;  
  228.                 case TableModelEvent.INSERT:  
  229.                     System.out.print("此次事件由 插入 行觸發:");  
  230.                     System.out.println("此次插入的是第 " + firstRow + " 行");  
  231.                     break;  
  232.                 case TableModelEvent.UPDATE:  
  233.                     System.out.print("此次事件由 更新 行觸發:");  
  234.                     System.out.println("此次更新的是第 " + firstRow + " 行第 " + column + " 列");  
  235.                     break;  
  236.                 default:  
  237.                     System.out.println("此次事件由 其他原因 觸發");  
  238.                 }  
  239.             }  
  240.   
  241.         });  
  242.     }  
  243.   
  244.     /** 
  245.      * @param args 
  246.      */  
  247.     public static void main(String[] args) {  
  248.         // TODO Auto-generated method stub   
  249.         TableModelEvent_1 frame = new TableModelEvent_1();  
  250.         frame.setTitle("表格模型事件示例");  
  251.         frame.pack(); //Realize the components.   
  252. //      frame.setBounds(100, 100, 600, 300);   
  253. //      textFieldA.requestFocus();   
  254.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  255.         frame.setVisible(true); //Display the window.   
  256.     }  
  257.   
  258. }  
Copyright © Linux教程網 All Rights Reserved