How To Set A Custom Close Tab Action(10:38, 11. May. 2011)

As you maybe know the JYTabbedPane component from our SyntheticaAddons package supports closeable tabs. However, sometimes it's useful to add some code to e.g. release resources or to deny a close action. In the examples below you can see how to set a custom close action which allows you to do whatever you have to do if a tab will be closed.

//JYTabbedPane Example:
JYTabbedPane tabbedPane = new JYTabbedPane();
tabbedPane.getActionMap().put("closeTab", new Tab.CloseTabAction(){ 
  @Override
  public void actionPerformed(ActionEvent evt){
    AbstractButton closeButton = (AbstractButton)evt.getSource(); 
    Tab tab = (Tab)closeButton.getParent().getParent(); 
    int tabIndex = tab.getTabIndex(); 
    //i.e. do not allow close for first tab 
    if (tabIndex == 0) 
      System.out.println("Veto on closing first tab!"); 
    else 
      super.actionPerformed(evt);
  } 
});

In a JYDocking context the usage is very similar

//JYDocking Example:
JYDockingPort port = new JYDockingPort(){
  @Override
  protected JTabbedPane createTabbedPane(){
    final JYTabbedPane tp = (JYTabbedPane)super.createTabbedPane();
    tp.setCloseButtonStrategy(CloseButtonStrategy.SELECTED_TAB);
    tp.getActionMap().put("closeTab", new Tab.CloseTabAction(){
      @Override
      public void actionPerformed(ActionEvent evt){
        AbstractButton closeButton = (AbstractButton)evt.getSource();
        Tab tab = (Tab)closeButton.getParent().getParent();
        int tabIndex = tab.getTabIndex();
        IDockable dockable = DockingManager.getDockable(tp.getComponentAt(tabIndex));
        DockingManager.close(dockable);
      }
    });
    return tp;
  }
};

Related Posts

Customization - How to apply a Firefox style to a JTabbedPane
How To Create A Flat Button Style
How To Add History To A JYSearchField