设为首页 加入收藏

TOP

Java表格模型事件示例 (一)
2014-11-24 08:41:41 】 浏览:10089
Tags: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;

/**
* 表格模型事件示例
*


*

  • 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 tableColumnNames = new Vector();
    tableColumnNames.add("A");
    tableColumnNames.add("B");
    Vector> tableva lues = new Vector>();
    for (int i = 1; i < 5; i++) {
    Vector vector = new Vector();
    vector.add("A" + i);
    vector.add("B" + i);
    tableva lues.add(vector);
    }
    final DefaultTableModel defaultTableModel = new DefaultTableModel(
    tableva lues, 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
    in
    编程开发网
  • 首页 上一页 1 2 3 4 下一页 尾页 1/4/4
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    上一篇Java自动计算表格某一数字列的和(.. 下一篇Java捕获窗口状态事件

    评论

    帐  号: 密码: (新用户注册)
    验 证 码:
    表  情:
    内  容:

    最新文章

    热门文章

    C 语言

    C++基础

    windows编程基础

    linux编程基础

    C/C++面试题目