Solved

Vertical JLabel

Posted on 2000-05-16
8
1,883 Views
Last Modified: 2013-11-23
How can I orientate my JLabel vertically
(Like for a JSlider:
         JSlider.setOrientation(JSlider.VERTICAL))
Please help me ???
0
Comment
Question by:Visioner
8 Comments
 

Accepted Solution

by:
id earned 100 total points
ID: 2815206
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
 


--------------------------------------------------------------------------------
18-nov-99
7:09
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) {
    this.value=value;
    valueChar= new char[value.length()];
    valueLabel= new JLabel[value.length()];
    value.getChars(0,value.length(),valueChar,0);
    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));
      valueLabel[i].setOpaque(true);
      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){
    try{
      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){
      ex.printStackTrace();
      return null;
    }
  }
}

bye bye
======================================



0
 
LVL 4

Expert Comment

by:kylar
ID: 2815360
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.

Kylar
0
 

Expert Comment

by:id
ID: 2818722
to Kylar: just curious - what class is BasicLabelUI in?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Expert Comment

by:kylar
ID: 2818896
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!

Kylar
0
 

Expert Comment

by:id
ID: 2819573
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.
0
 
LVL 4

Expert Comment

by:kylar
ID: 2822315
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. :)

Kylar
0
 
LVL 3

Expert Comment

by:kostello
ID: 2834921
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.
        add(testLabel);
    }

    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) {
                                        System.exit(0);
                                    }
                                });

        frame.setContentPane(new LabelDemo());
        frame.pack();
        frame.setVisible(true);
    }
    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();
        }
    }
}
0
 

Author Comment

by:Visioner
ID: 2855571
Comment accepted as answer
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 105
Cipher Configuration on Apache HTTPD 4 62
jsp login check 12 32
Convert from a json string array to a Java object 3 32
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question