我有一个严重的问题。 我正在使用Jtable,其中我使用此代码添加了一个按钮。
TableColumn buttonColumn = tableSupplier.getColumnModel().getColumn(8);
TableButton buttons = new TableButton();
buttons.addHandler(new TableButton.TableButtonPressedHandler() {
public void onButtonPress(int row, int column) {
try {
saveImageInFolder();
} catch (IOException e) {
// TODO Auto-generated catch block
Utility.getFormFieldValidator().showErrorMessage();
}
}
});
buttonColumn.setCellRenderer(buttons);
buttonColumn.setCellEditor(buttons);
我的按钮类代码是这样的。
package org.chillies.validator;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import org.chillies.view.SupplierList;
public class TableButton extends AbstractCellEditor implements TableCellEditor,TableCellRenderer
{
private static final long serialVersionUID = 5647725208335645741L;
public interface TableButtonPressedHandler
{
/**
* Called when the button is pressed.
* @param row The row in which the button is in the table.
* @param column The column the button is in in the table.
*/
void onButtonPress(int row, int column);
}
private List<TableButtonPressedHandler> handlers;
private Hashtable<Integer, JButton> buttons;
public TableButton()
{
handlers = new ArrayList<TableButtonPressedHandler>();
buttons = new Hashtable<Integer, JButton>();
}
/**
* Add a slide callback handler
* @param handler
*/
public void addHandler(TableButtonPressedHandler handler)
{
if (handlers != null)
{
handlers.add(handler);
}
}
/**
* Remove a slide callback handler
* @param handler
*/
public void removeHandler(TableButtonPressedHandler handler)
{
if (handlers != null)
{
handlers.remove(handler);
}
}
/**
* Removes the component at that row index
* @param row The row index which was just removed
*/
public void removeRow(int row)
{
if(buttons.containsKey(row))
{
buttons.remove(row);
}
}
/**
* Moves the component at oldRow index to newRow index
* @param oldRow The old row index
* @param newRow THe new row index
*/
public void moveRow(int oldRow, int newRow)
{
if(buttons.containsKey(oldRow))
{
JButton button = buttons.remove(oldRow);
buttons.put(newRow, button);
}
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focus, final int row, final int column)
{
JButton button = null;
if(buttons.containsKey(row))
{
button = buttons.get(row);
}
else
{
button = new JButton();
if(value != null && value instanceof String)
{
button.setText((String)value);
}
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(handlers != null)
{
for(TableButtonPressedHandler handler : handlers)
{
handler.onButtonPress(row, column);
}
}
}
});
button.setBorder(null);
button.setIcon(new ImageIcon(new ImageIcon(SupplierList.class
.getResource("/org/chillies/resource/Add.png")).getImage()
.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
buttons.put(row, button);
}
return button;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean selected, int row, int column)
{
JButton button = null;
if(buttons.containsKey(row))
{
button = buttons.get(row);
}
else
{
button = new JButton();
if(value != null && value instanceof String)
{
button.setText((String)value);
}
buttons.put(row, button);
}
return button;
}
public void setButtonText(int row, String text)
{
JButton button = null;
if(buttons.containsKey(row))
{
button = buttons.get(row);
button.setText(text);
}
}
public Object getCellEditorValue()
{
return null;
}
public void dispose()
{
if (handlers != null)
{
handlers.clear();
}
}
}
但是当我将我的项目导出到jar文件中时,这一行代码似乎有问题
buttonColumn.setCellRenderer(buttons);
当我删除此行并运行Jar文件时,项目运行正常,但是当我添加此行时,它甚至没有加载页面。
代码在日食中工作正常。
我正在加载我的视图类静态(只是额外的信息)。 谢谢你的时间。
答案 0 :(得分:0)
我得到了我的问题的答案,我修改了TableButton.java文件的一些代码。在做了一些快速修复后,我解决了我的问题。
答案 1 :(得分:0)
您似乎错过了 JTable 设计的要点 - 用于整个表格的单个渲染器/编辑器(我的意思是所有行):
不需要地图(哈希表)。
这同样适用于渲染器和编辑器。如果您想知道按下按钮的行,您只需调用 jTable.getSelectedRow()。
不需要自定义 TableButtonPressedHandler。