JTable 一个最好的例子

装载自:http://www.oschina.net/code/snippet_54100_1230

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;/**
* JTable的实用小例子
*
* @author 五斗米 <如转载请保留作者和出处>
* @blog http://blog.csdn.net/mq612
*/public class Test {private JFrame frame = null;private JTable table = null;private Table_Model model = null;private JScrollPane s_pan = null;private JButton button_1 = null, button_2 = null, button_3 = null;private JPanel pane = null;public Test() {frame = new JFrame("JTableTest");pane = new JPanel();button_1 = new JButton("清除数据");button_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {removeData();}});button_2 = new JButton("添加数据");button_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {addData();}});button_3 = new JButton("保存数据");button_3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {saveData();}});pane.add(button_1);pane.add(button_2);pane.add(button_3);model = new Table_Model(20);table = new JTable(model);table.setBackground(Color.white);String[] age = { "16", "17", "18", "19", "20", "21", "22" };JComboBox com = new JComboBox(age);TableColumnModel tcm = table.getColumnModel();tcm.getColumn(3).setCellEditor(new DefaultCellEditor(com));tcm.getColumn(0).setPreferredWidth(50);tcm.getColumn(1).setPreferredWidth(100);tcm.getColumn(2).setPreferredWidth(50);s_pan = new JScrollPane(table);frame.getContentPane().add(s_pan, BorderLayout.CENTER);frame.getContentPane().add(pane, BorderLayout.NORTH);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 200);frame.setVisible(true);}private void addData() {model.addRow("李逵", true, "19");table.updateUI();}private void removeData() {model.removeRows(0, model.getRowCount());table.updateUI();}// 保存数据,暂时是将数据从控制台显示出来private void saveData() {int col = model.getColumnCount();int row = model.getRowCount();for (int i = 0; i < col; i++) {System.out.print(model.getColumnName(i) + "\t");}System.out.print("\r\n");for (int i = 0; i < row; i++) {for (int j = 0; j < col; j++) {System.out.print(model.getValueAt(i, j) + "\t");}System.out.print("\r\n");}System.out.println("------------------------------------");}public static void main(String args[]) {new Test();System.out.println(
"按下保存按钮将会把JTable中的内容显示出来\r\n------------------------------------");}}/**
* TableModel类,继承了AbstractTableModel
*
* @author 五斗米
*
*/
class Table_Model extends AbstractTableModel {private static final long serialVersionUID = -7495940408592595397L;private Vector content = null;private String[] title_name = { "ID", "姓名", "性别", "年龄" };public Table_Model() {content = new Vector();}public Table_Model(int count) {content = new Vector(count);}public void addRow(String name, boolean sex, String age) {Vector v = new Vector(4);v.add(0, new Integer(content.size()));v.add(1, name);v.add(2, new Boolean(sex));v.add(3, age);content.add(v);}public void removeRow(int row) {content.remove(row);}public void removeRows(int row, int count) {for (int i = 0; i < count; i++) {if (content.size() > row) {content.remove(row);}}}/*** 让表格中某些值可修改,但需要setValueAt(Object value, int row, int col)方法配合才能使修改生效*/public boolean isCellEditable(int rowIndex, int columnIndex) {if (columnIndex == 0) {return false;}return true;}/*** 使修改的内容生效*/public void setValueAt(Object value, int row, int col) {((Vector) content.get(row)).remove(col);((Vector) content.get(row)).add(col, value);this.fireTableCellUpdated(row, col);}public String getColumnName(int col) {return title_name[col];}public int getColumnCount() {return title_name.length;}public int getRowCount() {return content.size();}public Object getValueAt(int row, int col) {return ((Vector) content.get(row)).get(col);}/*** 返回数据类型*/public Class getColumnClass(int col) {return getValueAt(0, col).getClass();}
}
View Code

 

转载于:https://www.cnblogs.com/Hungry3/p/3448519.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/408262.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

你的QQ为谁隐身?

习惯性的打开了qq&#xff0c; 习惯性的点击了隐身选项&#xff0c; 习惯性的戴上了耳机&#xff0c; 习惯性的看看那些闪动的头像&#xff1b;        默默的&#xff0c;默默的&#xff0c;只是等待&#xff1b; 一个月&#xff0c;两个月&#xff0c;三个月…     …

一些感动的日子

http://spaces.msn.com/members/coolbug/Blog/cns!1paSXV471KCYBnh-ynIQiUkg!543.entry

bravo

大白&#xff1a; p70 放置街灯 一开始看大白上的思路硬是 没看懂 后来看代码时有几个疑点 再结合 前面的分析 豁然开朗了 beautiful mind 实在是太聪明了&#xff01;&#xff01;&#xff08;鸡冻中。。。。&#xff09; 这里的一个技巧是 要是有两个 要优化的量 v1和v2 要求…

整理javascript操作文件说明.

Javascript是网页制作中离不开的脚本语言&#xff0c;依靠它&#xff0c;一个网页的内容才生动活泼、富有朝气。但也许你还没有发现并应用它的一些更高级的功能吧&#xff1f;比如&#xff0c;对文件和文件夹进行读、写和删除&#xff0c;就象在VB、VC等高级语言中经常做的工作…

堆排序算法的实现 (冒泡排序)

方法一&#xff1a; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ConsoleApplication3 {class Program{static void Main(string[] args){List<int> seqlist new List<int> …

角色转变

11月4日花了整整一天的时间和航发锦绣家园签订了不平等“条约”时&#xff0c;开始我的角色开始准备转化为负债者&#xff0c;在之后的10年间将每月偿还给工商银行&#xffe5;849。买房&#xff0c;你说还能是什么呢&#xff1f;这个我这个年龄段的头等大事。婚姻&#xff1f;…

a href='?out=login'是什么意思

<a href?outlogin>退出</a>前面加上问号&#xff1f;就是GET方式传递outlogin是要传递的数据点这个链接就可以执行接下来通过$_GET["out"]接收这个时候$_GET["out"]的值就是login<a href?outlogin>退出</a>就是向本页传递数据要…

常用js函数

/**//*------------------------------------------------------------ dateDlg(inputid,initDate,startYear,endYear) 弹出时间窗口 compareDate(end,s) 与当前时间比较 compareTwoDate(startDate,endDate,s) 比较两个时间 isNumber(text…

读取xml文件转成ListT对象的两种方法(附源码)

读取xml文件转成List<T>对象的两种方法&#xff08;附源码&#xff09;读取xml文件&#xff0c;是项目中经常要用到的&#xff0c;所以就总结一下&#xff0c;最近项目中用到的读取xml文件并且转成List<T>对象的方法&#xff0c;加上自己知道的另一种实现方法。 就…

委托的运用

下面这篇代码展示了委托的运用,非常精彩,代码版权归"microsoft"所有. 1//版权所有 (C) 2000 Microsoft Corporation。保留所有权利。23//bookstore.cs4usingSystem;56//处理书店的一组类:7namespaceBookstore 8{ 9 using System.Collections; 10 11 // 描述图书…

visual studio enter键代码自动补全

将resharper插件中的设置内容改成这个就可以enter键自动补全了

使用air进行移动app开发常见功能和问题(二)

1、 Air如何判断android、ios 平台网络连接状态&#xff1f; Android&#xff0c;使用as3原生api&#xff1a; [javascript] view plaincopy if(NetworkInfo.isSupported)//只有android支持 NetworkInfo.networkInfo.addEventListener(Event.NETWORK_CHANG…

虚拟内存相关原理

内存管理1.虚拟内存2.内存分段3.内存分页多级页表TLB1.虚拟内存 众所周知:单片机是没有操作系统的,所以单片机的CPU是直接操作内存的物理地址(physical address,PA) 在这种情况下,程序内存中是无法同时运行多个程序。例如第一个程序在10位置的位置存放立一个数值&#xff0c;…