4

As you can see from the image below, the Java text is horizontal. What I would like to do is get a vertical orientation of the JTabbedPane Titles.

While googling, I found that the only way is to add extra library. But I was wondering if this can be done without any extra library?

I would like for Title1 and Title2 to be vertically oriented and not horizontally

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89

3 Answers3

11

You can use a JLabel with a custom LabelUI as described in this answer, it gives the result I expected:

Vertical text on JTabbedPane

JTabbedPane tabPane = new JTabbedPane(JTabbedPane.LEFT);

// Add tabs with no text
tabPane.addTab(null, component1);
tabPane.addTab(null, component2);

// Create vertical labels to render tab titles
JLabel labTab1 = new JLabel("Tab #1");
labTab1.setUI(new VerticalLabelUI(false)); // true/false to make it upwards/downwards
tabPane.setTabComponentAt(0, labTab1); // For component1

JLabel labTab2 = new JLabel("Tab #2");
labTab2.setUI(new VerticalLabelUI(false));
tabPane.setTabComponentAt(1, labTab2); // For component2
Community
  • 1
  • 1
Matthieu
  • 2,736
  • 4
  • 57
  • 87
2

you have to use Html syntax, for any changes to the disabled Tab too

tabbedPane.addTab("<html>T<br>i<br>t<br>t<br>l<br>e<br>1</html>", panel1);  

EDIT

SSCCE for question about Html text formatting and alignment

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

/**
 *
 * @author korbel
 */
public class TestTabbedPane extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTabbedPane tabbedPane;

    public TestTabbedPane() {
        tabbedPane = new JTabbedPane();
        tabbedPane.setPreferredSize(new Dimension(300, 200));
        getContentPane().add(tabbedPane);
        JPanel panel = new JPanel();
        tabbedPane.add(panel, "null");
        JTextField one = new JTextField("one");
        tabbedPane.add(one, "one");
        JTextField two = new JTextField("two");
        tabbedPane.add(two, "<html> T<br>i<br>t<br>t<br>l<br>e <br> 1 </html>");
        tabbedPane.setEnabledAt(2, false);
        /*int comp = tabbedPane.getComponentCount();
        for (Component sc : tabbedPane.getComponents()) {
        if (sc instanceof javax.swing.JLabel) {
        JLabel lbl = (JLabel) sc;
        lbl.setForeground(Color.red);
        }
        if (sc instanceof javax.swing.JPanel) {
        JPanel pnl = (JPanel) sc;
        pnl.setName(pnl.getName());
        }
        if (sc instanceof javax.swing.JTextField) {
        JTextField txt = (JTextField) sc;
        txt.setForeground(Color.blue);
        txt.setDisabledTextColor(Color.red);
        }
        }
        UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
        UIManager.put("TabbedPane.highlight", new Color(255, 0, 0));
        UIManager.put("TabbedPane.lightHighlight", new Color(0, 255, 0));
        UIManager.put("TabbedPane.darkShadow", new Color(0, 255, 0));
        UIManager.put("TabbedPane.shadow",new Color(0, 0, 255));
        UIManager.put("TabbedPane.light" ,  new Color(0, 255, 0));
        UIManager.put("TabbedPane.foreground", new Color(0, 0, 0));
        UIManager.put("JTabbedPane.font", new Font("Dialog", Font.ITALIC, 12));
        UIManager.put("TabbedPane.selected", new Color(255, 0, 0));
        UIManager.put("disable", new Color(255, 0, 0));
        UIManager.put("TabbedPane.selectHighlight" , new Color(0, 0, 0));
        UIManager.put("TabbedPane.background",  new Color(0, 0, 0));
        SwingUtilities.updateComponentTreeUI(tabbedPane);*/
        tabbedPane.setTitleAt(2, "<html><font color="
                + (tabbedPane.isEnabledAt(2) ? "black" : "red") + ">"
                + tabbedPane.getTitleAt(2) + "</font></html>");
        tabbedPane.setTabPlacement(JTabbedPane.LEFT);
    }

    public static void main(String args[]) {
        TestTabbedPane frame = new TestTabbedPane();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Does it actually work with disabled tabs? (I mean as in render the correct colors - `JButton` does not.) – Andrew Thompson Jan 23 '12 at 12:28
  • @Andrew Thompson (-: BNI :-) can you be, sooooo little bit concrete, are you meaning to put JButton to the Tab, because I meant only visual text properties, JTabbedPane was created in year one-two and untill now nobody updated those protected / private methods which aren't accesible from outside – mKorbel Jan 23 '12 at 12:35
  • and how can I change the font size of the title because vertically it is too big? – Adel Boutros Jan 23 '12 at 12:56
  • sorry I didn't see / catch your question, are you able to solve that by setting FontSize in Html syntax ??? +1 – mKorbel Jan 24 '12 at 12:58
  • I tried `` . It didn't work, it showed me the whole html text instead of the title – Adel Boutros Jan 24 '12 at 13:03
  • @Adel Boutros I'll try to look for one code where about Change Tab Title Color for disabled Tab, I think that there is full hack for Tab title .... – mKorbel Jan 24 '12 at 13:11
  • please ask a new question Html rellated how to alignment text to CENTER and change Font Family and Size, because I not able to set together and without that everything is too ugly – mKorbel Jan 24 '12 at 13:43
1

If you use WebLaf LoolAndFeel Library There is a component called WebVerticalLabel that can have vertical text.

JTabbedPane .setTabComponentAt(1, new WebVerticalLabel("Title1"));
Thusitha Wickramasinghe
  • 1,063
  • 2
  • 15
  • 30