0

Original Top tab orientation setting:

http://dl.dropbox.com/u/3238736/screenshots/Screenshot-PasswordStore-1.png

Problematic Right tab orientation setting:

enter image description here

From the GUI above, my JTabbedPane (the blue colour tab on the right) is overlapping the "Quit" button (which is rendered using a GlassPane).

Note: Quit button is rendered onto top right using GlassPane.

I would like some technical advise on moving the blue colour tab to give some spacing for the "Quit" button ?

Codes for creating the GlassPane to insert the Quit button as shown below:

public void addUniversalQuitBtn() {
    // Thanks to http://www.java-forums.org/awt-swing/12267-how-add-jbutton-tabbed-pane-headder.html forum post regarding adding a button on glasspane.
    Rectangle tabBounds = mainTabPane.getBoundsAt(0);
    Container glassPane = (Container) this.getRootPane().getGlassPane();
    glassPane.setVisible(true);
    glassPane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.insets = new Insets(tabBounds.y, 0, 0, 10);
    gbc.anchor = GridBagConstraints.NORTHEAST;
    quitBtn.setPreferredSize(new Dimension(quitBtn.getPreferredSize().width, (int) tabBounds.getHeight() - 2));
    glassPane.add(quitBtn, gbc);

}

Thanks.

thotheolh
  • 7,040
  • 7
  • 33
  • 49
  • 2
    why are you using a glass pane if you don't want things to overlap? let the layoutmanagers do these things for you. – keuleJ Apr 19 '12 at 05:15
  • When the tabs are set to orientate on top, the Quit button would align nicely next to the tabs but when the tabs are orientated to the right, it would overlap. I would like to have the Quit button on the top right. The close button for the JFrame is made to iconify the window to the system tray so a Quit button should be added for shuttng down the app. – thotheolh Apr 19 '12 at 05:23
  • Which part of that explains use of the 'glass pane'? – Andrew Thompson Apr 19 '12 at 05:34
  • @thotheolh same way as the JButton is placed into Right_Top_Corner is possible to move with that anywhere inside the Container – mKorbel Apr 19 '12 at 06:41
  • something wrong with your button's position calculation that you are not showing ... – kleopatra Apr 19 '12 at 09:12
  • @kleopatra, sorry I don't get your meaning. – thotheolh Apr 19 '12 at 09:14
  • it boils down to: show an SSCCE :-) – kleopatra Apr 19 '12 at 09:16
  • I have updated with sample codes to add the GlassPane Quit button. Thanks. – thotheolh Apr 19 '12 at 09:17
  • Your fragment is _not_ an [sscce](http://sscce.org/), which would seem easier than (laboriously) blurred content. Is that a `JToolBar`? – trashgod Apr 19 '12 at 10:16

2 Answers2

1

Well, i'd offer you to move the quit button from glasspane to some proper container but with standart Swing JTabbedPane you can't put it that way...

So here is some sort of solution:

public static void main ( String[] args )
{
    JFrame frame = new JFrame ();

    Insets current = UIManager.getInsets ( "TabbedPane.tabAreaInsets" );
    UIManager.put ( "TabbedPane.tabAreaInsets",
            new Insets ( current.top, 40, current.bottom, current.right ) );

    JTabbedPane tabbedPane = new JTabbedPane ();
    tabbedPane.setTabPlacement ( JTabbedPane.RIGHT );
    tabbedPane.addTab ( "Tab 1", new JLabel () );
    tabbedPane.addTab ( "Tab 2", new JLabel () );
    frame.add ( tabbedPane );

    UIManager.put ( "TabbedPane.tabAreaInsets", current );

    JTabbedPane tabbedPane2 = new JTabbedPane ();
    tabbedPane2.setTabPlacement ( JTabbedPane.RIGHT );
    tabbedPane2.addTab ( "Tab 3", new JLabel () );
    tabbedPane2.addTab ( "Tab 4", new JLabel () );
    frame.add ( tabbedPane2, BorderLayout.SOUTH );

    frame.setSize ( 400, 400 );
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

First tabbed pane (top one) has 30px gap between top side and tabs. The second tabbed pane has default gap set.

By changing the insets under "TabbedPane.tabAreaInsets" key you can manipulate the spacings between tabs run and tabbed pane sides. Be aware that those insets are rotated when tab position differs from TOP. So if you want to modify the top spacing with RIGHT tabs position you should modify the left one but not the top one, like i did in my example.

And don't forget to put the old Insets value back, otherwise that change will affect ALL tabbed panes created after the change.

Also i cannot guarantee that this will work for all of the native UIs, but i think such basic features should be supported. Atleast it is supported in Windows, Mac OS and Metal LaFs.

One more thing - you won't be able to change tab area Insets in runtime for already created tabbed pane, since it is saved into specific system UI when it is created and it is not possible to update that value (atleast without using Reflection "features" and violently accesing private fields). So you will have to recreate the tabbed pane if you want that gap only in one kind of tab placement.

Mikle Garin
  • 10,083
  • 37
  • 59
0

ok. sounds a bit strange to me... but you could have 2 Buttons: one in the GlassPane (visible if TabbedPane is oriented top) and one in the Bar at the top (visible if TabbedPane is oriented right)

keuleJ
  • 3,418
  • 4
  • 30
  • 51