<java><JTable>使用AbstractTableModel & JTable实现号码的分布图(初稿)

一直没有写blog的习惯,认为太浪费时间啦。总觉得自己有多多牛x,到最后,当初还模模糊糊能理解记得的,都已不能在脑海里觅得踪影。终于狠下决心,不管java也好,C语言也好,驱动也好,Linux也好。咱一锅汤端了吧。

如题,最近学习Java,算算差不多学了有两个星期了吧。也能写写基本的比如Hello World之类的程序了,,哈哈哈。不扯淡了,进入正题。

先将成果呈上,后面自己翻看也能一目了然是关于什么的。

咳咳,请忽略掉下面布局的问题吧。。。。尴尬

在这个程序中我for循环三次用来生成数据(FirstName, LastName, Color, Sex, Year),,然后将它们显示到窗口的某一个Table中。边学边写,知道了JTable,知道了TableModel,知道了AbstractTableModel。但是使用起来却是不得要领,其实想起来自己也有点不会走就想跑的架势,管他呢,边学边写不是吗,写成了,自然就会了,会多了自然就懂了。在写这个Table的过程中,Table跟Model都是比较简单的,插入数据的时候遇到一点困难,翻看API手册,百度,谷歌。能用的方法都用上了。就是想不通(谁让人家是菜鸟呢。)。最终不断的尝试、发现,总算知道了如何使用。

简单的说:AbstractTableModel需要实现三个方法:

	public int getRowCount();
	public int getColumnCount();
	public Object getValueAt(int row, int column);

这三个方法的意思就不解释了。常规演示代码里面使用时在model,都是在model里面就初始化好数据,然后建立Table的时候直接添加进去了。这当然不是我想要的,查看Oracle的demo代码:SwingSet3后发现,getValueAt这个方法便是用来插入数据用的,个人理解很简单:插入数据后,通知侦听器,然后getValueAt,最终显示到表中(如有误请指正,谢谢!)。

于是从头至尾的步骤为:

1、创建Model类:

class MyTableModel extends AbstractTableModel{
        private String[] columnNames_2 = {
            "First Name",
            "Last Name",
            "Color",
            "Sex",
            "# of years"
        };
private ArrayList<DataInfo> dataInfo_2 = new ArrayList<DataInfo>();  // 插入数据集
2、创建表:
 	MyTableModel model = new MyTableModel();
        sorter = new TableRowSorter<MyTableModel>(model);
        table = new JTable(model);
        table.setRowSorter(sorter);

3、循环三次生成数据,并插入数据:
	for(int i=0; i<3; i++) {
            DataInfo data = new DataInfo();
            data.SetFirstName("Jinze"+i);
            data.SetLastName("Zhang"+i);
            data.SetColor(Color.yellow);
            data.SetSex('f');
            data.SetYear(12+i);
            model.setValueAt(data);  // 自写方法,实现内容见下面代码
        }
In MyTableModel:

public void setValueAt(DataInfo data) {
            dataInfo_2.add(data);
        }
// 看到没,这里就是我们想要的GetValueAt方法:
@Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            DataInfo dataInfo = dataInfo_2.get(rowIndex);
            if(columnIndex == 0) {
                return dataInfo.GetFirstName();
            } else if(columnIndex == 1) {
                return dataInfo.GetLastName();
            } else if(columnIndex == 2) {
                return dataInfo.GetColor();
            } else if(columnIndex == 3) {
                return dataInfo.GetSex();
            } else if(columnIndex == 4) {
                return dataInfo.GetYear();
            }
            return null;
        }
大概步骤如上,最终看起来是挺简单的,只是初学者的过程是在太折磨人,不把它写出来虐它几百遍,心里不甘心啊。OK,本章结束。


使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you&#39;re still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you&#39;re almost done, but not quite. What&#39;s the use of putting a JButton in a JTable if you can&#39;t press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值