Java/SWT JFace Eclipse/Button — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:56, 1 июня 2010
Содержание
Arrow Button Example
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ArrowButtonExample {
Display d;
Shell s;
ArrowButtonExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A Button Example");
final Button b1 = new Button(s, SWT.ARROW | SWT.UP);
b1.setBounds(100, 55, 20, 15);
final Button b2 = new Button(s, SWT.ARROW | SWT.DOWN);
b2.setBounds(100, 70, 20, 15);
final Text t1 = new Text(s, SWT.BORDER | SWT.SINGLE | SWT.CENTER);
t1.setBounds(80, 55, 20, 30);
t1.setText("1");
t1.selectAll();
b1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int n = new Integer(t1.getText()).intValue();
n++;
t1.setText(new Integer(n).toString());
t1.selectAll();
}
});
b2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int n = new Integer(t1.getText()).intValue();
n--;
t1.setText(new Integer(n).toString());
t1.selectAll();
}
});
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new ArrowButtonExample();
}
}
Button Example
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTButtonExample {
Display d;
Shell s;
SWTButtonExample() {
d = new Display();
s = new Shell(d);
s.setSize(150, 150);
s.setText("A Button Example");
final Button b1 = new Button(s, SWT.PUSH);
b1.setBounds(50, 50, 75, 40);
b1.setText("Push Me");
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new SWTButtonExample();
}
}
Button Selection Event
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.Display;
import org.eclipse.swt.widgets.Shell;
public class Gui_4 {
/**
* user code fields V4ALL Entry Level
*/
//user code begin FIELDS {1} Swing
//user code end
//user code begin FIELDS {1} SWT
//user code end
//user code begin FIELDS {1} HTML
//user code end
//user code begin FIELDS {1} Eclipse
//user code end
//user code begin FIELDS {1} C#
//user code end
/**
* user code methods V4ALL Entry Level
*/
//user code begin METHODS {1} Swing
//user code end
//user code begin METHODS {1} SWT
//user code end
//user code begin METHODS {1} HTML
//user code end
//user code begin METHODS {1} Eclipse
//user code end
//user code begin METHODS {1} C#
//user code end
public void processEvent() {
// user code begin {1} Swing
// user code end
// user code begin {1} SWT
ivjLabel100.setText("No worries.");
// user code end
// user code begin {1} HTML
// user code end
// user code begin {1} Eclipse
// user code end
// user code begin {1} C#
// user code end
}
/**
* Initializes connections
*
* @exception java.lang.Exception
* The exception description.
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initConnections() throws java.lang.Exception {
// user code begin {1} Swing
// user code end
// user code begin {1} SWT
// user code end
// user code begin {1} HTML
// user code end
// user code begin {1} Eclipse
// user code end
// user code begin {1} C#
// user code end
// user code begin {2} Swing
// user code end
// user code begin {2} SWT
// user code end
// user code begin {2} HTML
// user code end
// user code begin {2} Eclipse
// user code end
// user code begin {2} C#
// user code end
}
/**
* Return the Frame100 property value.
*
* @return Shell
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
public Shell getFrame100() {
if (ivjFrame100 == null) {
try {
Display display = new Display();
ivjFrame100 = new Shell(display);
ivjFrame100.setBounds(47, 55, 365, 184);
ivjFrame100.setForeground(new Color(null, 0, 0, 0));
int height = java.awt.Toolkit.getDefaultToolkit()
.getScreenSize().height;
int width = java.awt.Toolkit.getDefaultToolkit()
.getScreenSize().width;
ivjFrame100.setLocation(width / 2 - 365 / 2,
height / 2 - 184 / 2);
// user code begin {1} Swing
// user code end
// user code begin {1} SWT
// user code end
// user code begin {1} HTML
// user code end
// user code begin {1} Eclipse
// user code end
// user code begin {1} C#
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {1} Swing
// user code end
// user code begin {1} SWT
// user code end
// user code begin {1} HTML
// user code end
// user code begin {1} Eclipse
// user code end
// user code begin {1} C#
// user code end
handleException(ivjExc);
}
}
return ivjFrame100;
}
/**
* V4ALL Entry Level Return the Button100 property value.
*
* @return Buttonl
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private Button getButton100() {
if (ivjButton100 == null) {
try {
ivjButton100 = new Button(getFrame100(), SWT.NULL);
ivjButton100.setText("Click Me");
ivjButton100.setBounds(80, 76, 85, 25);
ivjButton100.setForeground(new Color(null, 0, 0, 0));
ivjButton100.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
processEvent();
// user code begin {3} SWT
// user code end
};
});
// user code begin {1} Swing
// user code end
// user code begin {1} SWT
// user code end
// user code begin {1} HTML
// user code end
// user code begin {1} Eclipse
// user code end
// user code begin {1} C#
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2} Swing
// user code end
// user code begin {2} SWT
// user code end
// user code begin {2} HTML
// user code end
// user code begin {2} Eclipse
// user code end
// user code begin {2} C#
// user code end
handleException(ivjExc);
}
}
return ivjButton100;
}
/**
* V4ALL Entry Level Return the Label100 property value.
*
* @return org.eclipse.swt.widgets.Labell
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private org.eclipse.swt.widgets.Label getLabel100() {
if (ivjLabel100 == null) {
try {
ivjLabel100 = new org.eclipse.swt.widgets.Label(getFrame100(),
SWT.NULL);
ivjLabel100.setText(" ");
ivjLabel100.setBounds(218, 76, 82, 23);
ivjLabel100.setForeground(new Color(null, 0, 0, 0));
// user code begin {1} Swing
// user code end
// user code begin {1} SWT
// user code end
// user code begin {1} HTML
// user code end
// user code begin {1} Eclipse
// user code end
// user code begin {1} C#
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2} Swing
// user code end
// user code begin {2} SWT
// user code end
// user code begin {2} HTML
// user code end
// user code begin {2} Eclipse
// user code end
// user code begin {2} C#
// user code end
handleException(ivjExc);
}
}
return ivjLabel100;
}
/**
* Initialize the class.
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
try {
// user code begin {1} Swing
// user code end
// user code begin {1} SWT
// user code end
// user code begin {1} HTML
// user code end
// user code begin {1} Eclipse
// user code end
// user code begin {1} C#
// user code end
initConnections();
// user code begin {2} Swing
// user code end
// user code begin {2} SWT
// user code end
// user code begin {2} HTML
// user code end
// user code begin {2} Eclipse
// user code end
// user code begin {2} C#
// user code end
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
/**
* V4ALL Entry Level Called whenever the part throws an exception.
*
* @param exception
* java.lang.Throwable
*/
private void handleException(java.lang.Throwable exception) {
/* Uncomment the following lines to print uncaught exceptions to stdout */
// System.out.println("--------- UNCAUGHT EXCEPTION ---------");
// exception.printStackTrace(System.out);
}
private Shell ivjFrame100 = null;
private Button ivjButton100 = null;
private org.eclipse.swt.widgets.Label ivjLabel100 = null;
/**
* Gui_4 constructor comment.
*
* @param title
* java.lang.String
*/
public Gui_4(String title) {
getFrame100().setText(title);
}
/**
* main entrypoint - starts the part when it is run as an application
*
* @param args
* java.lang.String[]
*/
public static void main(java.lang.String[] args) {
try {
Gui_4 aGui_4;
aGui_4 = new Gui_4("Frame");
aGui_4.getFrame100();
aGui_4.getButton100();
aGui_4.getLabel100();
aGui_4.getFrame100().open();
while (!aGui_4.getFrame100().isDisposed()) {
if (!aGui_4.getFrame100().getDisplay().readAndDispatch())
aGui_4.getFrame100().getDisplay().sleep();
}
aGui_4.getFrame100().dispose();
} catch (Throwable exception) {
System.err
.println("Exception occurred in main() of javax.swing.JFrame");
exception.printStackTrace(System.out);
}
}
}
Button Styles
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 26, 2004 7:38:13 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonStyles {
public ButtonStyles() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(5, true));
Button arrowButton = new Button(shell, SWT.ARROW);
arrowButton.setText("&Arrow Button");
Button checkButton = new Button(shell, SWT.CHECK);
checkButton.setText("&Check Button");
Button pushButton = new Button(shell, SWT.PUSH);
pushButton.setText("&Push Button");
Button radioButton = new Button(shell, SWT.RADIO);
radioButton.setText("&Radio Button");
Button toggleButton = new Button(shell, SWT.TOGGLE);
toggleButton.setText("&Toggle Button");
// shell.pack();
shell.setSize(500, 100);
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new ButtonStyles();
}
}
Default Button
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 25, 2004 11:46:01 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTDefaultButton {
public SWTDefaultButton() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
final String[] ratings =
new String[] { "Killer!", "Good stuff", "So-so", "Needs work" };
final Button[] radios = new Button[ratings.length];
for (int i = 0; i < ratings.length; i++) {
radios[i] = new Button(shell, SWT.RADIO);
radios[i].setText(ratings[i]);
}
Button cancelButton = new Button(shell, SWT.PUSH);
cancelButton.setText("Canel");
Button rateButton = new Button(shell, SWT.PUSH);
rateButton.setText("Rate!");
rateButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i < radios.length; i++)
if (radios[i].getSelection())
System.out.println("Rating: " + ratings[i]);
}
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println("Default selection");
}
});
shell.setDefaultButton(rateButton);
System.out.println(shell.getDefaultButton());
shell.pack();
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SWTDefaultButton();
}
}
Demonstrates Buttons
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates Buttons
*/
public class ButtonExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
// Create three push buttons
new Button(shell, SWT.PUSH).setText("Push 1");
new Button(shell, SWT.PUSH).setText("Push 2");
new Button(shell, SWT.PUSH).setText("Push 3");
// Create three checkboxes
new Button(shell, SWT.CHECK).setText("Checkbox 1");
new Button(shell, SWT.CHECK).setText("Checkbox 2");
new Button(shell, SWT.CHECK).setText("Checkbox 3");
// Create three toggle buttons
new Button(shell, SWT.TOGGLE).setText("Toggle 1");
new Button(shell, SWT.TOGGLE).setText("Toggle 2");
new Button(shell, SWT.TOGGLE).setText("Toggle 3");
// Create three radio buttons
new Button(shell, SWT.RADIO).setText("Radio 1");
new Button(shell, SWT.RADIO).setText("Radio 2");
new Button(shell, SWT.RADIO).setText("Radio 3");
// Create three flat buttons
new Button(shell, SWT.FLAT).setText("Flat 1");
new Button(shell, SWT.FLAT).setText("Flat 2");
new Button(shell, SWT.FLAT).setText("Flat 3");
// Create three arrow buttons
new Button(shell, SWT.ARROW);
new Button(shell, SWT.ARROW | SWT.LEFT);
new Button(shell, SWT.ARROW | SWT.DOWN);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Icon Selector
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 5, 2004 2:15:34 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
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.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class IconSelector {
Display display = new Display();
Shell shell = new Shell(display);
Label labelIconFile;
Text textIconFile;
Button buttonIconBrowse;
Button buttonSetIcon;
Image shellIcon;
Image buttonIcon;
public IconSelector() {
initializeUI();
shell.pack();
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void initializeUI() {
GridLayout gridLayout = new GridLayout(3, false);
shell.setLayout(gridLayout);
// Adds components to the first row.
labelIconFile = new Label(shell, SWT.NULL);
textIconFile = new Text(shell, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.grabExcessHorizontalSpace = true;
textIconFile.setLayoutData(gridData);
buttonIconBrowse = new Button(shell, SWT.PUSH);
// last row.
gridData = new GridData();
gridData.horizontalSpan = 3;
gridData.horizontalAlignment = GridData.CENTER;
buttonSetIcon = new Button(shell, SWT.PUSH);
buttonSetIcon.setLayoutData(gridData);
shell.setText("Icon Selector");
labelIconFile.setText("Select an icon:");
buttonIconBrowse.setText("Browse");
buttonSetIcon.setText("Set Icon");
// Register listeners.
buttonIconBrowse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
String file = dialog.open();
if (file != null) {
textIconFile.setText(file);
}
}
});
buttonSetIcon.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if(shellIcon != null)
shellIcon.dispose();
try {
shellIcon = new Image(display, textIconFile.getText());
shell.setImage(shellIcon);
}catch(Exception ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
IconSelector iconSelector = new IconSelector();
}
}
Image Button
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 26, 2004 8:19:53 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ImageButton {
Display display = new Display();
Shell shell = new Shell(display);
Image image = new Image(display, "jexp.gif");
public ImageButton() {
//shell.setLayout(new RowLayout());
Button button = new Button(shell, SWT.PUSH);
button.setBounds(10, 10, 200, 200);
button.setImage(image);
button.setText("button");
System.out.println(button.getImage());
shell.pack();
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new ImageButton();
}
}
Make a toggle button have radio behavior
/*
* Make a toggle button have radio behavior
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
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 Snippet169 {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Listener listener = new Listener() {
public void handleEvent(Event e) {
Control[] children = shell.getChildren();
for (int i = 0; i < children.length; i++) {
Control child = children[i];
if (e.widget != child && child instanceof Button
&& (child.getStyle() & SWT.TOGGLE) != 0) {
((Button) child).setSelection(false);
}
}
((Button) e.widget).setSelection(true);
}
};
for (int i = 0; i < 20; i++) {
Button button = new Button(shell, SWT.TOGGLE);
button.setText("B" + i);
button.addListener(SWT.Selection, listener);
if (i == 0)
button.setSelection(true);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Set the default button
/*
* Button example snippet: set the default button
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet108 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Label label = new Label(shell, SWT.NONE);
label.setText("Enter your name:");
Text text = new Text(shell, SWT.BORDER);
text.setLayoutData(new RowData(100, SWT.DEFAULT));
Button ok = new Button(shell, SWT.PUSH);
ok.setText("Ok");
Button cancel = new Button(shell, SWT.PUSH);
cancel.setText("Cancel");
shell.setDefaultButton(cancel);
shell.setLayout(new RowLayout());
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ButtonClass {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(200, 200);
shell.setText("Button Example");
final Button button = new Button(shell, SWT.PUSH);
button.setBounds(40, 50, 50, 20);
button.setText("Click Me");
final Text text = new Text(shell, SWT.BORDER);
text.setBounds(100, 50, 70, 20);
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
text.setText("No problem");
}
public void widgetDefaultSelected(SelectionEvent event) {
text.setText("No worries!");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT Button Action
/*
* Created on Nov 20, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* @author Steven Holzner
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class SWTButtonAction {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(200, 200);
shell.setText("Dialogs");
shell.open();
final Button opener = new Button(shell, SWT.PUSH);
opener.setText("Click Me");
opener.setBounds(20, 20, 50, 25);
final Text text = new Text(shell, SWT.SHADOW_IN);
text.setBounds(80, 20, 100, 25);
final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL
| SWT.DIALOG_TRIM);
dialog.setText("Dialog");
dialog.setSize(150, 100);
final Label label = new Label(dialog, SWT.NONE);
label.setText("OK to proceed?");
label.setBounds(35, 5, 100, 20);
final Button okButton = new Button(dialog, SWT.PUSH);
okButton.setBounds(20, 35, 40, 25);
okButton.setText("OK");
Button cancelButton = new Button(dialog, SWT.PUSH);
cancelButton.setBounds(70, 35, 40, 25);
cancelButton.setText("Cancel");
final boolean[] response = new boolean[1];
response[0] = true;
Listener listener = new Listener() {
public void handleEvent(Event event) {
if (event.widget == okButton) {
response[0] = true;
} else {
response[0] = false;
}
dialog.close();
}
};
okButton.addListener(SWT.Selection, listener);
cancelButton.addListener(SWT.Selection, listener);
Listener openerListener = new Listener() {
public void handleEvent(Event event) {
dialog.open();
}
};
opener.addListener(SWT.Selection, openerListener);
while (!dialog.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
if (response[0]) {
text.setText("You clicked OK");
} else {
text.setText("You clicked Cancel");
}
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT Button Example Demo
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* @author Steven Holzner
*
*/
public class SWTButtonExampleDemo {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(300, 200);
shell.setText("Button Example");
shell.setLayout(new RowLayout());
final Button button = new Button(shell, SWT.PUSH);
button.setText("Click Me");
final Text text = new Text(shell, SWT.SHADOW_IN);
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
text.setText("No worries!");
}
public void widgetDefaultSelected(SelectionEvent event) {
text.setText("No worries!");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Widget Styles
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 23, 2004 8:56:30 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class WidgetStyles {
Display display = new Display();
Shell shell = new Shell(display);
Button button;
public WidgetStyles() {
init();
shell.pack();
shell.open();
System.out.println("Bounds: " + button.getBounds() );
System.out.println("computeSize: " + button.ruputeSize(100, SWT.DEFAULT));
System.out.println("computeSize: " + button.ruputeSize(40, SWT.DEFAULT));
System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, 100));
System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, 20));
System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, 15));
System.out.println("computeSize: " + button.ruputeSize(100, 200));
System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, SWT.DEFAULT));
System.out.println("toControl: " + button.toControl(100, 200));
System.out.println("toDisplay: " + button.toDisplay(100, 200));
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
System.out.println(e.widget + " disposed");
}
});
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
shell.setLayout(new GridLayout());
button = new Button(shell, SWT.PUSH | SWT.LEFT);
button.setText("Button");
}
public static void main(String[] args) {
new WidgetStyles();
}
}