先看效果:
设置table某列渲染
table.getColumnModel().getColumn(4).setCellEditor(new MyRender());//设置编辑器
table.getColumnModel().getColumn(4).setCellRenderer(new MyRender());
jtable渲染类
/**
Table显示界面渲染类,重点为了实现鼠标移动到删除按钮上鼠标指针变成手型
*/
class MyRender extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, HyperlinkListener{
private static final long serialVersionUID = 1L;
private JEditorPane deleterPane =null;
private JEditorPane updatePane =null;
private JPanel panel;
public MyRender(){
panel=new JPanel();
panel.setLayout(new FlowLayout());
deleterPane = new JEditorPane();
deleterPane.setContentType("text/html");
deleterPane.setText(" <a href=''>删除</a>");
deleterPane.setName("delete");
deleterPane.setEditable(false);
deleterPane.setBackground(panel.getBackground());
deleterPane.addHyperlinkListener(this);
updatePane = new JEditorPane();
updatePane.setContentType("text/html");
updatePane.setText(" <a href=''>修改</a>");
updatePane.setName("update");
updatePane.setEditable(false);
updatePane.setBackground(panel.getBackground());
updatePane.addHyperlinkListener(this);
panel.add(deleterPane);
panel.add(updatePane);
}
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return panel;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
return panel;
}
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
//点击删除按钮触发事件
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
String selectDirectoryNum=(String) model.getValueAt(selectRow, 0);
switch (((JEditorPane)e.getSource()).getName()) {
case "delete":
int confirmResult=JOptionPane.showConfirmDialog(null, "确定删除 :"+selectDirectoryNum+"?","路径删除",JOptionPane.YES_NO_OPTION);
//删除操作
if(confirmResult==0){
//列表中移除
model.removeRow(selectRow);
//数据库中移除
DirectoryManageBusiness directoryManageBusiness=new DirectoryManageBusiness();
Map<String, Object>params=new HashMap<>();
params.put("extension_number", selectDirectoryNum);
List<DirectoryEntity> directoryEntitys = null;
try {
directoryEntitys = directoryManageBusiness.selectDirectorys(params);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
DirectoryEntity directory=directoryEntitys!=null?directoryEntitys.get(0):null;
try {
directoryManageBusiness.deleteDirectory(directory);
total--;
pageTotal=total%PAGE_COUNT==0?total/PAGE_COUNT:total/PAGE_COUNT+1;
countField.setText(total+"");
countPage.setText(pageTotal+"");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
break;
case "update":
List<String> params=new ArrayList<>();
params.add((String) model.getValueAt(selectRow, 0));
params.add((String) model.getValueAt(selectRow, 1));
params.add((String) model.getValueAt(selectRow, 2));
params.add((String) model.getValueAt(selectRow, 3));
add_updateDirectory(params);
break;
default:
break;
}
}
}
}