Clicky

What is different between Display.getDefault() and Display.getCurrent()??

Event though I read the reference , I can not understand the difference..

Please tell me that , and the way of usage.

Thanks in advance .

asked 05/24/2011 05:51

gamjaradio's gravatar image

gamjaradio ♦♦


2 Answers:
Maybe this sounds more undesrtandable:
in the FAQ, rather than the reference:
http://wiki.eclipse.org/FAQ_How_do_I_get_a_Display_instance%3F

FAQ How do I get a Display instance?
From Eclipsepedia
Jump to: navigation, search

Most users deploy Eclipse as one top-level window and manage their code in perspectives, explorers, editors, and views. However, SWT has been designed to optionally control a multitude of displays wherever this is allowed by the local operating system. The implication is that when creating something like a dialog window, SWT needs to be told what display to use for creating the dialog’s frame. Ideally, your application should keep its own references to the display where it is needed, but if for some reason you don’t have an instance, you have two ways to find one. The first way is by calling Display.getCurrent. A display is forever tied to the thread that created it, and a thread can have only one active display; a display is active until it is disposed of.

If you call Display.getCurrent, it returns the display that was created in that thread, if any. Here is an example:

   public static Display getDisplay() {
      Display display = Display.getCurrent();
      //may be null if outside the UI thread
      if (display == null)
         display = Display.getDefault();
      return display;            
   }

A calling thread that does not have an active display will return null. Therefore, this method is useful only when you are absolutely certain that you are in the thread that created the display. This brings us to the second way you can obtain a display instance: Display.getDefault(). It will return the first display that was created. If your application has only one display, this is an acceptable way of obtaining the display.
link
for_yan's gravatar image

for_yan

To add more, the getDisplay would follow the Singleton design pattern ..., so as to not duplicate the component.
link
vks_vicky's gravatar image

vks_vicky

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

Asked: 05/24/2011 05:51

Seen: 566 times

Last updated: 06/30/2011 03:21