/* (@)CellMarginRendererFun.java */
/* Copyright 2009 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Insets;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.table.TableCellRenderer;
public class CellMarginRendererFun {
static class CellMarginRenderer extends CompoundBorder
implements TableCellRenderer {
/** Serial version UID */
private static final long serialVersionUID = 1L;
private final TableCellRenderer renderer;
public CellMarginRenderer(TableCellRenderer renderer, Insets insets) {
if (insets == null) {
throw new IllegalArgumentException("insets not allowed null");
}
if (renderer == null) {
throw new IllegalArgumentException("renderer not allowed null");
}
this.renderer = renderer;
this.insideBorder =
BorderFactory.createEmptyBorder(insets.top, insets.left,
insets.bottom, insets.right);
}
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
final Component comp =
renderer.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
if (comp instanceof JComponent) {
final JComponent jcomp = (JComponent) comp;
final Border orgBorder = jcomp.getBorder();
this.outsideBorder = orgBorder;
jcomp.setBorder(this);
}
return comp;
}
}
/**
* Test main method.
*
* @param args ignored
*/
public static void main(String[] args) {
final JTable table = new JTable(10, 10);
table.setDefaultRenderer(Object.class, new CellMarginRenderer(table
.getDefaultRenderer(Object.class), new Insets(0, 5, 0, 2)));
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(new JScrollPane(table));
final JFrame f = new JFrame("Test Frame: CellPaddingRendererFun"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}