Java Tips Weblog

  • Blog Stats

    • 1,218,909 hits
  • Categories

  • Archives

Vertical Label UI

Posted by Darryl Burke on February 21, 2009

Once in a way, one comes across a situation where the ability to render text vertically – rotated 90° – could both conserve screen real estate as well as make for a more aesthetic GUI.

VerticalLabelUI, which extends BasicLabelUI, provides a comprehensive solution to such need.

To the maximum extent possible, VerticalLabelUI applies suitable transforms and invokes the methods of its superclass for laying out and painting the label. As a result, changing the class to extend MetalLabelUI instead of BasicLabelUI will result in rendering a vertical label in the style of the Metal L&F, no other changes being needed. While not exhaustively tested, the same should be true of any custom L&F that uses a LabelUI which inherits from BasicLabelUI.

To use the class, simply invoke setUI on a JLabel:

JLabel label = new JLabel("Label");
label.setUI(new VerticalLabelUI());

The default rotation is anticlockwise (vertical up). To rotate the label clockwise (vertical down) pass a parameter of true to the constructor.

label.setUI(new VerticalLabelUI(true));

Possible uses for VerticalLabelUI include a rotated rowHeaderView for a JScrollPane and in a header renderer for a table which has small column content but long column names. The latter involves using a customized TableHeaderUI and renderer, which will be published in a future posting.

verticallabel

Get The Code

VerticalLabelUI.java

See Also

Vertical Table Header Cell Renderer

Related Reading

Java API: javax.swing.plaf.basic.BasicLabelUI

About these ads

9 Responses to “Vertical Label UI”

  1. Shaun said

    This appears to fail when you center a label in a panel.

    When not rotated (label.setUI(new VerticalLabelUI()); commented out) it does indeed center vertically. However, as soon as we rotate (removing the comments) the text is pushed to the bottom of the panel :-(

    • Darryl Burke said

      Well, that’s to be expected with the label’s default horizontal alignment, which is LEFT. If you want the text at the top of the rotated label, use
      label.setHorizontalAlignment(JLabel.RIGHT);

      cheers, db

  2. vaielet said

    Hello!

    I would like to use html tags in my vertical JLabel. However, if I rotate my JLabel using VerticalLabelUI, I can’t use html tags anymore. Any idea?

    Thanks a lot.

    • Darryl Burke said

      Sorry, I can’t see where you have a problem. Try this:
      import darrylbu.plaf.vertical.VerticalLabelUI;
      import javax.swing.*;

      public class VerticalLabelTest {

      public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {
      new VerticalLabelTest().makeUI();
      }
      });
      }

      public void makeUI() {
      String text = "This is a
      vertical Label
      with HTML";
      JLabel label = new JLabel(text);
      label.setUI(new VerticalLabelUI());

      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(label);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      }
      }

      cheers, db

  3. Darryl Burke said

    Oops, WordPress chewed up the code formatting and the HTML. Here’s another attempt.
    import darrylbu.plaf.vertical.VerticalLabelUI;
    import javax.swing.*;
     
    public class VerticalLabelTest {
     
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
     
          @Override
          public void run() {
            new VerticalLabelTest().makeUI();
          }
        });
      }
     
      public void makeUI() {
        String text = "<HTML><CENTER>This is a<BR>vertical Label<BR>with HTML</CENTER></HTML>";
        JLabel label = new JLabel(text);
        label.setUI(new VerticalLabelUI());
     
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(label);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }

    db

  4. Anonymous said

    Exactly what I need. Thank you very much, Darryl. Your code is used and quoted.

  5. Anonymous said

    Thanks for your code. I am able to use it on a table for vertical column header.

  6. Awesome cheers Darryl. It wasn’t working very well initially because the label bounds were exactly square and caused components to the right of the label to shift and become obscured. But I then realised that it was because I am using JGoodies FormLayout. If you use this layout manager, make sure you set the column size to “preferred” not “default”. HTH.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: