4

我正在尝试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;
}
}
4

1 回答 1

4

这对我来说似乎很好用(尽管系统 L&F 和默认系统之间的区别并不明显)

示例(上面是原生 L&F,下面是 Metal):

在此处输入图像描述

以及创建这些的代码(只需注释 main 的第一行即可查看默认的 L&F)

import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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(new Vector<Vector<Object>>(), new Vector<String>(Arrays.asList("Hello", "World")));
        int column = 1;
        Component component = table.getTableHeader().getDefaultRenderer()
                .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.setColor(component.getForeground());
        g.setFont(component.getFont());
        component.paint(g);
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "Test", JOptionPane.INFORMATION_MESSAGE, null);
        System.exit(0);
    }
}

编辑:确实 Windows“皮肤”没有正确绘制。我不确定为什么它不直接绘制(可能是因为渲染器不是“实时”组件)。然而,一个简单的技巧也是打印它,是打印整个表头,但使用一个 Graphics ,它被转换为我们想要的列标题并裁剪为列标题的大小:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTable table = new JTable(new Vector<Vector<Object>>(), new Vector<String>(Arrays.asList("Hello", "World")));
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
        int column = 1;
        Rectangle headerRect = table.getTableHeader().getHeaderRect(column);
        BufferedImage img = new BufferedImage(headerRect.width, headerRect.height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();
        g.translate(-headerRect.width, 0);
        table.getTableHeader().print(g);
        frame.add(new JLabel(new ImageIcon(img)), BorderLayout.SOUTH);
        frame.pack();
    }
}
于 2013-03-21T12:45:09.480 回答