4

I am trying to make a JTabbedPane in Java 7 on OSX that has tabs positioned to the left with their text horizontal (instead of vertical). However, with the code:

import javax.swing.*;
import java.awt.Dimension;


class Probs extends JDialog {
    JTabbedPane options = new JTabbedPane();
    Probs(JFrame owner) {
        //main constructor
        super(owner, "User Preferences", true);
        //set the tabs to be left aligned
        options.setTabPlacement(JTabbedPane.LEFT);

        //construct the authorization panel
        JPanel authorization = new JPanel();
        authorization.add(new JLabel("test"));
        options.addTab("test", authorization);

        add(options);
        setSize(new Dimension(300,300)); //should use pack here
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);           
    }

    public static void main(String[] args) {
        JFrame test = new JFrame();
        new Probs(test);
        test.dispose();
    }
}

I get a dialog box which looks like: this image

I would like the tab text to be horizontal (the 'test' title on the tab be oriented horizontally instead of vertically).

I searched around on Google for a while and have only run into occurrences wherein people wanted to achieve vertical text on their tabs, I could not manage to locate any in which people wanted to have horizontal text (what I am trying to achieve).

In particular, I am trying to achieve something which exactly looks like the image mentioned in the first post of this question. It is basically the exact opposite of that question because the person in that tab started with what I am trying to achieve (I believe). Basically, I am trying to determine how to create the image displayed in the first post of that question.

Can someone please tell me how to have left-oriented tabs while preserving horizontal tab titles (as opposed to vertical)?

Thank you for your time and assistance.

Community
  • 1
  • 1
cryptopi
  • 303
  • 9
  • 19
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) A call to `pack()` is always valuable. Use it **instead of** `setSize(..)`.. – Andrew Thompson Dec 15 '13 at 01:46
  • @AndrewThompson, thanks for posting the image for me, and I posted an SSCCE (I hope, correctly) – cryptopi Dec 15 '13 at 02:00
  • Just about to head off to bed, but I suspect (indeed hope) some kind soul will have sorted this for you before I get up. Nice SSCCE BTW! – Andrew Thompson Dec 15 '13 at 02:07
  • You're code works fine for me. It presents the tab label horizontally. I'm running on Windows. It may be something with the OSX look and feel. Not sure. – Paul Samsotha Dec 15 '13 at 03:36
  • Since I can't replicate the the problem, I can offer a suggestion. Have you tried, using HTML as in the post your linked. Maybe without the `
    's`. `options.addTab("Test", authorization);`
    – Paul Samsotha Dec 15 '13 at 03:47
  • @peeskillet, good idea. However, I just tried that and the results were identical to the result without using HTML (that is, displayed in a vertical fashion) – cryptopi Dec 15 '13 at 03:50

3 Answers3

1

Again, since I can't replicate the problem, Try this suggestion:

    JPanel authorization = new JPanel();
    authorization.add(new JLabel("test"));
    options.addTab("", authorization);
    JLabel labTab2 = new JLabel("test");    // create a label
    options.setTabComponentAt(0, labTab2);  // set it to the component
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for the idea, but I tried it and the result can be found there: http://i.imgur.com/Rw3JFiW.png. Namely, it appeared to have the desired effect of the text being horizontal but the tab became not sufficiently wide to display all of the text. Any idea how to fix that? – cryptopi Dec 15 '13 at 14:38
1

The alignment is determined by your operating system. If you want to change the alignment of the tab text, you have to change the look and feel of your swing application. This worked for me. See here.

Monkey Supersonic
  • 1,165
  • 1
  • 10
  • 19
1

The system look and feel at MacOSX didn't support what you want in JTabbedPane. You must create a customized JComponent to do this or to set the look and feel of your application to cross platform (java metal) as stated before by @MonkeySupersonic.

I suggest the readings:

Amadeu Barbosa
  • 314
  • 2
  • 6