Java 对齐JTable中单元格的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2408541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Align the values of the cells in JTable?
提问by Venkat
I'm not aware of how to align the values of cells in JTable.
我不知道如何在 JTable 中对齐单元格的值。
For Ex,The Jtable shows, Name Salary Mr.X 100000.50 XXXX 234.34 YYYy 1205.50
例如,Jtable 显示,姓名工资 X 先生 100000.50 XXXX 234.34 YYYy 1205.50
I want to align the "Salaries" in the following format.
我想按照以下格式调整“工资”。
Name Salary
Mr.X 100000.50
XXXX 234.34
YYYy 1205.50
How to align as above the JTable
如何在 JTable 上面对齐
采纳答案by camickr
There is no need to create a custom class for this, just use the default renderer:
无需为此创建自定义类,只需使用默认渲染器:
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumnModel().getColumn(???).setCellRenderer(rightRenderer);
Or a better approach is to actually store Double values in the table and then a proper numeric renderer will be used and number renderers are automatically right aligned. You can then customize the formatting of the number using a Table Format Renderer.
或者更好的方法是将 Double 值实际存储在表中,然后将使用适当的数字渲染器,数字渲染器会自动右对齐。然后,您可以使用Table Format Renderer自定义数字的格式。
回答by Itay Maman
The way to go about it is to specify a custom cell rendererfor each column. Each renderer will format that data differently (names will e aligned to the left, decimals to the right, ...)
解决方法是为每列指定一个自定义单元格渲染器。每个渲染器都会以不同的方式格式化该数据(名称将向左对齐,小数在右对齐,......)
回答by Aaron Digulla
From this forum post:
从这个论坛帖子:
Create a class that extends DefaultTableCellRenderer
and implement the getTableCellRendererComponent()
method, something like:
创建一个扩展DefaultTableCellRenderer
并实现该getTableCellRendererComponent()
方法的类,例如:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel renderedLabel = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
renderedLabel.setHorizontalAlignment(SwingConstant s.RIGHT);
return renderedLabel;
}
and install this renderer for the column in question.
并为相关列安装此渲染器。
Now you only need to make sure that each value has the same number of decimal places because for most fonts, all digits have the same width.
现在您只需要确保每个值的小数位数相同,因为对于大多数字体,所有数字都具有相同的宽度。
回答by fabian
I have got a method that aligns a column in a table to the right:
我有一种方法可以将表中的列向右对齐:
private void alignRight(JTable table, int column) {
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
table.getColumnModel().getColumn(column).setCellRenderer(rightRenderer);
}
回答by Adrian
We need to make a small correction, the right way is DefaultTableCellRenderer.RIGHT
我们需要做一个小的修正,正确的方法是DefaultTableCellRenderer.RIGHT
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
tableExample.getColumn("Price").setCellRenderer( rightRenderer );
Notice the difference with the original response of camickr, "Price" is the name of the column, change according to the case.
请注意与camickr的原始响应不同,“价格”是列的名称,根据情况更改。
回答by Lê Quang Duy
A simple way is using DefaultTableModel and override method getColumnClass()
Ex:
一个简单的方法是使用 DefaultTableModel 和覆盖方法getColumnClass()
例如:
DefaultTableModel model = new DefaultTableModel() {
@Override
public Class getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return Integer.class;
} else {
return String.class;
}
}
};
If you return Integer, your column will align right anh if return String your column will align left.
如果您返回 Integer,则您的列将向右对齐,如果返回 String,您的列将向左对齐。
回答by OmAr
as for more than one table at once i managed to do this ... its as @camickr posted but i added the headers too
至于一次不止一张桌子,我设法做到了……正如@camickr 发布的那样,但我也添加了标题
DefaultTableCellRenderer rightRenderer_c = new DefaultTableCellRenderer();
DefaultTableCellRenderer rightRenderer_h = new DefaultTableCellRenderer();
rightRenderer_c.setHorizontalAlignment( javax.swing.JLabel.RIGHT );
for(JTable t : Tables){ //Tables is an ArrayList<JTable>
//for the headers :
rightRenderer_h = (DefaultTableCellRenderer) t.getTableHeader().getDefaultRenderer();
rightRenderer_h.setHorizontalAlignment( javax.swing.JLabel.RIGHT );
//for cells :
for(int i=0; i < t.getColumnCount(); i++){
t.getColumnModel().getColumn(i).setCellRenderer(rightRenderer_c);
}
}