开发者

Vertical headers in JTable?

开发者 https://www.devze.com 2023-01-05 02:17 出处:网络
Is there a way to rotate 90º the column headers of a JT开发者_JAVA百科able?Check out Darryl\'s Vertical Table Header Cell Renderer.This is little tricky. At first, you need to cast JTable headers to

Is there a way to rotate 90º the column headers of a JT开发者_JAVA百科able?


Check out Darryl's Vertical Table Header Cell Renderer.


This is little tricky. At first, you need to cast JTable headers to JLabels. It's just like

 ((JLabel)table.getTableHeader()

Then rotate JLabels. It's already answered here on StackOverflow


Maybe this helps, I haven't tested it though:

class RotatedTableCellRenderer extends JLabel implements TableCellRenderer {  
    protected int m_degreesRotation = -90;  

    public RotatedTableCellRenderer(int degrees) {  
        m_degreesRotation = degrees;  
    }  

    public Component getTableCellRendererComponent(JTable table, Object value,   
        boolean isSelected, boolean hasFocus, int row, int column) {  

        try {  
            this.setText(value.toString());  
        } catch(NullPointerException ne) {  
                this.setText("Nullvalue");  
        }  

        return this;  
    }  

    public void paint(Graphics g) {  
        Graphics2D g2 = (Graphics2D)g;  
        g2.setClip(0,0,500,500);  
        g2.setColor(new Color(60,179,113));  
        g2.setFont(new Font("Arial",Font.BOLD,12));  
        AffineTransform at = new AffineTransform();  
        at.setToTranslation(this.getWidth(), this.getHeight());  
        g2.transform(at);  
        double radianAngle = ( ((double)m_degreesRotation) / ((double)180) ) * Math.PI;  
        at.setToRotation(radianAngle);  
        g2.transform(at);  
        g2.drawString(this.getText(), 0.0f, 0.0f);  
    }  
}  

This is not my own, taken from here

0

精彩评论

暂无评论...
验证码 换一张
取 消