我想要一個表頭單元格渲染與下面的代碼BufferedImage
:Java Swing:Render Component incl。 LAF成的BufferedImage
Component component = table.getTableHeader().getDefaultRenderer().
getTableCellRendererComponent(table, value, selected, focused, 0, 0);
component.setBounds(0, 0,
table.getColumnModel().getColumn(0).getWidth(),
table.getRowHeight());
BufferedImage img = new BufferedImage(component.getWidth(),
component.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(component.getForeground());
g.setFont(component.getFont());
component.paint(g);
此代碼是TableCellRenderer#getTableCellRendererComponent
方法中運行。
該組件被渲染到BufferedImage中,但是Look-and-Feel沒有被應用。 LaF已經在應用程序啓動時設置爲系統的默認值。
爲了渲染組件的應用LaF,我還需要做些什麼?
編輯:
@Guillaume波萊:
我運行代碼,我也得到了相同的結果,但一旦我渲染整個表我看到以下內容:
在Linux上:
在Windows上:
在Linux上,代碼似乎可以工作,而在Windows中則省略了LaF。
在Windows上我希望是這樣的:
我的代碼:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.CellRendererPane;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
class TestPrint {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException,
IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestPrint().run();
}
});
}
protected void run() {
JTable table = new JTable(createData());
int column = 1;
TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer();
Component component = renderer.getTableCellRendererComponent(table, table.getColumnName(column), false,
false, -1, column);
component.setBounds(0, 0, table.getColumnModel().getColumn(0).getWidth(), table.getRowHeight());
BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setClip(0, 0, component.getWidth(), component.getHeight());
g.setColor(component.getForeground());
g.setFont(component.getFont());
CellRendererPane cellRendererPane = new CellRendererPane();
cellRendererPane.paintComponent(g, component, table, 0, 0, component.getWidth(), component.getHeight());
// SwingUtilities.paintComponent(g, component, table,
// new Rectangle(0, 0, component.getWidth(), component.getHeight()));
// component.paint(g);
showTable(table);
showImage(img);
}
private void showImage(BufferedImage img) {
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "Test",
JOptionPane.INFORMATION_MESSAGE, null);
}
private void showTable(JTable table) {
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
private DefaultTableModel createData() {
DefaultTableModel data = new DefaultTableModel(0, 6);
for (int i = 0; i < 30; i++) {
Vector v = new Vector();
for (int k = 0; k < 6; k++) {
v.add(new Float(k/(float) i));
}
data.addRow(v);
}
return data;
}
}
我已經編輯了這個問題。 – 2013-03-21 13:50:09
@ coding.mof在最後看到我編輯的新代碼。 – 2013-03-21 14:36:31
非常感謝!我自己從來沒有想出這個解決方案。 – 2013-03-21 14:39:40