java Swing JTable 改变单元格边框
java Swing JTable change cell Border
我想创建一个 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);
}
现在剩下的就是以某种方式获取单个单元格并更改它们的边框。
到目前为止,我只找到了通过渲染器在 table 创建时更改边框的解决方案。
i only found solutions for changing the border at the creation of the table through the renderer.
另一种方法可能是重写 JTable
的 prepareRenderer(...)
方法,因此您不需要为每种类型的数据创建自定义渲染器。
查看 Table Row Rendering 以了解此方法的一些示例。一个示例展示了如何在整行而不是每个单元格周围放置边框。
我想创建一个 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);
}
现在剩下的就是以某种方式获取单个单元格并更改它们的边框。
到目前为止,我只找到了通过渲染器在 table 创建时更改边框的解决方案。
i only found solutions for changing the border at the creation of the table through the renderer.
另一种方法可能是重写 JTable
的 prepareRenderer(...)
方法,因此您不需要为每种类型的数据创建自定义渲染器。
查看 Table Row Rendering 以了解此方法的一些示例。一个示例展示了如何在整行而不是每个单元格周围放置边框。