当前位置:编程学习 > JAVA >>

Java表格模型事件示例

\
[java]
package com.han; 
 
import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.InputEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.util.Arrays; 
import java.util.Vector; 
 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.ListSelectionModel; 
import javax.swing.event.TableModelEvent; 
import javax.swing.event.TableModelListener; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.JTableHeader; 
import javax.swing.table.TableColumnModel; 
 
/**
 * 表格模型事件示例
 * <p>
 * <li>A component generally gains the focus when the user clicks it, 
 * or when the user tabs between components, or otherwise interacts 
 * with a component. A component can also be given the focus programmatically,
 * such as when its containing frame or dialog-box is made visible. 
 * The snippet of the codes below shows how to give a particular component  
 * the focus every time the window gains the focus.
 * 
 * @author HAN
 * 
 */ 
public class TableModelEvent_1 extends JFrame { 
 
    /**
     * 
     */ 
    private static final long serialVersionUID = -8581492063632813033L; 
 
    public TableModelEvent_1() { 
        // TODO Auto-generated constructor stub  
        final Container container = getContentPane(); 
        Vector<String> tableColumnNames = new Vector<String>(); 
        tableColumnNames.add("A"); 
        tableColumnNames.add("B"); 
        Vector<Vector<String>> tableValues = new Vector<Vector<String>>(); 
        for (int i = 1; i < 5; i++) { 
            Vector<String> vector = new Vector<String>(); 
            vector.add("A" + i); 
            vector.add("B" + i); 
            tableValues.add(vector); 
        } 
        final DefaultTableModel defaultTableModel = new DefaultTableModel( 
                tableValues, tableColumnNames); 
        final JTable table = new JTable(defaultTableModel); 
        JScrollPane scrollPane = new JScrollPane(); 
        scrollPane.setViewportView(table); 
        container.add(scrollPane, BorderLayout.CENTER); 
        JPanel panel = new JPanel(); 
        container.add(panel, BorderLayout.SOUTH); 
        JLabel labelA = new JLabel("A: "); 
        final JTextField textFieldA = new JTextField(15); 
        JLabel labelB = new JLabel("B: "); 
        final JTextField textFieldB = new JTextField(15); 
        JButton buttonAdd = new JButton("添加"); 
        JButton buttonDel = new JButton("删除"); 
        JButton buttonDeselected = new JButton("取消选择"); 
        panel.add(labelA); 
        panel.add(textFieldA); 
        panel.add(labelB); 
        panel.add(textFieldB); 
        panel.add(buttonAdd); 
        panel.add(buttonDel); 
        panel.add(buttonDeselected); 
        buttonAdd.addActionListener(new ActionListener() { 
 
            @Override 
            public void actionPerformed(ActionEvent e) { 
                // TODO Auto-generated method stub  
                int[] selectedRows = table.getSelectedRows(); // table  
                                                                // 默认情况容许多行选择  
                Vector<String> rowData = new Vector<String>(); 
                rowData.add(textFieldA.getText()); 
                rowData.add(textFieldB.getText()); 
                if (selectedRows.length == 0) { 
                    defaultTableModel.addRow(rowData); 
                    textFieldA.setText(null); 
                    textFieldB.setText(null); 
                } else if (selectedRows.length == 1) { 
//   
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,