2013-03-21 36 views
4

我想要一個表頭單元格渲染與下面的代碼BufferedImageJava 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上:
enter image description here
在Windows上:
enter image description here
在Linux上,代碼似乎可以工作,而在Windows中則省略了LaF。
在Windows上我希望是這樣的:
enter image description here

我的代碼:

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

這似乎爲我工作正常(雖然它不是abvious系統L的差& F和默認的)

實施例(上文是天然I & F和下面是金屬):

enter image description here

和代碼來創建這些(簡評主要的第一線,看到默認的大號& 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的「皮」不正確塗。我不確定它爲什麼不直接繪製(可能是因爲渲染器不是「活」組件)。然而,一個簡單的竅門是打印也被打印整個表頭,但用它翻譯成我們想要的列標題,並夾在列標題大小的圖形:

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(); 
    } 
} 
+0

我已經編輯了這個問題。 – 2013-03-21 13:50:09

+0

@ coding.mof在最後看到我編輯的新代碼。 – 2013-03-21 14:36:31

+0

非常感謝!我自己從來沒有想出這個解決方案。 – 2013-03-21 14:39:40