如何格式化JTable的内容

huangapple go评论81阅读模式
英文:

How to format contents of a JTable

问题

我正在使用JTable来显示数值数据和字符串。数值数据默认格式显示在JTable的右侧,而字符串格式显示在左侧。我希望两者都能格式化为单元格的中心。我正在使用NetBeans开发GUI,但似乎对于这个问题没有帮助。

我的尝试是创建一个单元格渲染器类,覆盖JTable默认的单元格渲染器,但我不知道实际上要在新的单元格渲染器中更改格式的代码行。

任何帮助将不胜感激。

英文:

I am using a JTable to display numerical data and strings. The numerical data default formats to the right hand side of the JTable, and the strings format to the left. I want both to be formatted into the center of the cell. I am using Nedbeans to develop the GUI but it does not seem to help with this issue.

My attempt was to create a cell renderer class that overrides the JTable default cell renderer, but I
don't know the line of code to actually change the formatting in the new cell renderer.

Any help would be appreciated.

答案1

得分: 1

你的思路是正确的。在自定义的TableCellRenderer内部,实际上可以检查渲染的是哪一列/行/单元格,然后相应地分配特定于列/行/单元格的格式。

public static class CustomTableCellRenderer extends DefaultTableCellRenderer {    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        DefaultTableCellRenderer c = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        // 将第一列中的内容居中
        if (column == 0) {
            c.setHorizontalAlignment(JLabel.CENTER);
        }

        // 第一个单元格的背景和边框应为灰色
        if (column == 0 && row == 0) {
            c.setBackground(Color.GRAY);
            c.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 5, Color.GRAY));
        }

        return c;
    }
}

请注意,DefaultTableCellRenderer 会针对每个单独的单元格进行调用。

所有可用的格式化函数在相应的文档中有详细描述:
https://docs.oracle.com/javase/10/docs/api/javax/swing/table/DefaultTableCellRenderer.html

英文:

Your thinking is correct. Within the custom TableCellRenderer, you can actually check which column/row/cell is rendered and subsequently assign a column/row/cell specific formatting.

 public static class CustomTableCellRenderer extends DefaultTableCellRenderer {    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            DefaultTableCellRenderer c = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            // center everything in the first column
            if (column == 0) {
                c.setHorizontalAlignment(JLabel.CENTER);
            }

            // the background and border of the first cell should be gray
            if (column == 0 && row == 0) {
                c.setBackground(Color.GRAY);
                c.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 5, Color.GRAY));
            }

            return c;
        }
 }

Please note that the DefaultTableCellRenderer is called for each individual cell.

All available formatting functions are well described in the respective documentation:
https://docs.oracle.com/javase/10/docs/api/javax/swing/table/DefaultTableCellRenderer.html

huangapple
  • 本文由 发表于 2020年10月23日 22:05:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64501530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定