JRibbon - First Program



In this post I’m tell you Step by step how to implements JRibbon in your Application.

Note:  To begging the implement your JRibbon Frame you need to first Design your Ribbon Menu. Then go to implementation because implementation is happened in reverse direction of structure means you need to implements JRibbonBand First then JRibbonBand is Added into RibbonTask and Finally RibbonTask add in to Ribbon.

Step 1:

First of all when your start window based application you extends your class with JFrame. But in this application you need to extends your class with JRibbonFrame.

public  class ClassName extends JRibbonFrame

When you run your application

                                ClassName className = new ClassName();
                className.setSize(300,300);
                className.setVisible(true);

It look like this :



Step 2:

Create JRibbonBand and add some components in to JRibbonBand. JRibbonBand can created using two way.

A :

JRibbonBand clipboardBand = new JRibbonBand("Clipboard",                                            getResizableIconFromResource(new URL(“clipboard.png”)));

In this method creating a JRibbonBand pass two argument first is Name of your band and second is resizable icon for your band. ResizableIcon Details

B :

JRibbonBand clipboardBand = new JRibbonBand("Clipboard",                    getResizableIconFromResource(new URL(“clipboard.png”)), new ActionListener()
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });

In this method creating a JRibbonBand pass three argument first is Name of your band, second is resizable icon for your band and third is ActionListener for ExpandKey (explain below).


Step 3:

Now add ExpandKeyTip for you band.

clipboardBand.setExpandButtonKeyTip("FO");

What is ExpandKeyTip ?
ExpandKeyTip is an action trigger for your Expand Key.


When you press and ALT key JRibbon will display all mnemonics for  all Task, Components, and Expand Buttons.
In this case you press FO, ActionEvent Trigger Fire.


Step 4:

Now create a component for adding into JRibbonBand.
For JRibbon you not use an component like JButton, JCheckbox, JCombox.
JRibbon Provide its own components.
JCommandButton work as JButton
JCommandButton pasteButton = new JCommandButton("Paste", getResizableIconFromResource(new URL(“paste.png”))));

For JCommandButton two argument are passed, first is name of button and second is resizable icons for button

Step 5: 

Additional properties of JCommandButton :
Just like ExpandKeyTip JCommandButton having ActionKeyTip
pasteButton .setActionKeyTip("V");


ActionListener for JCommandButton
pasteButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               
            }
        });

 Step 6:

Setup an RichToolTip for JCommandButton .

Step 7:

Now add JCommandButton to JRibbonBand
clipboardBand.addCommandButton(pasteButton, RibbonElementPriority.TOP);
During  add JCommandButton we need to specify an Priority of RibbonComponents.

RibbonElementPriority.TOP
RibbonElementPriority.MEDIUM
RibbonElementPriority.LOW

Step 8:

Before adding JRibbonBand to RibbonTask you need to set Resizing policy for JRibbonBand

List<RibbonBandResizePolicy> resizePolicies = new ArrayList<RibbonBandResizePolicy>();
resizePolicies.add(
new CoreRibbonResizePolicies.Mirror(clipboardBand.getControlPanel()));
resizePolicies.add(
new CoreRibbonResizePolicies.Mid2Low(clipboardBand.getControlPanel()));
clipboardBand.setResizePolicies(resizePolicies);

Step 9:

Now Create RibbonTask and add JRibbonBand in that RibbonTask.
RibbonTask homeTask = new RibbonTask("Home", clipboardBand);
To add another band in this homeTask just appends new Band in declaration of Task.
e.g.
RibbonTask homeTask = new RibbonTask("Home", clipboardBand, newBand, anohterNewBand);

Step 10:

And finally add Task to Ribbon
this.getRibbon().addTask(homeTask);

Step 11:

When you run it look like


5 comments:

  1. awesome tutorial!

    ReplyDelete
  2. The next code not found:
    package javaapplication2;
    import java.awt.Dimension;
    import java.awt.List;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.Arrays;
    import org.pushingpixels.flamingo.api.common.JCommandButton;
    import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
    import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
    import org.pushingpixels.flamingo.api.ribbon.*;
    import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
    import org.pushingpixels.flamingo.api.ribbon.resize.IconRibbonBandResizePolicy;
    import org.pushingpixels.flamingo.api.ribbon.resize.RibbonBandResizePolicy;

    /**
    *
    * @author Roberto
    */
    public class JFrameRibbonPrueba extends JRibbonFrame {
    private JRibbonBand clipboard;
    private JRibbonBand clipboardBand;
    public void setRibbonBand() throws MalformedURLException{
    clipboardBand = new JRibbonBand("Clipboard",getResizableIconFromResource(new URL("/imagenes/32_bit.png")));
    JCommandButton pasteButton = new JCommandButton("Paste", getResizableIconFromResource(new URL("/imagenes/accordion.png")));
    clipboardBand.addCommandButton(pasteButton, RibbonElementPriority.TOP);

    List resizePolicies = new ArrayList();
    resizePolicies.add(
    new CoreRibbonResizePolicies.Mirror(clipboardBand.getControlPanel()));
    resizePolicies.add(
    new CoreRibbonResizePolicies.Mid2Low(clipboardBand.getControlPanel()));
    //clipboardBand.setResizePolicies(resizePolicies);
    JRibbonBand band1 = new JRibbonBand("Hello", null);
    JRibbonBand band2 = new JRibbonBand("world!", null);
    //band1.setResizePolicies((List) Arrays.asList( new CoreRibbonResizePolicies.None(band1.getControlPanel()), new CoreRibbonResizePolicies.Mirror(band1.getControlPanel()), new CoreRibbonResizePolicies.Mid2Low(band1.getControlPanel()), new CoreRibbonResizePolicies.High2Low(band1.getControlPanel()), new IconRibbonBandResizePolicy(band1.getControlPanel())));
    // band2.setResizePolicies((List) Arrays.asList(new IconRibbonBandResizePolicy(band1.getControlPanel())));
    RibbonTask task1 = new RibbonTask("One", clipboardBand);
    RibbonTask task2 = new RibbonTask("Two", band2);
    }

    public static ResizableIcon getResizableIconFromResource(URL resource) {
    return ImageWrapperResizableIcon.getIcon(resource, new Dimension(32, 32));
    }


    }

    The next error:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot have empty ribbon task
    at org.pushingpixels.flamingo.api.ribbon.RibbonTask.(RibbonTask.java:85)
    at javaapplication2.JavaApplication2$1.run(JavaApplication2.java:37)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

    ReplyDelete
  3. hey robert
    try to remove your band2, or put some buttons in it

    ReplyDelete
    Replies
    1. I remove the bands, try remove the band2, but have problem:
      run:
      Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot have empty ribbon task
      at org.pushingpixels.flamingo.api.ribbon.RibbonTask.(RibbonTask.java:85)
      at javaapplicationribbon.RibbonJFrame$1.run(RibbonJFrame.java:94)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
      at java.awt.EventQueue.access$000(EventQueue.java:84)
      at java.awt.EventQueue$1.run(EventQueue.java:602)
      at java.awt.EventQueue$1.run(EventQueue.java:600)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
      GENERACIÓN CORRECTA (total time: 8 seconds)

      Delete
  4. Hey, does anybody know how to set an icon in the ApplicationMenu button ?

    ReplyDelete