如何在 Java 中编辑 DefualtTableCellRenderer?

发布于 2025-01-05 04:27:31 字数 381 浏览 1 评论 0原文

我在编辑 DefaultTableCellRenderer 时遇到问题。

我想右对齐表格中的第二列。然而,下面的代码右对齐所有列,而不仅仅是第二列!

谁能看到我哪里出错了?

for (int i=0; i < tblMenu.getModel().getColumnCount(); i++) 
{ 
    DefaultTableCellRenderer renderer = 
        (DefaultTableCellRenderer) tblMenu.getCellRenderer(i, 1);

    renderer.setHorizontalAlignment(JTextField.RIGHT); 
}

I am having a problem with editing the DefaultTableCellRenderer.

I would like to right align just the 2nd column in my table. However, the code below right aligns all the columns, and not just the 2nd one!

Can anyone see where I am going wrong?

for (int i=0; i < tblMenu.getModel().getColumnCount(); i++) 
{ 
    DefaultTableCellRenderer renderer = 
        (DefaultTableCellRenderer) tblMenu.getCellRenderer(i, 1);

    renderer.setHorizontalAlignment(JTextField.RIGHT); 
}

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

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

发布评论

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

评论(3

陪你搞怪i 2025-01-12 04:27:31

您可以使用匿名内部类覆盖表上的默认渲染器。类似以下(未经测试)的代码可能会起作用:

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (column == 2) {
                setHorizontalAlignment(JTextField.RIGHT);
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }

    });

You can override the default renderer on your table with an anonymous inner class. Something like the following (untested) code might work:

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (column == 2) {
                setHorizontalAlignment(JTextField.RIGHT);
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }

    });
网名女生简单气质 2025-01-12 04:27:31

默认情况下,所有列都使用相同的渲染器,您需要为要更改的列提供不同配置的渲染器:

DefaultTableCellRenderer righty = new DefaultTableCellRenderer();
righty.setHorizontalAlignment(JTextField.RIGHT);

tblMenu.getColumnModel().getColumn(1).setCellRenderer(righty);

也许比 A Lee 的方式简单一点(取决于您正在执行多少其他自定义操作)。

All columns use the same renderer by default, you need to provide a differently configured one for the column you want to change:

DefaultTableCellRenderer righty = new DefaultTableCellRenderer();
righty.setHorizontalAlignment(JTextField.RIGHT);

tblMenu.getColumnModel().getColumn(1).setCellRenderer(righty);

Maybe a little simpler than A Lee's way (depending on how much other customization you're doing).

笑叹一世浮沉 2025-01-12 04:27:31

您可以为特定列设置单元格渲染器。您所要做的就是获取表的columnModel,然后获取特定列,然后为该列调用setCellRenderer。

但更简单的是,如果您的列包含数字数据,只需覆盖表模型的 getColumnClass(...) 即可返回该列的 Integer.class,并且表将为您正确设置格式。或者,如果您知道您的表不会为空,只需让表的数据告诉世界它是什么类型,并让 JTable 以最佳方式呈现内容:

// if the table isn't empty, this will work.
class MyDTableModel extends DefaultTableModel {
  public Class<? extends Object> getColumnClass(int c) {
     return getValueAt(0, c).getClass();
  }
}

You can set the cell renderer for a specific column. All you have to do is get the table's columnModel, then get the specific column, then call setCellRenderer for that column.

But even easier, if your column holds numeric data, simply override the table model's getColumnClass(...) to return Integer.class for that column and the table will format correctly for you. Or if you know your table will not be empty, simply let the table's data tell the world what type of type it is and let the JTable render things as best it should:

// if the table isn't empty, this will work.
class MyDTableModel extends DefaultTableModel {
  public Class<? extends Object> getColumnClass(int c) {
     return getValueAt(0, c).getClass();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文