我想创建一个 JTable,它可以在按下按钮时更改外部突出显示单元格的边框颜色。
我已经找到了如何制作按钮并获取必须更改的单元格的信息,如下所示:
frame.add(new JButton(new AbstractAction("Create Border "){
private static final long serialVersionUID = 1L;
private void createBorder(){
System.out.println(table.getSelectedColumn());
System.out.println(table.getSelectedRow());
System.out.println(table.getSelectedRowCount());
System.out.println(table.getSelectedColumnCount());
System.out.println(table.getSelectedRows()[0]);
System.out.println(table.getSelectedColumns());
}
@Override
public void actionPerformed(ActionEvent e){
createBorder();
}
}), BorderLayout.SOUTH);
frame.pack();
frame.setLocation(150,150);
frame.setVisible(true);
}
现在剩下的就是以某种方式获取各个单元格并更改它们的边框。
到目前为止,我只找到了通过渲染器创建表格时更改边框的解决方案。
最佳答案
i only found solutions for changing the border at the creation of the table through the renderer.
另一种方法可能是重写 JTable
的 prepareRenderer(...)
方法,因此您不需要为每种类型创建自定义渲染器数据。
查看Table Row Rendering有关此方法的一些示例。一个示例展示了如何在整行而不是每个单元格周围放置边框。
关于java Swing JTable 更改单元格边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42419547/