Java Swing - proper tabbed windows

Discussion in 'Mac Programming' started by AlmostThere, Apr 17, 2007.

  1. AlmostThere macrumors 6502a

    Does anyone know what Swing class I need to use to create tabs like these, or, for example, like those in Safari although not a Java app (some of these screens are from Windows, but as I have used both these apps, the behaviour is the same): where the "FormsTutorial" is on a tab. where "FormSpecPanel", "GridBagLayoutUI" and "TableLayoutUI" are tabs

    More examples: where "sa@SQL Server ... " is on a closeable tab
    and showing two sets of tabs.

    These are closeable in the actual app and are obviously different from the vanilla JTabbedPane.

    I know there is a Java 6 tutorial explaining this on the Sun website but I am running (and will be for the foreseeable future) Java 5.

    Also, I vaguely remember some tool that allowed to identify the class of a Swing component from a running app - but maybe I am mistaken. Does this ring any bells with anyone?

  2. therevolution macrumors 6502

    May 12, 2003
    Those apps look like they were built on the Eclipse Rich Client Platform. If you want your app to have a similar layout, you should check it out.

    Otherwise, look at using JTabbedPane with a different Look and Feel package. I'm not sure which one would give you the exact same look, so you'll have to search. You could subclass JTabbedPane to add the close-button-on-tab functionality.
  3. Sir Al macrumors member

    Sir Al

    Feb 14, 2005
    Vancouver, Canada
    Also, it looks like these are using SWT, not Swing.
  4. AlmostThere thread starter macrumors 6502a

    As far as I can see, neither of these apps come bundled with the swt jar file or jnilibs, hence I guessed they were swing.

    SWT, which I have been using, is not really an alternative - I need to use some 3rd party Swing components and have had no end of problems with the SWT_AWT bridge on OS X (and yes, I have all the updates that supposedly fixed these issues).
  5. ryan macrumors 6502

    May 17, 2002
    Denver, CO
  6. abcfob macrumors newbie

    Apr 10, 2007
    Have you tried a JTabbedPane?

    all you would have to do is add your swing components using the addTab(String,Component) method.

    Ah, I see you're already familiar with that, if you want to add the "X" to close the tab, you would have to add mouse listeners to the tab to track the mouse movement, and add custom code to display the "x". It would be easier to just include a JButton on the side which would close the currently selected tab in the JTabbedPane.

    If you want to know what class your component is, you can use instanceof such as

    if(pComponent instanceof JLabel){
    //do something


    Typing "closeable swing tabs" in google yields links with examples of people doing what you want
    here's one for example:
  7. AlmostThere thread starter macrumors 6502a

    I saw that thread and will have a look at the JGoodies stuff as well. I will have a play with these (although I might have a hard time pushing for the JGoodies licences on what is essentially an R&D project).

    I was just wondering if there was a standard widget that gave the behaviour I am looking for, especially having seen similar behaviour in other apps. It has been a few years since I did much swing and know it has come on quite a bit in that time.

    Thanks for all the input.

Share This Page