Actionlistener (or equiv) on the tab's icon in a JTabbedPane

Imagine for a moment the tab of a JTabbedPane, on it is a nice icon of an "X" in the left(ish) side. When the user clicks this "X" icon, the tab its on closes. If anyone knows how to make this work, and is willing to share the source/example how to do it, it would be much appreciated! Ive been coming back to it over and over for a month.

Try this. Far from perfect, but a start. If you want to switch to SWT (eclipse) I've got a fully functional one you can have.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
public class Rrahl1
     static class Tab
          Component component;
          String name;
          TabButton button;
          public Tab(Component component, String name)
               this.component= component;
               this.name= name;
     static class TabButton
          extends JPanel
          public static int SELECT= 0;
          public static int CLOSE= 1;
          private static CompoundBorder sEtchedBorder= new CompoundBorder(
               new EtchedBorder(EtchedBorder.LOWERED),
               new EmptyBorder(0, 2, 0, 2));
          private static EmptyBorder sEmptyBorder= new EmptyBorder(2,4,2,4);
          private ImageIcon sImageClose= new ImageIcon("images/close.gif");
          private JLabel mLabel;
          private JLabel mCheck;
          private ActionListener mListener;
          private boolean mSelected= false;
          private boolean mMouseIn= false;
          private Dimension mPreferredSize;
          public TabButton(String title, ActionListener listener)
               mListener= listener;
               mCheck= new JLabel(sImageClose);
               mCheck.setBorder(sEtchedBorder);
               mLabel= new JLabel(title);
               mLabel.setBorder(sEmptyBorder);
               setLayout(new BorderLayout(0,2));
               add(mLabel, BorderLayout.CENTER);
               add(mCheck, BorderLayout.EAST);
               setBorder(sEmptyBorder);
               addMouseListener(new MouseAdapter() {
                    public void mousePressed(MouseEvent e) {
                         System.err.println(
                              getComponentAt(e.getPoint().x, e.getPoint().y) == mCheck ? "Check" : "Label");
                         if (getComponentAt(e.getPoint().x, e.getPoint().y) == mCheck)
                              mListener.actionPerformed(new ActionEvent(mCheck, CLOSE, null));
                         else
                              mListener.actionPerformed(new ActionEvent(mCheck, SELECT, null));
                    public void mouseExited(MouseEvent e) {
                         mMouseIn= false;
                         if (!mSelected) {
                              mLabel.setBorder(sEmptyBorder);
                              mCheck.setVisible(false);
                    public void mouseEntered(MouseEvent e) {
                         mMouseIn= true;
                         if (!mSelected) {
                              mLabel.setBorder(sEtchedBorder);
                              mCheck.setVisible(true);
               mPreferredSize= getPreferredSize();
               mCheck.setVisible(false);
          public void setSelected(boolean selected)
               mSelected= selected;
               if (!mMouseIn) {
                    if (mSelected) {
                         mLabel.setBorder(sEtchedBorder);
                         mCheck.setVisible(true);
                    else {
                         mLabel.setBorder(sEmptyBorder);
                         mCheck.setVisible(false);
          public Dimension getPreferredSize() {
               return mPreferredSize == null ? super.getPreferredSize() : mPreferredSize;
     static class TabPanel
          extends JPanel
          private ArrayList mTabs= new ArrayList();
          private Tab mCurrent= null;
          private JPanel mTabPanel= new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
          private JPanel mControlPanel= new JPanel();
          public TabPanel()
               setLayout(new BorderLayout());
               add(mTabPanel, BorderLayout.NORTH);
               add(mControlPanel, BorderLayout.CENTER);
               mControlPanel.setLayout(new OverlayLayout(mControlPanel));
          public void add(final Tab tab)
               mTabs.add(tab);
               TabButton button= new TabButton(tab.name, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         if (e.getID() == TabButton.SELECT)
                              show(tab);
                         else
                              close(tab);
               mTabPanel.add(button);
               mControlPanel.add(tab.component);
               tab.button= button;
               show(tab);
          private void show(Tab tab)
               if (mCurrent == tab)
                    return;
               if (mCurrent != null) {
                    mCurrent.component.setVisible(false);
                    mCurrent.button.setSelected(false);
               tab.component.setVisible(true);
               tab.button.setSelected(true);
               mCurrent= tab;
          private void close(Tab tab)
               mTabPanel.remove(tab.button);
               mTabPanel.setVisible(false);
               mTabPanel.setVisible(true);
               mControlPanel.remove(tab.component);
               int index= mTabs.indexOf(tab);
               Tab removed= (Tab) mTabs.remove(index);
               if (removed == mCurrent) {
                    if (index < mTabs.size())
                         show((Tab)mTabs.get(index));
                    else if (mTabs.size() > 0)
                         show((Tab)mTabs.get(index-1));
               validate();
     static int newTab= 0;
     public static void main(String[] argv)
          JPanel blue= new JPanel();
          blue.setBackground(Color.BLUE);
          JPanel red= new JPanel();
          red.setBackground(Color.RED);
          JPanel green= new JPanel();
          green.setBackground(Color.GREEN);
          green.setPreferredSize(new Dimension(320,240));
          final TabPanel tabPanel= new TabPanel();
          tabPanel.add(new Tab(red, "Red"));
          tabPanel.add(new Tab(green, "Green"));
          tabPanel.add(new Tab(blue, "Blue"));
          JButton btn= new JButton("Add tab...");
          btn.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                    tabPanel.add(new Tab(
                         new JLabel(new java.util.Date().toString()),
                         "Tab " +(newTab++)));
          JFrame frame= new JFrame("Tab");
          frame.getContentPane().add(tabPanel, BorderLayout.CENTER);
          frame.getContentPane().add(btn, BorderLayout.SOUTH);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
}

Similar Messages

  • The tabs and icons for org. tabs is stretched vertically, the icons are triple row! Also no x in the right top corner.

    When looking at the screen, the tabs are 3 times higher than any of the other browser areas (address bar), favorite locations etc. The icons to the right for organizing tabs are repeated vertically 3 times! Additionally menu issues. I have no menu, but just a tab marked "Firefox" in the upper left. If I take the menu off, I lose my bookmarks as well! I tried to restart in safe mode using:
    firefox -safe-mode
    But that did not work. Also using the "Alt" key did NOT show my menu even for a second.

    I'm not sure what you mean by "connection scale". If you are referring to the padlock, this has been replaced by the site identity button, for details on using it see https://support.mozilla.com/kb/Site+Identity+Button
    The Home button by default has been moved to the right hand side of the navigation toolbar, you can move it back. For details of how to do that see https://support.mozilla.com/kb/how-do-i-customize-toolbars
    If any buttons are missing, you can restore them as shown here - https://support.mozilla.com/kb/Back+and+forward+or+other+toolbar+items+are+missing

  • In FF 7.0.1, the New Tab button on the Tab Strip disappeared after clicking the Tab Group icon and trying to return, so I'm looking for advice on how to get the New Tab to reappear on the Tab Strip.

    In Windows Vista, FF 7.0.1, I selected the Tab Group button to try it out. However, when I selected the Tab Group button again to close that view, the New Tab button in the Tab Strip no longer appears. I'm requesting help to restore the New Tab button (has the "+" sign on it) on the Tab Strip just to the right of the currently open tabs.

    You can find the New Tab button showing as a '+' on the Tab bar.
    You can open the Customize window and drag the New Tab button that shows as a plus sign (+) from the Tab bar on another toolbar and it will become a regular toolbar button like the New Tab button that you have in Firefox 3 versions.<br />
    Open the Customize window via "View > Toolbars > Customize" or via "Firefox > Options > Toolbar Layout"
    If you can't find the New Tab button then click the "Restore Default Set" button in the Customize window.
    If you want the New Tab button at the far right end of the Tab bar then place a flexible space to the left of it.

  • How do you adjust the font size for the tabs and icons around the screen?

    How do  you adjust the font size of tabs and and icons to ake them larger?

    sfcalling
    Click on the text in the menu so that you see the bounding box around it. Look to the right of the Movie Menu customization workspace, Adjustments Palette, Text and edit your text font style and size.
    In this screenshot, "Glass Flowers" text in the main menu is selected and its Adjustments Palette Text editing opportunities are shown. To do comparable editing in the scene menu, click on the scene menu in the bin to bring its image up to the screen.
    Please let us know if you are OK with the details.
    Please also review the following Adobe document on disc menus which applies to both version 11 and 12.
    Adobe Premiere Elements 11 * Creating disc menus
    By the way, what computer operating system is your Premiere Elements 12 running on?
    Thanks.
    ATR

  • No tab group icon on my address toolbar

    Win7, Aero, FF4.0. When I create a tab group, using keyboard shortcut, am I thereafter supposed to see the tab group icon on the address or bookmark toolbar?
    I don't.. I did put it there manually earlier, but once I restored Default Set in Toolbar Layout, it hasn't come back.
    Is 4.0 still in Beta? Or is one of my add-ons screwing this up?

    Try:
    * http://kb.mozillazine.org/Corrupt_localstore.rdf

  • The + to add a tab is mising and I have no access to the tab grouping. I just downloaded Firefox to a new Win 7 computer.

    I am using a brand new Win 7 computer. I downloaded Firefox expecting it would be just the same as my previous version-8.?
    I have no + on the tab bar to add a new tab.
    Additionally, I loved the tabbed grouping and used it a lot. The set of boxes in the upper right corner to access the grouping page isn't even there.
    To make sure I was getting Firefox without problems, I downloaded it from Firefox. The first time I downloaded it with Google Chrome. I am familiar with the games MS Wins plays with Mozilla.
    Can you help me?

    Hi jb4long,
    You should try starting Firefox in Safe Mode by holding down shift while it is starting. Then you should choose to disable all the add-ons and use the default theme. That should put things back in order.
    You might also want to take a look at [https://support.mozilla.org/en-US/kb/how-do-i-customize-toolbars this article on customizing the toolbar]. You can put the Tab Groups icon in your toolbar or you can access it by hitting ''CTRL + SHIFT + E''
    Hopefully this helps!

  • How does Apple get their tab bar icons so sharp?

    Looking at the iTunes app on my iP4, the tab bar icon's are extremely sharp.  Anyone know how those might be done?
    Most doc's on the subject say that we should use (approximately) 30x30 pixel's at about 72 dpi.  I do that and my icon's end up looking very jagged.
    There must be more to this to get the razor sharp icon's Apple produces.
    I have used Photoshop and am currently using Inkscape, but both produce similar jagged results.
    Any comments or suggestions will be greatly appreciated.
    Thanks.

    Thanks for the suggestion.
    I found a tut on using Omnigraffle ($99) to create sharp tab bar icons but thankfully I did not take the plunge because the results are no better that what I am currently gettting using Inkscape.  Photoshop is not any better, but in truth, I have not even looked into anti-aliasing in that app.
    I've never heard of Fireworks, but I'll take a look at it.
    These are my results using Inkscape, but I think they could be much better.  Grab is from a 320x460 png.
    Thanks for your comments.  I appreicate the help.

  • My bookmark icons can be fully seen in bookmark toolbar. But on the address bar and on tab most of the icons are blank and when I open most of the websites, their icons are also blank, too. I've already reinstall the firefox 3.6.3 but it is the same. Plea

    My bookmark icons can be fully seen in bookmark toolbar. But on the address bar and on tab most of the icons are blank and when I open most of the websites, their icons are also blank, too. I've already reinstall the firefox 3.6.3 but it is the same. Please help me. Some of the websites show thier icon normally.
    == User Agent ==
    Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3

    Yes, it doesn't work. I ran in safe mode. nothing changed. I uninstalled Mozilla firefox with only standard removal, no change. I finally uninstalled Mozilla with complete removal and reinstalled it, then it return to normal. Why does this happen? Thanks for your reply.

  • Since installing 4.0.1 I can't right click on the Icon in my task bar and have Firefox give me the option of "close and save" the tabs. Only let's me know I'm about to close multiple tabs. How do I get "save and close" back?

    Before installing 4.0.1 I could right click on the Firefox icon on my computer's taskbar and hit close window. It would then give me the option to "save tabs and quit" or "close all tabs."
    Now it just alerts me that I'll be closing multiple tabs.
    It was convenient to be able to close Firefox and open it with all of the tabs in place. There is nothing more irritating than having multiple tabs lost because your computer did an update overnight and restarted. Also it was helpful when you wanted to run a tun-up or cleaner that asked you close out of your browser for optimum result's.
    How do I get "save and quit" back?
    Thanks

    Firefox now always stores the old session, and you can access it by going to the History menu and selecting "Restore Previous Session"
    If you want Firefox to display the message to save the session, it can be turned back on by changing some preferences.
    # Type '''about:config''' into the location bar and press enter
    # Accept the warning message that appears, you will be taken to a list of preferences
    # Locate the preference '''browser.tabs.warnOnClose''', if its value is set to '''false''', double-click on it to change its value to '''true'''
    # Repeat this for these 3 preferences '''browser.warnOnQuit''', '''browser.warnOnRestart''' and '''browser.showQuitWarning'''
    If you always open the last set of tabs, an alternative approach is this:
    # Click the orange Firefox button, then select options to open the options window
    # Go to the General panel
    # Change the setting "When Firefox starts" to "Show my windows and tabs from last time"

  • Is there a way to open a new tab/window by clicking the Safari dock icon?

    Is there a way to open a new tab or window by clicking the Safari dock icon? Right now, the default behavior seems to be that Safari simply un-minimizes or displays the last window in focus.
    I want to be able to quickly fire up a new tab or window in Safari when it is out of focus, such as when Finder is in focus. The workaround right now is to right-click on the Safari dock icon, and select "New Window", or to allow the last window in focus to appear, and cmd+t or cmd+n. But this is too many steps over the course of a day for a highly frequented task like this.

    I'm glad that helped. Thanks for posting back.
    ... when I save this as an application as "run only", it's relatively quick to run, but there's a delay to load... I assume it's opening Script Editor every time?
    As long as the script is saved as an application -- Run Only or not -- it should definitely not cause the Script Editor to open. You can verify this by closing the Script Editor application altogether prior to clicking on the docked script icon that you saved. You should notice that the Script Editor does not open when the script is run.
    FWIW, I went back and saved a version of the script as a Run Only app, and I didn't notice any difference between it and the original, which was saved without the Run Only option checked. The delay that you're noticing might be accounted for by the fact that the script, because it was saved as an application, will be activated just as any other standalone application would be. This happens prior to Safari being activated. Watch your menu bar when clicking on the docked script icon and you should see the name of the saved script app appear for a split second before Safari gets activated.
    The only time I can think of that you might wish to save the script as a Run Only application would be if you wanted to conceal the script for some reason. If, for example you were publishing the script as a part of an application release and didn't want its contents known, or you didn't want an end user to be able to edit the script, you might select the Save As > Run Only option. To illustrate this, try dragging and dropping a script saved as a Run Only application onto the Script Editor's icon. You should see a warning dialog to this effect:
    *Unable to read the file because the script is not editable (it was saved as run-only).*
    Whereas if you drag a script that was not saved as a Run Only app onto the Script Editor's icon, the script should open in the Editor, and it can be edited.
    I played around with the "stay open" option for saving the script as an application, but it appears I have to close the app and reopen it before I can get it to work a second time.
    The Stay Open option is appropriate for scripts which use an "On Idle" handler, which really wouldn't apply in this case. Check out more on Idle Handlers here and here.
    Is there a faster way to use this multiple times throughout the day?
    One way you might speed up the action would be to save the script +as a script+ by choosing Save As > File Format: script from the Script Editor's File menu. The resulting script can then be activated by assigning a hot key to it using one of several 3rd-party keyboard shortcut programs available on the Internet. (I used Spark, assigned a function key to the script I posted earlier, and found the action to run significantly faster.)
    Good luck.

  • All of a sudden I can no longer open a new tab using either the icon on the tab bar, the keyboard short cut or the File menu option. I can open a new tab ONLY by clicking on a bookmark or link.

    All of a sudden I can no longer open a new tab using either the icon on the tab bar, the keyboard short cut or the File menu option. I can open a new tab ONLY by clicking on a bookmark or link

    This issue can be caused by the Ask<i></i>.com toolbar (Tools > Add-ons > Extensions)
    Your system details show that toolbar in the user agent as AskTbAD2/3.12.2.16749
    * https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes

  • When I read a pdf document using Safari I'm unable to read more than one page of a file. A padlock icon is locked on the tab. Please help.

    When I read a pdf document using Safari on my IPad  I'm unable to read more than one page of a file. A padlock icon is locked on the tab. Please help.

    Hi snoreaustralia,
    It sounds like you just need to associate the PDF filename extension with Acrobat, rather than Preview. Please see this document from the Microsoft Help for instructions on choosing the default program for opening a particular file type (in your case, PDF): Change which programs Windows uses by default - Windows Help
    Best,
    Sara

  • I have lost the tabs on the top where bookmarks are, and all my saved tabs. The screen is very big and the icons on the bottom are gone as well.

    I noticed the tab on top where it says file edit bookmarks etc was missing. There was a Firefox with a down facing arrow and I clicked on it to see if I could find my bookmark tabs. I don't know what I pushed but the 2 other saved tabs under the first one went away. also the icons on the bottom of the screen also went away. I am very computer illiterate and don't know what I did or if I can even get it back.

    Make sure that you do not run Firefox in full screen mode (press F11 or Fn + F11 to toggle; Mac: command+Shift+F).<br />
    If you are in full screen mode then hover the mouse to the top to make the Navigation Toolbar and Tab bar appear.<br />
    You can click the Maximize button at the top right to leave full screen mode or right click empty space on a toolbar and use "Exit Full Screen Mode" or press F11.<br />

  • How do I get a Vista 64 bit XP machine to show a "+" icon on the tab bar to "Open a new tab" and the "Open a new tab" icon on the far right of the Bookmarks tool bar?

    I have Firefox 4.0.1 installed on two laptops, one with Windows Vista, 64 bit and the other with Windows XP. Both machines have the "Open a new tab" icon on the far right of the Bookmarks tool bar. The XP machine also has a + icon on the tab bar to "Open a new tab" while the Vista machine does not have that + icon. I like using that; is there any way to get it on the Vista machine?

    After weeks of trying, I stumbled on the solution:
    # Right-click on an empty section of the Tab Strip, select Customize... and the Customize Toolbar window will open.
    # At the bottom of the Customize Toolbar window, there are additional options for changing the appearance of the icons in your toolbars. From the Show drop-down menu, you can choose what to display in the toolbars — Icons, Text, or Icons and Text together. By default, Firefox just shows Icons.
    # Select Icons and Text
    # When you are finished making changes, click Done to close the Customize Toolbar window.

  • I have Windows Professional and I used to be able to move the curser over the Firefox icon in the taskbar and it would give me a telescoped view of all open tabs, now it just shows the tab that I'm currently viewing. How do I fix this?

    I have Windows 7 Professional and I used to be able to move the curser over the Firefox Icon in the taskbar and it would show me a telescoped view of all open tabs, now it just shows the tab that I'm currently viewing. How do I fix this?

    Hey MikeDo,
    Do you have the option checked for ''Show Tab Previews in the Windows Taskbar''? If not, just go to ''tools->options->tabs.'' You can find more information on what each of these settings do in the Knowledge Base article [[Options window - Tabs panel]].
    Hopefully this helps!

Maybe you are looking for