2

Im using a jTabbedpane for my application. In normal window size, its like this.

enter image description here

But when its maximized, Its like this. (Tabs do not change their size).

enter image description here I want to adjust tabs as to distribute evenly in any window size. Thank You.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Hasi007
  • 146
  • 2
  • 14
  • 1
    *Tabs do not change their size)* thats the expected behavior... Why is it that space needs to be filled so bad?? But anyhow please post and [SSCCE](http://sscce.org) – David Kroukamp Jan 03 '13 at 17:48
  • @DavidKroukamp Thats space distroys app's neatness. User may change the window size as he wish. so that i don't want the space to be there. suggest me a method please. BTW how to post a SSCCE on a question like this? Thank you. – Hasi007 Jan 03 '13 at 17:52
  • The whole point of an SSCCE is to replicate the problem, so we dont have to write the code (and we can see specific problems with your code). As for the tabs spacing, its most likely by coincidence the tabs reach the end of the `JFrame` in fact I think you are calling `setSize(..)` you should be using `pack()` on `JFrame`, as `JTabbedPane` by default does not worry about tab spacing. You could pad the titles with spacing to fill the window via white spaces or better html... see [here](http://stackoverflow.com/questions/476678/tabs-with-equal-constant-width-in-jtabbedpane) – David Kroukamp Jan 03 '13 at 18:00
  • @DavidKroukamp Found the code. Thank you for your helps. – Hasi007 Jan 03 '13 at 19:12
  • As a side note the tab Drugs I find really I dont know unfitting .. maybe prescription medicine or something but DRUGS lol :) – David Kroukamp Jan 03 '13 at 21:26
  • @DavidKroukamp :D we are making a clinical system..so DRUGS= Medicine.. :P – Hasi007 Jan 04 '13 at 12:38

3 Answers3

3

I found a code.

        int wid = (tp_main.getSize().width)/6;
        for(int i=0; i<6;i++){
            String name= tp_main.getTitleAt(i);
            tp_main.setTitleAt(i,"<html><div style=\"width: "+new Integer(wid)+"px\">"+new String(name)+"</div></html>");
        }

tp_main is the jTabbedpane

int wid = (tp_main.getSize().width)/6;  

here devided by 6 as i have 6 tabs.

Hasi007
  • 146
  • 2
  • 14
  • FYI: there is no need to instantiate `String` and `Integer` simply use directly `wid` and `name` since they are immutable types. – Guillaume Polet Jan 03 '13 at 22:30
  • @GuillaumePolet here if i used as `"
    "+name+"
    " ` ; it compiles the code as `tp_main.setTitleAt(i,"
    name
    ");` It doesn't take it as a Int or String inside a HTML code.
    – Hasi007 Jan 04 '13 at 12:41
  • @Hasi007 I did not say that you have to remove the surrounding quotes and plus (+) sign!, just the wrapping instantiation: `tp_main.setTitleAt(i,"
    "+name+"
    ");`. Btw, for primitive types, it is always better to go through the `valueOf()` methods: `Integer.valueOf()`, `String.valueOf()`, etc...
    – Guillaume Polet Jan 04 '13 at 12:44
0

The only thing I can think of is when the window size changes, adjust the padding of the tabs dynamically. Here is a good starting point.

fudge22it
  • 103
  • 1
  • 2
  • 13
  • Thank you. I tried changing tab sizes. But it fits only for a one window size. Same problem occurs when changing jFrame size. – Hasi007 Jan 03 '13 at 18:06
0

this is worked for me. change your tab sizes went to remove this space

 JLabel lab = new JLabel();
        lab.setPreferredSize(new Dimension(200, 30));
        jTabbedPane1.setTabComponentAt(0, lab);  // tab index, jLabel

or try this change to all tab component in same sizes (called in main method)

UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab.contentMargins", new Insets(10, 100, 0, 0));

Harsha Basnayake
  • 4,565
  • 3
  • 17
  • 23