Friday, 2 December 2016

SWT: Link widget


By using Link widget, you can display text with hyper link.

Following constructor is used to define Link instance.
Link(Composite parent, int style)

import java.awt.Desktop;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;

public class TestLink {

 public static void main(String[] args) {
  Display display = new Display();
  Shell shell = new Shell(display);

  Link link = new Link(shell, SWT.NONE);
  String message = "This is a link to <a href=\"https://self-learning-java-tutorial.blogspot.com/\">Java Tutorial</a>";
  link.setText(message);
  link.setSize(400, 100);
  link.addSelectionListener(new SelectionAdapter() {
   @Override
   public void widgetSelected(SelectionEvent e) {
    System.out.println("You have selected: " + e.text);
    try {
     Desktop.getDesktop().browse(java.net.URI.create(e.text));
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
   }
  });
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  display.dispose();
 }
}



Run above application, you can able to see following window.



Previous                                                 Next                                                 Home

Thursday, 1 December 2016

SWT: Group widget

Group widget is used to group SWT widgets. A group is surrounded by a border and may, optionally, contain a title. The position of widgets inside the group are relative to the group.

How to add group to the screen?
Group group1 = new Group(shell, SWT.BORDER);

How to place widgets inside a group?
The way how you place the widgets inside a shell, you can place other widgets inside the group.

Button button = new Button(group1, SWT.PUSH);
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static int xPosition = 30;
 private static int yPosition = 50;

 private static int shellWidth = 600;
 private static int shellHeight = 600;

 private static int groupWidth = 250;
 private static int groupHeight = 250;

 private static String[] countries = { "India", "Germany", "Canada", "Japan", "Sri Lanka", "Singpore", "Russia" };
 private static String[] hobbies = { "Trekking", "Movies", "Games", "Painting", "Singing", "Dancing" };

 private static void addWidgetsToShell(Display display, Shell shell) {

  Group group = new Group(shell, SWT.BORDER);
  group.setBounds(10, 10, groupWidth, groupHeight);
  group.setText("Personal Information");

  Combo countryCombo = new Combo(group, SWT.DROP_DOWN);
  countryCombo.setItems(countries);
  countryCombo.setBounds(xPosition, yPosition, 150, 100);
  countryCombo.setText("Select Country");

  yPosition += 50;

  Combo hobbyCombo = new Combo(group, SWT.DROP_DOWN);
  hobbyCombo.setItems(hobbies);
  hobbyCombo.setBounds(xPosition, yPosition, 150, 100);
  hobbyCombo.setText("Select hobbies");

  yPosition += 50;

  Button button1 = new Button(group, SWT.PUSH);
  button1.setText("Submit");
  button1.setLocation(xPosition + 100, yPosition);
  button1.setSize(100, 20);

 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(shellWidth, shellHeight);

  addWidgetsToShell(display, shell);

  /* Open shell window */
  shell.open();

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  /* Dispose the display */
  display.dispose();
 }
}





Previous                                                 Next                                                 Home

Why to use Boolean.valueOf than Boolean constructor?

First let’s see the implementation of Boolean.valueOf method.

    public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

As you see the above source code, valueOf method takes one argument ‘b’ and return TRUE, if the argument ‘b’ is true, else FALSE. TRUE, FALSE are the constants defined like below.

    public static final Boolean TRUE = new Boolean(true);
    public static final Boolean FALSE = new Boolean(false);

The variables TRUE, FALSE are defined as static final variable, that is these are immutable variables. One greatest advantage of immutable variables is that, they can’t change their state, so we can reuse these variable at any point of time. Following posts explain about the immutable classes.


In case of constructor, constructor creates new object on every call. If you are going to use same variable many times, then go for valueOf method than constructor. It improves the performance and save space.

Test.java
public class Test {

 public static void main(String args[]) {

  long time1 = System.currentTimeMillis();

  for (int i = 0; i < 100000000; i++) {
   Boolean b = Boolean.valueOf(true);
  }

  long time2 = System.currentTimeMillis();

  for (int i = 0; i < 100000000; i++) {
   Boolean b = new Boolean(true);
  }

  long time3 = System.currentTimeMillis();

  System.out.println("Time taken by valueOf method is " + (time2 - time1));
  System.out.println("Time taken by constructor is " + (time3 - time2));
 }

}



Wednesday, 30 November 2016

SWT: Composite widget

A Composite widget can contain other widgets, the way how you are adding widgets to shell, you can add widgets to Composite.

How to define a Composite?
Composite composite1 = new Composite(shell,SWT.BORDER);

How to add widgets to a composite?
Just like how you are adding widgets to Shell, you can add widgets to composite also.

Label label = new Label(composite1,SWT.NONE);

The position of widgets inside the composite are relative to the composite. You can apply following styles to Composite widget.

Widget
Description
NO_BACKGROUND
Style constant for no background behavior
NO_FOCUS
Style constant for no focus from the mouse behavior
NO_MERGE_PAINTS
Style constant for no paint event merging behavior
NO_REDRAW_RESIZE
Style constant for no redraw on resize behavior
NO_RADIO_GROUP
Style constant for preventing child radio group behavior
EMBEDDED
Style constant to allow embedding
DOUBLE_BUFFERED
Style constant to indicate double buffering

package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static int xPosition = 30;
 private static int yPosition = 30;

 private static int shellWidth = 600;
 private static int shellHeight = 600;

 private static int compositeWidth = 250;
 private static int compositeHeight = 250;

 private static String[] countries = { "India", "Germany", "Canada", "Japan", "Sri Lanka", "Singpore", "Russia" };

 private static void addWidgetsToShell(Display display, Shell shell) {

  Composite composite = new Composite(shell, SWT.BORDER);
  composite.setBackground(new Color(display, 102, 153, 153));
  composite.setBounds(10, 10, compositeWidth, compositeHeight);

  Combo combo = new Combo(composite, SWT.DROP_DOWN);
  combo.setItems(countries);
  combo.setBounds(xPosition, 20, 150, 100);
  combo.setText("Select your country");

  yPosition += 100;

  Button button1 = new Button(composite, SWT.PUSH);
  button1.setText("Submit");
  button1.setLocation(xPosition + 100, yPosition);
  button1.setSize(100, 20);

  button1.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    System.out.println("Country Selected is : " + countries[combo.getSelectionIndex()]);
   }
  });

 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(shellWidth, shellHeight);

  addWidgetsToShell(display, shell);

  /* Open shell window */
  shell.open();

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  /* Dispose the display */
  display.dispose();
 }
}


Select any country and submit, you can able to see the selected country in the console.



Previous                                                 Next                                                 Home