Solved

Vertical JLabel

Posted on 2000-05-16
8
1,888 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

707 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