JTable中的垂直标题?

时间:2010-06-27 20:59:33

标签: java swing jtable

有没有办法将JTable的列标题旋转90º?

3 个答案:

答案 0 :(得分:8)

答案 1 :(得分:3)

这有点棘手。首先,您需要将JTable标头转换为JLabel。这就像

 ((JLabel)table.getTableHeader()

然后旋转 JLabels。它已经回答了here on StackOverflow

答案 2 :(得分:0)

也许这有帮助,但我没有测试过它:

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);  
    }  
}  

这不是我自己的,取自here