如何在单元格之间设置空间,以便不擦除单元格的内容

时间:2013-11-06 17:18:59

标签: java swing jtable

我的下一个恼人的问题是在单元格之间设置空间,以便不会擦除单元格的内容。 我已经尝试过所有互联网解决方案,但它们没有帮助,因为当我增加边界时,单元格的内容被删除。 这是我试过的:

public class DateFormatDemo extends JFrame
{
    private Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
    private JTable dataSearchResultTable;

    public DateFormatDemo()
    {
        JPanel panel = new JPanel(new GridLayout(2, 1, 5, 10));
        panel.setPreferredSize(new Dimension(500, 300));
        panel.add(new JScrollPane(initDataSearchResultTable()));
        super.getContentPane().add(panel);
        super.pack();
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
        super.setVisible(true);
    }

    private JTable initDataSearchResultTable()
    {
        dataSearchResultTable = new JTable(new MyTableModel()) {

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
            {
                JComponent component = (JComponent) super.prepareRenderer(renderer, row, column);
//              component.setBorder(emptyBorder);
                return component;
            }
        };
        dataSearchResultTable.setIntercellSpacing(new Dimension(5, 10));
        dataSearchResultTable.setSelectionBackground(new Color(0xaaaaff));
        dataSearchResultTable.setFillsViewportHeight(true);
        dataSearchResultTable.setRowSelectionAllowed(true);
        dataSearchResultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        dataSearchResultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        dataSearchResultTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column)
            {
                JComponent component = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                component.setBorder(emptyBorder);
                return component;
            }
        });
        return dataSearchResultTable;
    }

    class MyTableModel extends AbstractTableModel
    {
        private String[] columnNames = { "First Name", "Last Name", "Timestamp", "Number", "Vegetarian" };
        private Object[][] data ;

        MyTableModel()
        {
            data = new Object[][] {
                    { "Vova", "KipokKipokKipokKipok", Timestamp.valueOf("2013-04-12 11:20:41"), new Integer(5),
                            new Boolean(true) },
                    { "Olia", "Duo", Timestamp.valueOf("2010-01-11 11:11:41"), new Integer(3), new Boolean(false) },
                    { "Oksana", "Stack", Timestamp.valueOf("2012-04-12 11:20:41"), new Integer(2), new Boolean(false) },
                    { "Petro", "White", Timestamp.valueOf("2010-04-12 11:20:21"), new Integer(20), new Boolean(true) },
                    { "Ivan", "Brown", Timestamp.valueOf("2011-04-11 11:20:41"), new Integer(10), new Boolean(true) } };
            fireTableDataChanged();
        }

        public int getColumnCount()
        {
            return columnNames.length;
        }

        public int getRowCount()
        {
            return data.length;
        }

        public String getColumnName(int col)
        {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col)
        {
            if (data.length > 0 && data[0] != null) {
                return data[row][col];
            }
            return null;
        }

        /*
         * JTable uses this method to determine the default renderer/ editor for
         * each cell. If we didn't implement this method, then the last column
         * would contain text ("true"/"false"), rather than a check box.
         */
        public Class getColumnClass(int c)
        {
            Object valueAt = getValueAt(0, c);
            return valueAt == null ? Object.class : valueAt.getClass();
        }

        /*
         * Don't need to implement this method unless your table's editable.
         */
        public boolean isCellEditable(int row, int col)
        {
                return true;
        }

        /*
         * Don't need to implement this method unless your table's data can
         * change.
         */
        public void setValueAt(Object value, int row, int col)
        {
            if (data.length > 0 && data[0] != null) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
        }

    }

    public static void main(String[] args) throws ParseException
    {
        new DateFormatDemo();
    }
}

有人知道如何在单元格之间设置空格,以便不删除单元格的内容吗?

解决:

dataSearchResultTable.setRowHeight(25);
        dataSearchResultTable.setRowMargin(5);
        dataSearchResultTable.getColumnModel().setColumnMargin(20);

但是rowMargin导致选定的行比行边界窄。解决方案是仅在rowHeight设置rowMargin。这确保了选择不比行本身窄。

谢谢!

1 个答案:

答案 0 :(得分:1)

我建议你不要像你提到的那样使用setRowHeight()方法,而是建议你为你的表使用next方法(found here):

private void updateRowHeights(JTable table ) {
    for (int row = 0; row < table.getRowCount(); row++) {
        int rowHeight = table.getRowHeight();
        for (int column = 0; column < table.getColumnCount(); column++) {
            Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
            rowHeight = Math.max(rowHeight,comp.getPreferredSize().height);
        }
        table.setRowHeight(row, rowHeight);
    }
}

因为它计算每行的高度。在这种情况下,所有行都可以正常显示。

当您使用setRowHeight()并不总是解决系统默认属性的问题时,请尝试将该行添加到您的代码中,您会看到我的意思dataSearchResultTable.setFont(new Font("Arial", 1, 25));