Hi guys,
 
I have solved both questions I posted 2 days ago, here are my solutions:
 
1) vertical table headers: I created a sub class of JTableHeader which sets a new UI to the DefaultTableCellRenderer. Note that the DefaultTableCellRenderer is actually a sub class of JLabel. The new UI takes the current text, generates an image of the text in a vertical sequence and sets it in the JLabel.
 
Here is the code for that:
//the vertical JTableHeader class
public class VerticalTableHeader extends JTableHeader {
    public VerticalTableHeader() {
        super();
        JLabel label = (DefaultTableCellRenderer)getDefaultRenderer();
        label.setUI(new VerticalLabelUI(false));
    }
 
    public VerticalTableHeader(TableColumnModel cm) {
        this();
        setColumnModel(cm);
    }
}
//the vertical UI
public class VerticalLabelUI extends BasicLabelUI {
    static {
        labelUI = new VerticalLabelUI(false);
    }
 
    protected boolean clockwise;
 
    /**
     * VerticalLabelUI constructor
     * @param  clockwise  direction of the text.
     */
    public VerticalLabelUI( boolean clockwise ) {
        super();
        this.clockwise = clockwise;
    }
 

    public Dimension getPreferredSize(JComponent c) {
        Dimension dim = super.getPreferredSize(c);
        return new Dimension( dim.height, dim.width );
    }
 
    private static Rectangle paintIconR = new Rectangle();
    private static Rectangle paintTextR = new Rectangle();
    private static Rectangle paintViewR = new Rectangle();
    private static Insets paintViewInsets = new Insets(0, 0, 0, 0);
 
    public void paint(Graphics g, JComponent c) {
        JLabel label = (JLabel)c;
        String text = label.getText();
        Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
 
        if ((icon == null) && (text == null)) {
            return;
        }
 
        FontMetrics fm = g.getFontMetrics();
        paintViewInsets = c.getInsets(paintViewInsets);
 
        paintViewR.x = paintViewInsets.left;
        paintViewR.y = paintViewInsets.top;
 
        // Use inverted height & width
        paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
        paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
 
        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
 
        String clippedText =
        layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
 
        Graphics2D g2 = (Graphics2D) g;
        AffineTransform tr = g2.getTransform();
        if ( clockwise ) {
            g2.rotate( Math.PI / 2 );
            g2.translate( 0, - c.getWidth() );
        } else {
            g2.rotate( - Math.PI / 2 );
            g2.translate( - c.getHeight(), 0 );
        }
 
        if (icon != null) {
            icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
        }
 
        if (text != null) {
            int textX = paintTextR.x;
            int textY = paintTextR.y + fm.getAscent();
 
            if (label.isEnabled()) {
                paintEnabledText(label, g, clippedText, textX, textY);
            } else {
                paintDisabledText(label, g, clippedText, textX, textY);
            }
        }
 
        g2.setTransform( tr );
    }
}
 
---------------------------------------------------------------------------
 
2) The squashed table headers: this problem is caused since my first header (the left one) was an empty String, once I changed the header's value to " " (a white space), the problem was solved.
 
I recommend to copy and save this code, it will help you save a few hours of investigating almost all classes in the swing.table package...  :o)
 
 
I want to thank anyone who tried to help!
David Treves.
 
----- Original Message -----
To: JDJList
Sent: Monday, June 10, 2002 7:09 AM
Subject: [jdjlist] Re: 2 JTable questions

Check out my comments below.
----- Original Message -----
To: JDJList
Sent: Sunday, June 09, 2002 3:18 PM
Subject: [jdjlist] 2 JTable questions

Hi all,
 
1) I wish to set vertical headers in a JTable, is that possible? if so, how is it done?
[Madhav]
As such, there is no direct support for vertical headers.
 
2) For some reason my current headers look like they were squashed... Their height is very short, I don't know why that happens, I simply create a table, generate the headers and add the table to a scroll pane, I can't seem to overcome that problem. Did anyone see something like that? I am sure I am missing something in here...
[Madhav]
Which look&feel are you using? and also the OS.
Try table.column.setHeaderRenderer(TableCellRenderer) to increase the header height.
 
 
 
Thanks,
David.
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to