java table集合,为Java集合实现AbstractTableModel

I'm trying to implement an AbstractTableModel for a collection named "clients" but I keep receiving the error "required variable found value" for the "add" method.

Here is my code:

I'm sorry for the confusion created. The add method is meant to add a new client in the table (by that I mean a new row). I don't want to add a new client to the collection.

class ModelTabel extends AbstractTableModel{

public int getRowCount() {

return clients.size();

}

public int getColumnCount() {

return 4;

}

public Object getValueAt(int row, int column) {

Client c = clients.get(row-1);

switch(column){

case 0: return c.getName();

case 1: return c.getSurname();

case 2: return c.getID();

case 3: return c.getPhone();

default:return "ERROR";

}

}

public void add(Client c) {

clients.get(clients.size()++) = a;

fireTableDataChanged();

}

}

解决方案

I believe this is the same problem as this question... your variable assignment is reversed.

It should be (although this code is still incorrect - see below):

a = clients.get(clients.size()++);

EDIT: this was already answered by Prabhakaran, but apparently people felt the need to downvote my answer.. I think I did address the original question, but I appreciate that my code sample was still incorrect, so I will try to provide a more complete answer:

First of all, as to the "required variable not found" error, if you google it you will see that other SO question as the first hit. clients.get(clients.size()++) is not a variable, so you can't assign things to it. I am not sure where a is declared in your code, but assume that it is a variable and thus my suggestion of reversing the assignment.

Next, for the clients.get(clients.size()++) line, as others have mentioned or alluded to - VAR++ is equivalent to VAR = VAR + 1, so again is an assignment operation going on. In this case, clients.size() is not a variable, so you can not increment it. If you wanted the clients.size() + 1 index, you could have written: a = clients.get(clients.size() + 1)... but that will throw an ArrayIndexOutOfBoundsException, because you're trying to access an element of clients beyond its current size!

That is why Prabhakaran rewrote your method as they did, changing that line to a clients.add(c) call - as it seemed to fit the original intent of the method.

使用 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'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're almost done, but not quite. What's the use of putting a JButton in a JTable if you can'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、付费专栏及课程。

余额充值