import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import NETSOURCE.SearchPanel;
import NETSOURCE.SearchPanel.ButtonEditor;
import NETSOURCE.SearchPanel.ButtonRenderer;
public class ManageStoredData extends JPanel{
private DefaultTableModel tableModel;
private JTable table;
private final int columnWithoutButton = 5;
public ManageStoredData() {
setLayout(null);
this.setBackground(Color.pink);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setAutoscrolls(true);
tableModel = new DefaultTableModel() {
// 设置列能否编辑
public boolean isCellEditable(int row, int column) {
if (column == 4 || column == 5 )
return true;
return false;
}
};
String headName[] = { "序号", "文件名", "文件大小", "修改日期",
"导出", "删除"};
tableModel.setColumnIdentifiers(headName);
table = new JTable(tableModel);
table.getColumn("导出").setCellRenderer(new ButtonRenderer());
table.getColumn("导出").setCellEditor(new ButtonEditor(new JCheckBox()));
table.getColumn("删除").setCellRenderer(new ButtonRenderer());
table.getColumn("删除").setCellEditor(new ButtonEditor(new JCheckBox()));
// 设置列标题不能移动
// table.getTableHeader().setReorderingAllowed(false);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane.setViewportView(table);
// 设置列宽,并且当调整Panel时列宽也能相应的变化
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
// table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //若设置它,列宽永远固定
table.getColumnModel().getColumn(0).setPreferredWidth(30);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(80);
table.getColumnModel().getColumn(3).setPreferredWidth(80);
table.getColumnModel().getColumn(4).setPreferredWidth(70);
table.getColumnModel().getColumn(5).setPreferredWidth(70);
// 设置行高
table.setRowHeight(25);
scrollPane.setBounds(80, 20, 500, 125);
this.add(scrollPane);
setVisible(true);
InitValue();
}
private void InitValue() {
for(int i=0;i<5;i++){
Vector vector=new Vector();
vector.addElement("1");
vector.addElement("Open Close Msg");
vector.addElement("do");
vector.addElement("2");
vector.addElement("导出");
vector.addElement("删除");
tableModel.getDataVector().add(vector);
}
}
public 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());
return this;
}
}
public class ButtonEditor extends DefaultCellEditor {
protected JButton button;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
ButtonClick();
}
});
}
@Override
public Component getTableCellEditorComponent(JTable table1,
Object value, boolean isSelected, int row, int column) {
if (isSelected) {
System.out.println("change column");
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
System.out.println("change row");
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
button.setText(table1.getValueAt(row, column).toString());
return button;
}
//若缺少此方法,点击按钮后将获取不到Button的Text值,将会显示false.
@Override
public Object getCellEditorValue(){
return button.getText();
}
protected void ButtonClick() {
// TODO Auto-generated method stub
System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
System.out.println(table.getSelectedColumn() + " and "
+ table.getSelectedRow());
}
}
}