More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Use of ColorRegistry and FontRegistry 
 

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

                         

» View all related tutorials
Related Tags: c eclipse ide class button io swt get ip widgets order vi widget using cli border this id package rad

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.