对齐 JTable 中单元格的值?

发布于 2024-08-24 19:19:50 字数 290 浏览 5 评论 0原文

我不知道如何对齐 JTable 中单元格的值。

例如,Jtable 显示, 姓名 薪资 X先生 100000.50 XXXX 234.34 YYYy 1205.50

我想按以下格式对齐“工资”。

   Name      Salary
   Mr.X      100000.50
   XXXX         234.34
   YYYy        1205.50

如何像上面的 JTable 一样对齐

I'm not aware of how to align the values of cells in JTable.

For Ex,The Jtable shows,
Name Salary
Mr.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

只是偏爱你 2024-08-31 19:19:50

无需为此创建自定义类,只需使用默认渲染器:

DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumnModel().getColumn(???).setCellRenderer(rightRenderer);

或者更好的方法是在表中实际存储 Double 值,然后使用适当的数字渲染器,并且数字渲染器自动右对齐。然后,您可以使用表格格式呈现器。

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.

a√萤火虫的光℡ 2024-08-31 19:19:50

我们需要做一个小修正,正确的方法是 DefaultTableCellRenderer.RIGHT

DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
tableExample.getColumn("Price").setCellRenderer( rightRenderer );

注意与 camickr 原始响应的区别,“Price”是列的名称,根据情况更改。

We need to make a small correction, the right way is 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.

雨的味道风的声音 2024-08-31 19:19:50

一种简单的方法是使用 DefaultTableModel 并覆盖方法 getColumnClass()
例如:

DefaultTableModel model = new DefaultTableModel() {
        @Override
        public Class getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Integer.class;
            } else {
                return String.class;
            }
        }
    };

如果返回 Integer,您的列将右对齐;如果返回 String,您的列将左对齐。

A simple way is using DefaultTableModel and override method getColumnClass()
Ex:

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.

樱&纷飞 2024-08-31 19:19:50

来自此论坛帖子

创建一个扩展的类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;
}

并为相关列安装此渲染器。

现在您只需确保每个值具有相同的小数位数,因为对于大多数字体,所有数字都具有相同的宽度。

From this forum post:

Create a class that extends DefaultTableCellRenderer and implement the getTableCellRendererComponent() method, something like:

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.

白衬杉格子梦 2024-08-31 19:19:50

我有一种将表中的列向右对齐的方法:

private void alignRight(JTable table, int column) {
    DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
    rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
    table.getColumnModel().getColumn(column).setCellRenderer(rightRenderer);
}

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);
}
从﹋此江山别 2024-08-31 19:19:50

解决方法是指定一个自定义 单元格每列的渲染器。每个渲染器将以不同的方式格式化该数据(名称将向左对齐,小数点向右对齐,...)

The way to go about it is to specify a custom cell renderer for each column. Each renderer will format that data differently (names will e aligned to the left, decimals to the right, ...)

怕倦 2024-08-31 19:19:50

至于同时不止一张桌子,我设法做到了这一点...它是@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);
        }
    }

as for more than one table at once i managed to do this ... its as @camickr posted but i added the headers too

    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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文