OnJava8-Examples/swt/TabbedPane.java
2015-12-15 11:47:04 -08:00

150 lines
5.0 KiB
Java

// swt/TabbedPane.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Placing SWT components in tabbed panes.
import swt.util.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.browser.*;
public class TabbedPane implements SWTApplication {
private static TabFolder folder;
private static Shell shell;
@Override
public void createContents(Composite parent) {
shell = parent.getShell();
parent.setLayout(new FillLayout());
folder = new TabFolder(shell, SWT.BORDER);
labelTab();
directoryDialogTab();
buttonTab();
sliderTab();
scribbleTab();
browserTab();
}
public static void labelTab() {
TabItem tab = new TabItem(folder, SWT.CLOSE);
tab.setText("A Label"); // Text on the tab
tab.setToolTipText("A simple label");
Label label = new Label(folder, SWT.CENTER);
label.setText("Label text");
tab.setControl(label);
}
public static void directoryDialogTab() {
TabItem tab = new TabItem(folder, SWT.CLOSE);
tab.setText("Directory Dialog");
tab.setToolTipText("Select a directory");
final Button b = new Button(folder, SWT.PUSH);
b.setText("Select a Directory");
b.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
DirectoryDialog dd = new DirectoryDialog(shell);
String path = dd.open();
if(path != null)
b.setText(path);
}
});
tab.setControl(b);
}
public static void buttonTab() {
TabItem tab = new TabItem(folder, SWT.CLOSE);
tab.setText("Buttons");
tab.setToolTipText("Different kinds of Buttons");
Composite composite = new Composite(folder, SWT.NONE);
composite.setLayout(new GridLayout(4, true));
for(int dir : new int[]{
SWT.UP, SWT.RIGHT, SWT.LEFT, SWT.DOWN
}) {
Button b = new Button(composite, SWT.ARROW | dir);
b.addListener(SWT.MouseDown, listener);
}
newButton(composite, SWT.CHECK, "Check button");
newButton(composite, SWT.PUSH, "Push button");
newButton(composite, SWT.RADIO, "Radio button");
newButton(composite, SWT.TOGGLE, "Toggle button");
newButton(composite, SWT.FLAT, "Flat button");
tab.setControl(composite);
}
private static Listener listener = new Listener() {
public void handleEvent(Event e) {
MessageBox m = new MessageBox(shell, SWT.OK);
m.setMessage(e.toString());
m.open();
}
};
private static void newButton(Composite composite,
int type, String label) {
Button b = new Button(composite, type);
b.setText(label);
b.addListener(SWT.MouseDown, listener);
}
public static void sliderTab() {
TabItem tab = new TabItem(folder, SWT.CLOSE);
tab.setText("Sliders and Progress bars");
tab.setToolTipText("Tied Slider to ProgressBar");
Composite composite = new Composite(folder, SWT.NONE);
composite.setLayout(new GridLayout(2, true));
final Slider slider =
new Slider(composite, SWT.HORIZONTAL);
final ProgressBar progress =
new ProgressBar(composite, SWT.HORIZONTAL);
slider.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
progress.setSelection(slider.getSelection());
}
});
tab.setControl(composite);
}
public static void scribbleTab() {
TabItem tab = new TabItem(folder, SWT.CLOSE);
tab.setText("Scribble");
tab.setToolTipText("Simple graphics: drawing");
final Canvas canvas = new Canvas(folder, SWT.NONE);
ScribbleMouseListener sml= new ScribbleMouseListener();
canvas.addMouseListener(sml);
canvas.addMouseMoveListener(sml);
tab.setControl(canvas);
}
private static class ScribbleMouseListener
extends MouseAdapter implements MouseMoveListener {
private Point p = new Point(0, 0);
public void mouseMove(MouseEvent e) {
if((e.stateMask & SWT.BUTTON1) == 0)
return;
GC gc = new GC((Canvas)e.widget);
gc.drawLine(p.x, p.y, e.x, e.y);
gc.dispose();
updatePoint(e);
}
public void mouseDown(MouseEvent e) { updatePoint(e); }
private void updatePoint(MouseEvent e) {
p.x = e.x;
p.y = e.y;
}
}
public static void browserTab() {
TabItem tab = new TabItem(folder, SWT.CLOSE);
tab.setText("A Browser");
tab.setToolTipText("A Web browser");
Browser browser = null;
try {
browser = new Browser(folder, SWT.NONE);
} catch(SWTError e) {
Label label = new Label(folder, SWT.BORDER);
label.setText("Could not initialize browser");
tab.setControl(label);
}
if(browser != null) {
browser.setUrl("http://www.MindviewInc.com");
tab.setControl(browser);
}
}
public static void main(String[] args) {
SWTConsole.run(new TabbedPane(), 800, 600);
}
}