?
Solved

Vertical JLabel

Posted on 2000-05-16
8
Medium Priority
?
1,890 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Accepted Solution

by:
id earned 300 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

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