Display class in java

Using Display Class : Display « SWT « Java Tutorial

The primary functions of Display instances are as follows:

  1. The Display object is the connection between the SWT and the underlying windowing system.
  2. Implementing the SWT event loop in terms of a platform event model
  3. Providing methods for accessing information about the operating system
  4. Managing operating system resources used by SWT
  5. Only a single Display instance is required in most SWT-based applications.
  6. The thread that creates the Display object is the event-loop thread (user-interface thread).

17.3.Display
17.3.1. Using Display Class
17.3.2. Get the bounds and client area of a display
17.3.3. Causes the system hardware to emit a short sound if it supports this capability
17.3.4. Returns the button dismissal alignment
17.3.5. Get the longest duration in milliseconds between two mouse button clicks that will be deemed a double-click.
17.3.6. Returns the maximum allowed depth of icons on the display.
17.3.7. To find the primary monitor, call getPrimaryMonitor.
17.3.8. Get color for the given constant
17.3.9. SWT Event Handling with Displays

java2s.com | © Demo Source and Support. All rights reserved.

Источник

Display class in java

The Display class provides an application with access to the device’s user interface hardware resources. It includes static methods for obtaining Display objects as well as instance methods to retrieve their properties and display text on them.

Display Resources

A device MAY have one or more display resources for interacting with the user. Each resource includes a line-oriented display device and may also include keys or other appropriate means for user input. Those means MUST create key events, that can be processed by a Display via an associated KeyListener , as defined in the javax.microedition.key package. A Display that supports key events, MUST implement the InputDevice interface as defined in the javax.microedition.key package. This implies of course that this optional package would have to be implemented by the MEEP 8 implementation. An application gains access to a display resource using a Display object. Each Display object represents a specific application’s use of a specific resource, rather than the resource itself. Hence, for a given display resource, each application has its own dedicated Display object for accessing it.

Display States

Multiple applications may want to use the same display resource simultaneously. A line-oriented display resource is exclusive in nature; that is, it can be made available to only one application at a time. For example, regular key events are typically provided to a single application. The nature of specific resources and the policies for sharing them between applications are largely platform dependent. However, in principle, the following should be true. A Display may be assigned to the corresponding display resource. The resource is available to this Display then. For a given display resource, there can be no more than one assigned Display . If the resource is also used by the native UI of the device, there may be times when no Display is assigned. A Display may be unassigned to the corresponding display resource. Then all of its display resources are relinquished and unavailable to the application. For a given set of display resources, there can be any number of Display s that are in the unassigned state. A Display is initially in this state and it cannot be changed unless text is shown on it. The application may request that a Display becomes assigned or unassigned by calling the Display ‘s setHardwareAssigned method. However, these requests to change the state are not guaranteed and are subject to the device’s application display sharing management behavior.

Obtaining Display Objects

For an application, there may be a primary Display object. The primary Display corresponds to the device’s main (built-in) display, if any, that is normally used to access its features. Note that devices may have only auxiliary displays. As auxiliary displays are only temporarily attached to the device, in this case there is not always a display available. Of course it makes nmo sense to mention a primary display in this case. The getDisplays(boolean) method returns a list of Display objects corresponding to a given application. The primary Display object, if any and if being part of the result list, is always returned as the first element in the list, followed by any secondary Display objects that are also available.

Display Capabilities

A Display may or may not be able to process key events (depending on whether the associated hardware is coupled with a user input facility like a keyboard). Note that displays supporting key events require the implementation of the optional javax.microedition.key package (see optionality chapter for details), and that those displays MUST implement the InputDevice interface. Besides a line-oriented display may support several text colors, or just a single color. Display can also support background color with or without the ability of change as well as lighting with a fixed color or the capability to change lighting color as well. The Display class also defines name and values for events that can be used to handle lighting of displays (if supported by a paticular Display) using the Event class. A display may be also capable to scroll texts vertically or horizontally or both. If a display is capable to scroll text horizontally and/or vertically, the scrolling mode can be switched on and off, independend for both scrolling directions (if both are supported). If a display provides capabilities for both, horizontal and vertical scrolling, it is strongly recommended to use only one of those possibilities at a time for the sake of readability of the display.

Field Summary

Источник

Object getClass() Method in Java

Object getClass() Method in Java | We can use getClass() method of java.lang.Object class to get the runtime class definition of a runtime class object. Prototype of getClass() method of java.lang.Object class is:-

public final native Class getClass()

Return value:- The Class object that represents the runtime class of this object.

  • Runtime class: The class that is loaded into JVM at execution time is called the runtime class.
  • Runtime class object: Every class bytecode is stored using java.lang.Class object. This java.lang.Class object is called the runtime class object.

After loading every .class file, JVM creates one object of java.lang.Class type in the heap area. We can use this object to get class-level information like class name, method information, constructor information, and e.t.c. The getClass() method of the Object class is used very frequently in the reflection API.

Assume there is a class called “A”. Using the getClass() method we can get the runtime class object of the “A” class.

A a1 = new A(); Class cls = a1.getClass();

Java Object getClass() Method Example on Pre-defined classes,

import java.util.ArrayList; public class Test < public static void main(String[] args) < String str = new String(); System.out.println("Class of str: " + str.getClass()); ArrayList al = new ArrayList(); System.out.println("Class of al: " + al.getClass()); >>

Class of str: class java.lang.String
Class of al: class java.util.ArrayList

Java Object getClass() Method Example on Custom classes,

class A < >public class Test < public static void main(String[] args) < Test t1 = new Test(); System.out.println("Class of t1: " + t1.getClass()); A a1 = new A(); System.out.println("Class of a1: " + a1.getClass()); >>

Class of t1: class Test
Class of a1: class A

Get Class Name in Java

To get the name of a Java class, call the getName() method on the runtime class object. This getName() method is defined in java.lang.Class class. Its prototype is:-

public String getName()

Assume there is a class called “A”. Below code shows retrieving the class name of runtime class object using the getClass() method in Java:-

// create object of class A a1 = new A(); // get runtime object Class cls = a1.getClass(); // get class name String name = cls.getName();

The above lines of code can be written in a single line as follows:-

getclass().getname() Method in Java,

String name = a1.getClass().getName();

Java program to get the class name

Using getClass() Method in Java to get Class Information

  • getFields()
  • getConstructors()
  • getMethods()

On calling the above methods on the runtime class object, they will give information of the current class, and all its superclass.

Java program to display all methods of a class using getClass() method of Object class,

import java.lang.reflect.*; class Test < // methods public void m1()<>public void m2()<> public void m3()<> public static void main(String[] args) < Test t1 = new Test(); Class cls = t1.getClass(); // display class name of runtime class object String classname = cls.getName(); System.out.println("Class Name: "+ classname); // display all methods of this Class object Method[] methods = cls.getMethods(); System.out.println("Methods: "); for(Method m : methods) < System.out.print(m.getName() + " "); >> >

Class Name: Test
Methods:
m3 main m2 m1 wait wait wait equals toString hashCode getClass notify notifyAll

Test class in a direct child class of java.lang.Object class therefore methods of Objects class are also available to the child class.

Another simple way to display all methods of a class is by using javap tool. The javap is also a tool like ‘javac’ and ‘java’. The ‘javac’ and ‘java’ tools are used to compile and run the Java program. Open cmd and type “javap ”.

> javap Test

Compiled from “Test.java”
class Test Test();
public void m1();
public void m2();
p ublic void m3();
public static void main(java.lang.String[]);
>

Different Ways to Get Runtime Class Object in Java

In Java, the getClass() method is not the only way to get the runtime class object. Different ways to get the object of a given class are,

1) Class cls = classname.class;
2) Class cls = Class.forName(classname);
3) Class cls = obj.getClass();

Let us take a Test class for demonstration purposes,

public class Test < public static int i = m1(); static < System.out.println("Test class: Static block executed"); >public static void main(String[] args) < System.out.println("Test class: main method executed"); >public static int m1() < System.out.println("Test class: Static variable initialized"); return 5; >>

1) Using classname.class to get the runtime class object,

// classname.class demonstration public class Example1 < public static void main(String[] args) < Classt1 = Test.class; System.out.println("Name of class: " + t1.getName()); > >

In the first approach, using Test.class => Test class is loaded & stored in the JVM, and the runtime class object is returned.

In this approach, the Test class static variables are not initialized, static block and the main method of the Test class are also not executed.

2) Using Class.forName() to get the runtime class object,

 // Class.forName() demonstration public class Example2 < public static void main(String[] args) throws ClassNotFoundException < Classt1 = Class.forName("Test"); System.out.println("Name of class: " + t1.getName()); > >

Test class: Static variable initialized
Test class: Static block executed
Name of class: Test

In this approach, Class.forName(«Test»); => The Test class is loaded, java.lang.Class object is created, and static variables of the Test class are initialized. And static blocks of the Test class are also executed. Finally, the runtime class object is returned. The main method will not be executed.

3) Using getClass() to get the runtime class object,

// getClass() demonstration public class Example3 < public static void main(String[] args)< // creating object of class System.out.println("Creating an object of Test class"); Test t1 = new Test(); System.out.println("Test class object is created"); // get class reference Class cls = t1.getClass(); System.out.println("getClass() is called"); // get name of class System.out.println("Name of class: " + cls.getName()); > >

Creating an object of Test class
Test class: Static variable initialized
Test class: Static block executed
Test class object is created
getClass() is called
Name of class: Test

In this approach, the already created Class object reference is returned because the Test class has already loaded due to its object creation.

Whenever the constructor is called for the first time (assuming the class was not used before that statement) then the class is loaded, static variables are initialized, and static blocks are executed. But getClass() is not performing these operations.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

Читайте также:  Php array helper functions
Оцените статью