All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.ribbon.Ribbon Maven / Gradle / Ivy

The newest version!
package net.alantea.swing.ribbon;

import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.alantea.swing.panels.FoldablePanel;
import net.alantea.swing.tab.TabbedPane;


@SuppressWarnings("serial")
public class Ribbon extends FoldablePanel
{
   private Map actions = new HashMap<>();
   private List keys = new ArrayList<>();;
   /**
    * Create the composite.
    */
   public Ribbon()
   {
      super(new TabbedPane());
      
      ChangeListener changeListener = new ChangeListener() {
         public void stateChanged(ChangeEvent changeEvent) {
           TabbedPane sourceTabbedPane = (TabbedPane) changeEvent.getSource();
           Component component = sourceTabbedPane.getSelectedComponent();
           Runnable action = actions.get(component);
           if (action != null)
           {
              action.run();
           }
         }
       };
      ((JTabbedPane) getContent()).addChangeListener(changeListener);

   }
   
   public void showTab(String key)
   {
      int num = keys.indexOf(key);
      if ((num >= 0) && (num < keys.size()))
      {
         ((JTabbedPane) getContent()).setSelectedIndex(num);
      }
   }
   
   public Component addTab(String key, String text, Icon icon, Component component)
   {
      return addTab(key, text, icon, component, null);
   }
   
   public Component addTab(String key, String text, Icon icon, Component component, Runnable runnable)
   {
      if ((key != null) && (!key.isEmpty()) && (!keys.contains(key)))
      {
         keys.add(key);
      }
      ((JTabbedPane) getContent()).addTab(text, icon, component);
      actions.put(component, runnable);
      return component;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy