SWT – Button Example
Written on January 4, 2009 at 1:20 pm by
mkyong
In SWT, button widget consists of five buttons style.
1) Normal Button - SWT.PUSH
2) Arrow Button – SWT.ARROW
3) Toggle Button – SWT.TOGGLE
4) Check Box button – SWT.CHECK
5) Radio Button – SWT.RADIO

Here i demonstrate how to create those buttons in SWT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class SWTButton { public static void main (String [] args) { Display display = new Display (); Shell shell = new Shell(display); //push button Button pushButton = new Button(shell, SWT.PUSH); pushButton.setLocation(50, 50); pushButton.setText("Im a Push Button"); pushButton.pack(); //arrow button Button arrowLeftButton = new Button(shell, SWT.ARROW | SWT.LEFT); arrowLeftButton.setLocation(50, 100); arrowLeftButton.setText("LEFT"); arrowLeftButton.pack(); Button arrowRightButton = new Button(shell, SWT.ARROW | SWT.RIGHT); arrowRightButton.setLocation(80, 100); arrowRightButton.setText("RIGHT"); arrowRightButton.pack(); Button arrowUpButton = new Button(shell, SWT.ARROW | SWT.UP); arrowUpButton.setLocation(110, 100); arrowUpButton.setText("UP"); arrowUpButton.pack(); Button arrowDownButton = new Button(shell, SWT.ARROW | SWT.DOWN); arrowDownButton.setLocation(140, 100); arrowDownButton.setText("DOWN"); arrowDownButton.pack(); //toggle Button toggleNotPressedButton = new Button(shell, SWT.TOGGLE); toggleNotPressedButton.setSelection(false); toggleNotPressedButton.setLocation(50, 150); toggleNotPressedButton.setText("Button Not Pressed"); toggleNotPressedButton.pack(); Button togglePressedButton = new Button(shell, SWT.TOGGLE); togglePressedButton.setSelection(true); togglePressedButton.setLocation(170, 150); togglePressedButton.setText("Button Pressed"); togglePressedButton.pack(); //check box Button[] checkBoxs = new Button[3]; checkBoxs[0] = new Button(shell, SWT.CHECK); checkBoxs[0].setSelection(true); checkBoxs[0].setText("Choice 1"); checkBoxs[0].setLocation(50,200); checkBoxs[0].pack(); checkBoxs[1] = new Button(shell, SWT.CHECK); checkBoxs[1].setText("Choice 2"); checkBoxs[1].setLocation(120,200); checkBoxs[1].pack(); checkBoxs[2] = new Button(shell, SWT.CHECK); checkBoxs[2].setText("Choice 3"); checkBoxs[2].setLocation(190,200); checkBoxs[2].pack(); //radio Button[] radioButtons = new Button[3]; radioButtons[0] = new Button(shell, SWT.RADIO); radioButtons[0].setSelection(true); radioButtons[0].setText("Choice 1"); radioButtons[0].setLocation(50,250); radioButtons[0].pack(); radioButtons[1] = new Button(shell, SWT.RADIO); radioButtons[1].setText("Choice 2"); radioButtons[1].setLocation(120,250); radioButtons[1].pack(); radioButtons[2] = new Button(shell, SWT.RADIO); radioButtons[2].setText("Choice 3"); radioButtons[2].setLocation(190,250); radioButtons[2].pack(); shell.setSize(500,500); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } } |
This article was posted in SWT category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery