JTable_4 操纵表格


import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class ExampleFrame_04 extends JFrame {
	
	private JTable table;
	
	public static void main(String args[]) {
		ExampleFrame_04 frame = new ExampleFrame_04();
		frame.setVisible(true);
	}
	
	public ExampleFrame_04() {
		super();
		setTitle("操纵表格");
		setBounds(100, 100, 500, 375);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		String[] columnNames = { "A", "B", "C", "D", "E", "F", "G" };
		Vector columnNameV = new Vector();
		for (int column = 0; column < columnNames.length; column++) {
			columnNameV.add(columnNames[column]);
		}
		Vector tableValueV = new Vector();
		for (int row = 1; row < 21; row++) {
			Vector rowV = new Vector();
			for (int column = 0; column < columnNames.length; column++) {
				rowV.add(columnNames[column] + row);
			}
			tableValueV.add(rowV);
		}
		
		table = new JTable(tableValueV, columnNameV);
		table.setRowSelectionInterval(1, 3);// 设置选中行
		table.addRowSelectionInterval(5, 5);// 添加选中行
		scrollPane.setViewportView(table);
		
		JPanel buttonPanel = new JPanel();
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		
		JButton selectAllButton = new JButton("全部选择");
		selectAllButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				table.selectAll();// 选中所有行
			}
		});
		buttonPanel.add(selectAllButton);
		
		JButton clearSelectionButton = new JButton("取消选择");
		clearSelectionButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				table.clearSelection();// 取消所有选中行的选择状态
			}
		});
		buttonPanel.add(clearSelectionButton);
		//
		System.out.println("表格共有" + table.getRowCount() + "行"
				+ table.getColumnCount() + "列");
		System.out.println("共有" + table.getSelectedRowCount() + "行被选中");
		System.out.println("第3行的选择状态为:" + table.isRowSelected(2));
		System.out.println("第5行的选择状态为:" + table.isRowSelected(4));
		System.out.println("被选中的第一行的索引是:" + table.getSelectedRow());
		int[] selectedRows = table.getSelectedRows();// 获得所有被选中行的索引
		System.out.print("所有被选中行的索引是:");
		for (int row = 0; row < selectedRows.length; row++) {
			System.out.print(selectedRows[row] + "  ");
		}
		System.out.println();
		System.out.println("列移动前第2列的名称是:" + table.getColumnName(1));
		System.out.println("列移动前第2行第2列的值是:" + table.getValueAt(1, 1));
		table.moveColumn(1, 5);// 将位于索引1的列移动到索引5处
		System.out.println("列移动后第2列的名称是:" + table.getColumnName(1));
		System.out.println("列移动后第2行第2列的值是:" + table.getValueAt(1, 1));
	}
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值