/* (@)ItalicCellRendererTestGui.java */
/* Copyright 2010 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.Dimension;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
public class ItalicCellRendererTestGui {
private static class ItalicCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
protected void setValue(Object value) {
if (value instanceof String) {
final String s = (String) value;
final String regExp = "(?<=\\S+\\s)\\S+";
final String replacement =
"<span style=\"font-weight:bold;"
+ "text-decoration:underline;"
+ "font-style:italic;"
+ "color:red;\">$0</span>";
value = "<html>" + s.replaceFirst(regExp, replacement);
}
super.setValue(value);
}
}
private static void createAndShowGui() {
final Object[][] tableData =
{ { "Ein bisschen Text hier", "Ein bisschen Text da" },
{ "Ein anderer Text hier", "Noch ein anderer Text da" },
{ "Und hier noch mehr Text", "Und da auch" }, };
final String[] columnNames = { "Links", "Rechts" };
final JTable table = new JTable(tableData, columnNames);
table.setDefaultRenderer(Object.class, new ItalicCellRenderer());
table.setFont(table.getFont().deriveFont(24f));
table.setRowHeight(32);
table.setPreferredScrollableViewportSize(new Dimension(600, 200));
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(new JScrollPane(table));
final JFrame f = new JFrame("Test Frame: ItalicCellRendererTestGui"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}