Java JTable: any default cell alignment technique?

喜夏-厌秋 提交于 2019-12-12 02:36:11

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/4631985/java-jtable-any-default-cell-alignment-technique

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!