Home Tutorials Swt Use of ColorRegistry and FontRegistry
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Use of ColorRegistry and FontRegistry

Advertisement
SWT provides a great application of ColorRegistry and FontRegistry. These are the classes of package org.eclipse.jface.resource.

Use of ColorRegistry and FontRegistry

     

This section illustrates you the use of ColorRegistry and FontRegistry.

SWT provides a great application of ColorRegistry and FontRegistry. These are the classes of package org.eclipse.jface.resource. The class ColorRegistry owns all of the color objects registered with it, and automatically disposes of them by disposing the SWT Display that creates the colors. Similarly the class FontRegistry owns all of the font objects registered with it, and automatically disposes of them by disposing the SWT Display that creates the Font.

We have create a label which shows the changes. A button is created to perform an action by calling the SelectionEvent class. 

 

Following code adds a color to the ColorRegistry and a font to the FontRegistry:

colorRegistry.put(FOREGROUND, new RGB((int) (Math.random() * 255), (int) 
  (Math.random() * 255), (int) (Math.random() * 255)));
FontData data = new FontData("Arial Narrow",(int) (Math.random() * 62), SWT.BOLD);
fontRegistry.put(FONT, new FontData[] { data});

Then we create the method propertyChange() to set the property into label. The method colorRegistry.get(FOREGROUND)  returns the color associated with the given symbolic color name and the method  fontRegistry.get(FONT) returns the font associated with the given symbolic font name.

Here is the code of ShowRegistryExample.java

import org.eclipse.jface.resource.*;
import org.eclipse.jface.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.window.ApplicationWindow;

public class ShowRegistryExample extends ApplicationWindow 
   implements
IPropertyChangeListener {
  private static final String FOREGROUND = "foreground";
  private static final String FONT = "font";
  Label label;
  ColorRegistry colorRegistry;
  FontRegistry fontRegistry;

  public ShowRegistryExample() {
  super(null);
  }
  protected Control createContents(Composite parent) {
  Composite composite = new Composite(parent, SWT.NONE);
  composite.setLayout(new FillLayout(SWT.VERTICAL));

  colorRegistry = new ColorRegistry();
  colorRegistry.addListener(this);

  fontRegistry = new FontRegistry();
  fontRegistry.addListener(this);

  label = new Label(composite, SWT.CENTER);
  label.setText("Hello World");

  Button button = new Button(composite, SWT.PUSH);
  button.setText("Show");
  button.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent e) {
  colorRegistry.put(FOREGROUND, new RGB((int
  (Math.random() * 255), (int) (Math
  .random() * 255), (int) (Math.random() * 255)));
  FontData data = new FontData("Arial Narrow",
 (int) (Math.random() * 62), SWT.BOLD);
  fontRegistry.put(FONT, new FontData[] { data});
  }
  });
  return composite;
  }
  public void propertyChange(PropertyChangeEvent event) {
  if (colorRegistry.hasValueFor(FOREGROUND)){
  label.setForeground(colorRegistry.get(FOREGROUND));
  }
  if (fontRegistry.hasValueFor(FONT)){
  label.setFont(fontRegistry.get(FONT));
  }
  getShell().pack();
  }
  public static void main(String[] args) {
 ShowRegistryExample example= new ShowRegistryExample();
  example.setBlockOnOpen(true);
  example.open();
  Display.getCurrent().dispose();
  }
}

Output will be displayed as:

On clicking the button, output will be displayed as:

 Download Source Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2008

Related Tutorials

Ask Questions?    Discuss: Use of ColorRegistry and FontRegistry  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com