我正在尝试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 Polet:
我运行你的代码,我得到了相同的结果,但是一旦我渲染了整个表格,我会看到以下内容:
在 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;
}
}