tags:

views:

77

answers:

3

Hi! Is there a way to rotate 90º the column headers of a JTable?

A: 

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

npinti
well that's for cells not headers.
Xorty
A: 

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

Xorty
+1  A: 

Check out Darryl's Vertical Table Header Cell Renderer.

camickr