将cellpadding添加到Java JTable [英] Add cellpadding to a Java JTable

查看:76
本文介绍了将cellpadding添加到Java JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现Swing JTable.我遵循了 http://docs.oracle.com/上的教程javase/tutorial/uiswing/components/table.html#simple

I'm trying to implement a Swing JTable. I followed the tuorial on http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#simple

我希望表格单元格不可编辑(这可行),并且我希望表格单元格的边框具有更多填充.就像HTML中的单元格填充一样.

I want the table cell's not to be editable (this works) and I want the table cells to have more padding to it's borders. Like cellpadding in HTML.

这是我的代码的一部分,cellpadding无效.

This is part of my code and the cellpadding thing doesn't work.

class BoardTableCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
        return this;
    }
}

String[] columnNames = {"Datei",
        "Zeile",
        "Zeichen",
        "Fehler", "test"};

Object[][] data = {
        {"Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe", "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White", "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown", "Pool", new Integer(10), new Boolean(false)}
    };

JTable table = new JTable(data, columnNames){
    private static final long serialVersionUID = -4430174981226468686L;

    @Override
    public boolean isCellEditable(int arg0, int arg1) {
        return false;
    }};

table.setAutoCreateRowSorter(true);
table.getTableHeader().setReorderingAllowed(false);
table.setDefaultRenderer(String.class, new BoardTableCellRenderer());

table放在JScrollPane上.显示该表,单元格不可编辑,但未应用单元格填充!

table is placed on a JScrollPane. The table is displayed, the cells are not editable but the cellpadding is not applied!

任何人都可以帮忙吗?谢谢:)

Can anyone help? Thanks :)

推荐答案

DefaultTableModel中的getColumnClass()的默认实现由JTable使用(默认),返回Object.class.这就是不使用BoardTableCellRenderer的原因,因为您正在为使用String.class的列设置它.

Default implementation of getColumnClass() in DefaultTableModel which is used by JTable (by default) returns Object.class. That is the reason BoardTableCellRenderer is not used, as you're setting it up for columns with String.class.

您可以覆盖getColumnClass.或在此示例的情况下,替换为:

You may override getColumnClass. Or in case of this sample, replace:

table.setDefaultRenderer(String.class, new BoardTableCellRenderer());

具有:

table.setDefaultRenderer(Object.class, new BoardTableCellRenderer());

查看BoardTableCellRenderer的效果.

这篇关于将cellpadding添加到Java JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆