Solved

Vertical JLabel

Posted on 2000-05-16
8
1,875 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
Comment Utility
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
Comment Utility
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
Comment Utility
to Kylar: just curious - what class is BasicLabelUI in?
0
 
LVL 4

Expert Comment

by:kylar
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:id
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Comment accepted as answer
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now