Aligning the text on an SWT button

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 400, 200);
      shell.setText("SWT Button Alignment Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            System.out.print(button.getText());
            System.out.println(" selected = " + button.getSelection());
         };
      };
       
      Button button1 = new Button(buttonGroup, SWT.PUSH);
      button1.setText("left");
      sizeControl(button1, 100, 30);   
      button1.setAlignment(SWT.LEFT);   
      button1.addSelectionListener(selectionListener);
       
      Button button2 = new Button(buttonGroup, SWT.PUSH);
      button2.setText("center");
      sizeControl(button2, 100, 30);
      button2.setAlignment(SWT.CENTER);
      button2.addSelectionListener(selectionListener);
 
      Button button3 = new Button(buttonGroup, SWT.PUSH);
      button3.setText("right");
      sizeControl(button3, 100, 30);
      button3.setAlignment(SWT.RIGHT);
      button3.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
    
   public static void sizeControl(Control control, int width, int height) {
      GridData gridData = new GridData();   
      gridData.widthHint = width;
      gridData.heightHint = height;
      control.setLayoutData(gridData);
   }
}

Using an image button in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT Image Button Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            System.out.print(button.getText());
            System.out.println(" selected = " + button.getSelection());
         };
      };
                   
      Button button1 = new Button(buttonGroup, SWT.PUSH);
      button1.setImage(getImage("jpgIcon.gif"));
      button1.addSelectionListener(selectionListener);
      Button button2 = new Button(buttonGroup, SWT.PUSH);
      button2.setImage(getImage("gifIcon.gif"));
      button2.addSelectionListener(selectionListener);
      Button button3 = new Button(buttonGroup, SWT.PUSH);
      button3.setImage(getImage("tiffIcon.gif"));
      button3.addSelectionListener(selectionListener);
       
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
    
   public static Image getImage(String imageName) {
      ImageData source = new ImageData(Main.class.getResourceAsStream(imageName));
      ImageData mask = source.getTransparencyMask();
      Image image = new Image(null, source, mask);
      return image;
   }
}

Images used:


Using an ArrowButton in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT ToggleButton Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 4;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            System.out.println("integer value for alignment: " + ((Button) event.widget).getAlignment());
         };
      };
 
      createArrowButton(buttonGroup, SWT.LEFT, selectionListener);
      createArrowButton(buttonGroup, SWT.RIGHT, selectionListener);
      createArrowButton(buttonGroup, SWT.UP, selectionListener);
      createArrowButton(buttonGroup, SWT.DOWN, selectionListener);
      
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
   
   public static void createArrowButton(Composite parent, int alignment, 
                                          SelectionListener selectionListener) {
      GridData gridData = new GridData();
      gridData.widthHint = 30;
      gridData.heightHint = 30;
      Button button = new Button(parent, SWT.ARROW);
      button.setLayoutData(gridData);
      button.setAlignment(alignment);
      button.addSelectionListener(selectionListener);
   }
}

Using a ToggleButton in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT ToggleButton Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            System.out.print(button.getText());
            System.out.println(" selected = " + button.getSelection());
         };
      };
      
      Button button1 = new Button(buttonGroup, SWT.TOGGLE);
      button1.setText("orange");
      button1.addSelectionListener(selectionListener);
      Button button2 = new Button(buttonGroup, SWT.TOGGLE);
      button2.setText("pear");
      button2.addSelectionListener(selectionListener);
      Button button3 = new Button(buttonGroup, SWT.TOGGLE);
      button3.setText("apple");
      button3.addSelectionListener(selectionListener);
      
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Using a RadioButton in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT RadioButton Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL));
      
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            System.out.print(button.getText());
            System.out.println(" selected = " + button.getSelection());
         };
      };
            
      Button button1 = new Button(buttonGroup, SWT.RADIO);
      button1.setText("orange");
      button1.addSelectionListener(selectionListener);
      Button button2 = new Button(buttonGroup, SWT.RADIO);
      button2.setText("pear");
      button2.addSelectionListener(selectionListener);
      Button button3 = new Button(buttonGroup, SWT.RADIO);
      button3.setText("apple");
      button3.addSelectionListener(selectionListener);
      
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Using a CheckBox in SWT

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
 
public class Main {
   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
 
      // pos x, pos y, width, height
      shell.setBounds(200, 200, 300, 200);
      shell.setText("SWT Checkbox Demonstration");
      shell.setLayout(new GridLayout());
 
      Group buttonGroup = new Group(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 3;
      buttonGroup.setLayout(gridLayout);
      buttonGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      
      SelectionListener selectionListener = new SelectionAdapter () {
         public void widgetSelected(SelectionEvent event) {
            Button button = ((Button) event.widget);
            System.out.print(button.getText());
            System.out.println(" selected = " + button.getSelection());
         };
      };
                  
      Button button1 = new Button(buttonGroup, SWT.CHECK);
      button1.setText("orange");
      button1.addSelectionListener(selectionListener);
      Button button2 = new Button(buttonGroup, SWT.CHECK);
      button2.setText("pear");
      button2.addSelectionListener(selectionListener);
      Button button3 = new Button(buttonGroup, SWT.CHECK);
      button3.setText("apple");
      button3.addSelectionListener(selectionListener);
      
      shell.open();
 
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}

Don’t change filename textfield when a directory is selected in a JFileChooser

Here’s some code that solves that problem.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.io.*;
 
public class Main extends JFrame
{
   private String fileSelected = "autoexec.bat";
 
   public static void main(String []args) {
      Main main = new Main();
      main.show();
   }
  
   public Main() {
      JButton fileButton = new JButton("Select File");
      fileButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            File file = getFileFromUser();
            if (file != null)
               System.out.println(file.getName());
         }
      });
      getContentPane().add(fileButton);
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
 
      pack();
   }
  
   public File getFileFromUser() {
      JFileChooser fc = new JFileChooser();
 
      // use current directory
      fc.setCurrentDirectory(new File("c:\"));
 
      // set default name
      fc.setSelectedFile(new File(fileSelected));
 
      fc.addPropertyChangeListener(new PropertyChangeListener() {
         public void propertyChange(PropertyChangeEvent pce) {
            String property = pce.getPropertyName();
            if (property.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
               File dir = (File) pce.getNewValue();
               if (dir.isDirectory()) {
                  ((JFileChooser) pce.getSource()).setSelectedFile(new File(dir, fileSelected));
               }
               else {
                  fileSelected = ((JFileChooser) pce.getSource()).getSelectedFile().getName();
               }
            }
            else if (property.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {
               File dir = (File) pce.getNewValue();
               ((JFileChooser) pce.getSource()).setSelectedFile(new File(dir, fileSelected));
            }
         }
      });
 
      // show dialog for opening files
      int result = fc.showSaveDialog(this);
 
      if (result != fc.APPROVE_OPTION) 
         return null;
       
      return fc.getSelectedFile();
   }
}

Embedding IE in an SWT application

Main.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
 
public class EmbedIE {
   OleAutomation automation = null;
   
   public EmbedIE() {
      Display display = new Display();
      Shell shell = new Shell(display);

      FormLayout layout= new FormLayout();
      shell.setLayout(layout);

      OleFrame frame = new OleFrame(shell, SWT.PUSH);
      FormData formData = new FormData();
      formData.top = new FormAttachment(0, 10);
      formData.left = new FormAttachment(0, 100);
      formData.right = new FormAttachment(100, 0);
      formData.bottom = new FormAttachment(100, -10);
      frame.setLayoutData(formData);
      OleControlSite controlSite = new OleControlSite(frame, SWT.NONE, "Shell.Explorer");
      controlSite.doVerb(OLE.OLEIVERB_SHOW);
      automation = new OleAutomation(controlSite);
      int[] rgdispid = automation.getIDsOfNames(new String[]{"GoHome"});
      int dispIdMember = rgdispid[0];
      automation.invoke(dispIdMember);
      
      Composite composite = new Composite(shell, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      composite.setLayout(gridLayout);
      Button site1 = new Button(composite, SWT.NONE);
      site1.setText("IE Home");
      Button site2 = new Button(composite, SWT.NONE);
      site2.setText("Synergetic");
      Button site3 = new Button(composite, SWT.NONE);
      site3.setText("Esus.com");
      
      site1.addListener(SWT.Selection, new Listener() {
         public void handleEvent(Event e) {
            goHome();
         }
      });      
      
      site2.addListener(SWT.Selection, new Listener() {
         public void handleEvent(Event e) {
            goUrl("www.synergetic-solutions.be");
         }
      });
      
      site3.addListener(SWT.Selection, new Listener() {
         public void handleEvent(Event e) {
            goUrl("www.esus.com");
         }
      });
      
      
      shell.open();
      
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();      
   }
   
   public void goHome() {
      int[] rgdispid = automation.getIDsOfNames(new String[]{"GoHome"});
      int dispIdMember = rgdispid[0];
      automation.invoke(dispIdMember);
   }      
   
   public void goUrl(String url) {
      int[] rgdispid = automation.getIDsOfNames(new String[]{"Navigate"});
      int dispIdMember = rgdispid[0];
      Variant[] rgvarg = new Variant[1]; // this is the URL parameter
      rgvarg[0] = new Variant(url);
      Variant pVarResult = automation.invoke(dispIdMember, rgvarg);      
   }
   
   public static void main(String []args) {
      new EmbedIE();
   }
}

Creating a JFace ApplicationWindow with MenuBar

Main.java:

import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.action.*;
import org.eclipse.jface.window.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
 
public class Main extends ApplicationWindow {
   public Main(Shell parent) {
      super(parent);
   }
   
   public static void main(String []args) {
      Display display = new Display();
      Shell shell = new Shell(display);
      
      Main main = new Main(shell);
      main.createComponents();
      main.open();
   }
 
   public void createComponents() {
      setBlockOnOpen(true);
      addMenuBar();
   }
   
   protected MenuManager createMenuManager() {
      MenuManager menuManager = new MenuManager();
      menuManager.add(createFileMenu());
      return menuManager;
   }
   
   protected MenuManager createFileMenu() {
      MenuManager menu = new MenuManager("&File", "Id01");

      menu.add(new Action() {
         public String getText() {
            return "&Open";
         }
         
         public void run() {
            String[] buttons = { IDialogConstants.OK_LABEL,
                                  IDialogConstants.CANCEL_LABEL };
            MessageDialog dialog = new MessageDialog(getShell(), "Title", null, 
                                                     "File/Open selected!", 
                                                     MessageDialog.INFORMATION, buttons, 0);
            dialog.open();   // blocking call
         }
      });

      menu.add(new Action() {
         public String getText() {
            return "E&xit";
         }
         
         public void run() {
            Main.this.getParentShell().close();
         }
      });
       
      return menu;
   }
    
   protected void configureShell(Shell shell) {
      super.configureShell(shell);
      shell.setSize(300, 200);
      shell.setText("Example JFace with MenuBar");
   }  
}

Creating a simple JFace ApplicationWindow

Make sure your classpath and library path includes the correct SWT and JFace libraries.

In my case:

   org.eclipse.ui_2.0.1/workbench.jar
   org.eclipse.swt.win32_2.0.1/ws/win32/swt.jar
   org.eclipse.core.runtime_2.0.1/runtime.jar

Main.java:

import org.eclipse.jface.window.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
 
public class Main extends ApplicationWindow {
   public Main(Shell parent) {
      super(parent);
      setBlockOnOpen(true);
   }
   
   public static void main(String []args) {
      Display display = new Display();
      Shell shell = new Shell(display);
      
      Main main = new Main(shell);
      main.open();
   }
}