JTable 的模式我重载的AbstractTableModel
如何添加最后一列为JButton.
让我点Button的时候知道所选中的行.
谢谢了。
------解决方案--------------------
没印象了, 只记得原来加过 JCheckBox render 和edit 是两个不同的东西,记住这点应该差不多了
------解决方案--------------------
http://topic.csdn.net/t/20050804/10/4187846.html
http://topic.csdn.net/t/20020515/10/723443.html
http://www.devtopic.com/Java/J2SE/20070727/70407.html
如果你搜过的话,就当跳过这个
------解决方案--------------------
你要继承下 AbstractCellEditor ;
public class ButtonCellEdit extends AbstractCellEditor implements
TableCellRenderer, TableCellEditor, ActionListener {
}
会有以下两个方法要你重写的,你加两个button进去, 一个做为 render,一个做为 button试试看
getTableCellRendererComponent(
getTableCellEditorComponent
------解决方案--------------------
不知道Button列在展现的时候是正常的,还是就是一个按钮。
看看我的博客,里面就有这样的例子,使用Table的CellRenderer和CellEditor实现。
http://topic.csdn.net/u/20081222/22/258e0297-9936-43a0-9f40-44170c95b397.html?878802289下面也是我的一个回复。
PS:Renderer和Editor最好还是分开
------解决方案--------------------
晕死,前几天给你回复的没有解决啊?!
- Java code
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
public class JButtonTableExample extends JFrame {
public JButtonTableExample() {
super("JButtonTable Example");
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(new Object[][] { { "button 1", "foo" },
{ "button 2", "bar" } }, new Object[] { "Button", "String" });
JTable table = new JTable(dm);
table.setOpaque(true);
table.setBackground(Color.red);
table.getColumn("Button").setCellRenderer(new ButtonRenderer());
table.getColumn("Button").setCellEditor(
new ButtonEditor(new JCheckBox()));
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
setSize(400, 100);
setVisible(true);
}
public static void main(String[] args) {
JButtonTableExample frame = new JButtonTableExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText((value == null) ? "" : value.toString());