开发者

Java JTable: any default cell alignment technique?

I have a JTable with 9 columns, and I know I can create custom cell renderers for each column, at the expense of about 8 lines of code per renderer.

But all I really need is to display text placed in all cells as right-justified. Is there any way to set this alignment for the whole table?

Thanks in advance 开发者_开发问答for any suggestions.

John Doner


Try this:

for (int i = 0; i < myTable.getModel().getRowCount(); i++) {
   for (int j = 0; j < myTable.getModel().getColumnCount(); j++) {
      DefaultTableCellRenderer renderer =
         (DefaultTableCellRenderer)myTable.getCellRenderer(i, j);
      renderer.setHorizontalAlignment(JTextField.RIGHT);
   } // End for(j)
} // End for(i)

Since each cell already has a renderer, this grabs each cell's existing TableCellRenderer from the TableModel and uses the built-in method setHorizontalAlignment(int) inherited from JLabel.

Hope that's what you're looking for!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜