以值为基础的带有居中值和背景的单元格

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

Cells with centered value and background based on value

问题

这是我的问题
我读取了一个数据库并通过渲染器显示数据。显示的数据必须居中,并且背景颜色必须根据单元格的值而改变。

以下是渲染器。首先,table.getColumnModel() 显示居中的值,第二个调用一个自定义渲染器来改变颜色。如果我取消第二个调用的注释,我会得到背景颜色,但不会得到居中的值,反之亦然

for (int row = 0; row < table.getColumnCount(); row++) {
    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment(JLabel.CENTER);
    table.getColumnModel().getColumn(row).setCellRenderer(centerRenderer);
    //table.getColumnModel().getColumn(row).setCellRenderer(new CustomRenderer()); 
}

这是自定义渲染器

class CustomRenderer extends DefaultTableCellRenderer
{
   public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
   {
       Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
       if(table.getValueAt(row, column).equals("A")){
           cellComponent.setBackground(Color.YELLOW);
       } else if(table.getValueAt(row, column).equals("B")){
           cellComponent.setBackground(Color.GRAY);
       } else {
           cellComponent.setBackground(Color.white);
       }
       return cellComponent;
     }
}

如何获得带有颜色和居中的单元格?
谢谢
paps

英文:

here is my problem
I read a DB and show the data via the renderer. Shown data must be centered and bgcolor must change according to the value of the cell.

Following is the renderer. First table.getColumnModel() shows the centered value, second one calls a customer renderer to change the color. If I remove the comment on the second call I get the bgcolor but not the centered value and viceversa

for (int row = 0; row < table.getColumnCount(); row++) {
    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment(JLabel.CENTER);
    table.getColumnModel().getColumn(row).setCellRenderer(centerRenderer);
    //table.getColumnModel().getColumn(row).setCellRenderer(new CustomRenderer()); 
}

This is the Custom renderer

class CustomRenderer extends DefaultTableCellRenderer
{
   public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
   {
       Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
       if(table.getValueAt(row, column).equals("A")){
           cellComponent.setBackground(Color.YELLOW);
           
       } else if(table.getValueAt(row, column).equals("B")){
           cellComponent.setBackground(Color.GRAY);
       }
       else {cellComponent.setBackground(Color.white);}
           return cellComponent;
     }
}

How can I obtain a colored and center cell?
Than you
paps

答案1

得分: 0

以下是已经翻译好的部分:

这里有一个简短的代码片段演示了我认为您想要实现的内容

public class Testing {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Testing();
            }
        });

    }

    public Testing() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        String[] colNames = { "Col1", "Col2" };
        Object[][] data = { { "A", "B" }, { "B", "C" } };

        JTable table = new JTable(data, colNames);
        CustomRenderer renderer = new CustomRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);
        // table.setDefaultRenderer(String.class, renderer); // 如果您只想着色和居中显示所有字符串
        for (int col = 0; col < table.getColumnCount(); col++) {
            // 如果您想要着色和居中显示表格的每个条目
            table.getColumnModel().getColumn(col).setCellRenderer(renderer);
        }
        panel.add(table);
        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    class CustomRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            
            cellComponent.setBackground(Color.white); // 如果不是 "A" 或 "B" 则为白色
            if (value instanceof String) {
                String temp = (String) value;
                if (temp.equals("A")) {
                    cellComponent.setBackground(Color.YELLOW);
                } else if (temp.equals("B")) {
                    cellComponent.setBackground(Color.GRAY);
                }
            }
            return cellComponent;
        }
    }
}

结果将如下所示:

以值为基础的带有居中值和背景的单元格

请注意,您的自定义渲染器只需创建一次,而不是在每次循环中创建。您的代码中的问题在于,您没有使用自己的渲染器,而是使用了 DefaultTableCellRenderer。此外,如果您只想在特定类别的对象上使用自定义渲染,例如仅在 String 对象上使用,您可以使用注释掉的行,而不是遍历表格的所有列。当然,您可以根据需要进一步限制 CustomRenderer

英文:

Here a short snippet demonstrating what I think you are trying to achieve:

public class Testing {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Testing();
}
});
}
public Testing() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String[] colNames = { &quot;Col1&quot;, &quot;Col2&quot; };
Object[][] data = { { &quot;A&quot;, &quot;B&quot; }, { &quot;B&quot;, &quot;C&quot; } };
JTable table = new JTable(data, colNames);
CustomRenderer renderer = new CustomRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
// table.setDefaultRenderer(String.class, renderer); // if you want to only
// color and center all strings
for (int col = 0; col &lt; table.getColumnCount(); col++) {
// if you want to color and center every entry of the table
table.getColumnModel().getColumn(col).setCellRenderer(renderer);
}
panel.add(table);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
class CustomRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
cellComponent.setBackground(Color.white); // white if not &quot;A&quot; or &quot;B&quot;
if (value instanceof String) {
String temp = (String) value;
if (temp.equals(&quot;A&quot;)) {
cellComponent.setBackground(Color.YELLOW);
} else if (temp.equals(&quot;B&quot;)) {
cellComponent.setBackground(Color.GRAY);
}
}
return cellComponent;
}
}
}

The result will look like:

以值为基础的带有居中值和背景的单元格

Note that your custom renderer only has to be created once, not on every loop. The issue in your code was, that you didn't use your own renderer, but a DefaultTableCellRenderer instead. Also, if you want to only use the custom rendering on a specific class type, e.g. only on String objects, you can use the commented out line instead of iterating through all columns of the table.
And of course you can add further restrictions to the CustomRenderer according to your needs.

huangapple
  • 本文由 发表于 2020年8月6日 15:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63278642.html
匿名

发表评论

匿名网友

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

确定