Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1900
  • Last Modified:

Vertical JLabel

How can I orientate my JLabel vertically
(Like for a JSlider:
Please help me ???
1 Solution
One approach:
JButton with literally vertical text
18-nov-99 : 5:06 :akris2  
Hi JavaProff..,

I have a problem with the button.I want the JButton to have the text in the vertical direction and the same buuton should act as the header for the JTable. I have checked out with the ComponentOrientation source file but i could not get a solution b'coz the static final variables were not defined for the vert and top. Can anyone please help me out ?????

Bye Krishna

dusal       (#1 of 1) Add New Message  
 a simple approach is create a component to show a vertical text then you could use this component as a cell render
import javax.swing.*;
import java.awt.*;
public class VerticalLabel extends JPanel {
  private int nbrElement = 0;
  private String value=null;
  private char[] valueChar =null;
  private JLabel[] valueLabel=null;
  public VerticalLabel(String value) {
    valueChar= new char[value.length()];
    valueLabel= new JLabel[value.length()];
    this.setLayout(new GridBagLayout());
    for(int i=0;i<valueChar.length;i++){
      char[] temp = new char[1];
      temp[0] = valueChar[i];
      valueLabel[i]=new JLabel(new String(temp));
      this.add(valueLabel[i],createGridBagConstraints(0 , i, 1, 1,0.0, 0.0,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
  public static GridBagConstraints createGridBagConstraints(int gridx,
                                        int gridy,
                                        int gridwidth,
                                        int gridheight,
                                        double weightx,
                                        double weighty,
                                        int anchor,
                                        int fill,
                                        java.awt.Insets insets,
                                        int ipadx,
                                        int ipady){
      GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = gridx;
      gridBagConstraints.gridy = gridy;
      gridBagConstraints.gridwidth = gridwidth;
      gridBagConstraints.gridheight = gridheight;
      gridBagConstraints.weightx = weightx;
      gridBagConstraints.weighty = weighty;
      gridBagConstraints.anchor =  anchor;
      gridBagConstraints.fill = fill;
      gridBagConstraints.insets = insets;
      gridBagConstraints.ipadx = ipadx;
      gridBagConstraints.ipady = ipady;
      return gridBagConstraints;
    }catch(Exception ex){
      return null;

bye bye

That is an easier way, but not very elegant. you could create your own class of JLabel, and extend BasicLabelUI to paint it vertically instead of horizontally.

to Kylar: just curious - what class is BasicLabelUI in?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

BasicLabelUI is it's own class, it's a UI class that the JLabel UI is built on. The full packagized class name is javax.swing.plaf.basic.BasicLabelUI. Swing is based on a M-V-C architecture, where the Model, View and Controller are all done seperately. In this case, the View is a BasicLabelUI, the Model is a JLabel, and there isn't really a controller for labels. As with all Swing components, you can create your own UI and simply plug it in to an existing component. Assuming that they are compatible, there shouldn't be any problem, and the component should be rendered as detailed in the new UI Class, and everything else will compensate accordingly. This is easier said than done, however, since calculating paint methods, and overriding UI classes is a lot of work. Good Luck!

Another easy imperfect solution for vertical orientation would be to use html instead of text, and insert html breaks tags after each char, e.g. JLabel verticalLabel = new JLabel("<html>l<br>a<br>b<br>e<br>l</html>"); This could be extended to a convinience class simular to previous solution.

Be aware it's not supported with old Swing versions.

to Kylar: another problem with implementing own paint() to draw text vertically would be a lost of pluggable look and feel and "some other Swing display tricks". I think a proper way would be if ComponentOrientation constants would be extended in future to include top-to-bottom and bottom-to-top.
Actually, if you set the UI properly, give it the correct bounds and stuff, all the PLAF stuff should work fine, the layout managers would compensate accordingly, I think. That's the beauty of the PLAF system, and also why it's such a pain to implement sometimes. :)

Another alternative is to treat your text as an icon, and use your own text Icon class to display.  A simple example follows (note - there's still a small hole where the size needs to be initially calculated)

import java.awt.*;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Icon;

public class LabelDemo extends JPanel {
    JLabel testLabel;

    public LabelDemo() {
        Icon icon = new myIcon("Test");
        setLayout(new GridLayout(1,1));

        testLabel = new JLabel(icon);

        //Add label to the JBufferedPane.

    public static void main(String[] args) {
         * Create a window.  Use JFrame since this window will include
         * lightweight components.
        JFrame frame = new JFrame("LabelDemo");

        frame.addWindowListener(new WindowAdapter() {
                                    public void windowClosing(WindowEvent e) {

        frame.setContentPane(new LabelDemo());
    private class myIcon implements Icon {
        private String myText;
        private int h=17, w=12;
        public myIcon(String text) {
            myText = text;
        public void paintIcon(java.awt.Component A, java.awt.Graphics B, int C, int D) {
            // this really needs to be done before getIconWidth & Height are done,
            // which occurs before the paint gets called
            FontMetrics fm = B.getFontMetrics();
            int charHeight = fm.getHeight();
            h = fm.getHeight();
            w = fm.stringWidth("W");
            System.out.println("In paintIcon");
            int height = charHeight;
            for (int i = 0; i < myText.length(); i++) {
                B.drawString(myText.substring(i, i+1), 0, height);
                height += charHeight;
        public int getIconWidth() {
            return w;
        public int getIconHeight() {
            return h*myText.length();
VisionerAuthor Commented:
Comment accepted as answer

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now