How to close all open dialogs of a given SWT shell? - java

My program opens a dialog (on a given shell):
new FileDialog(shell).open();
My Question is: How can I (in another part of my program) close all open dialogs of a given
shell?
private void closeDialogs(Shell shell) {
// how to close open dialogs?
}
Below is minimal test case, one button opens the dialogs. The other button should
close all open dialogs of the current shell.
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Testcase");
shell.setLayout(new FillLayout());
final Button button = new Button(shell, SWT.PUSH);
button.setText("Open Dialog");
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
new NonModalDialog(shell).open();
}
});
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Close open dialogs");
button2.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// How to close all open dialogs of the given shell?
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

You will have to collect the dialogs in a list as you open them.
When the "close" button is clicked, walk through the list and close the dialogs.
Note: This will not work when any of the dialogs are modal. A modal dialog will take over the event loop and ignore any events that aren't directed at itself, so clicking on the button will be impossible.

Related

Stacking modal dialogs in SWT

I've a problem stacking dialog windows in pure SWT.
It's a little bit complicated to describe it but I try it: I have a starter class which builds the main application window. From the main window I open a first modal dialog by clicking a button. And from this first dialog I open a second dialog which is a modal window again. This second dialog contains only one label and no other components.
The use case is to have the main GUI and a first dialog in which the user can manage menu shortcuts. For this purpose the window contains for example a dorpdown list with the existing shortcuts and a button to change a selected one. By clicking this button the second window is opened in which the user will prompt for pressing the new keys.
My code looks like this:
public class Runner {
public static void main(String[] args) {
Display display = new Display();
MainWindow app = new MainWindow(display);
app.open();
display.dispose();
}
}
public class MainWindow {
private Display display;
private Shell shell;
public MainWindow(Display display) {
this.display = display;
createContents();
}
public void open() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
private void createContents() {
shell = new Shell(display);
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
showFirstWindow();
}
});
}
private void showFirstWindow() {
FirstWindow firstWindow = new FirstWindow(shell);
firstWindow.open();
}
}
public class FirstWindow extends Dialog {
private Display display;
private Shell shell;
public FirstWindow(Shell parent) {
super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
display = parent.getDisplay();
createContents();
}
public void open() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
private void createContents() {
shell = new Shell(getParent(), getStyle());
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
showSecondWindow();
}
});
}
private void showSecondWindow() {
SecondWindow secondWindow = new SecondWindow(shell);
secondWindow.open();
}
}
public class SecondWindow extends Dialog {
private Display display;
private Shell shell;
private Label label;
public SecondWindow(Shell parent) {
super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
display = parent.getDisplay();
createContents();
}
public void open() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
private void createContents() {
shell = new Shell(getParent(), getStyle());
label = new Label(shell, SWT.NONE);
label.setLocation(10, 10);
label.setSize(200, 13);
label.setText("prompt text");
}
}
Under Windows this construct works fine but under Mac OS X the second dialog is invisible. It's interesting that the KeyPressedHandler is triggered but the window and thus the prompt message is not shown.
Is it not possible to overlay a few dialogs in OS X or is it a bug of SWT or is it a problem in my code? I can't figure out the reason.

Change Label into Textbox/Button

I would like to create a dialog window that shows a user's profile (such as name, avatar, sex, etc.) in a visually pleasing way by using labels that contain the relevant information. At the same time, I would like to show an edit button that transforms the labels into textboxes or buttons allowing the name or avatar to be changed.
How can I realize this with SWT? What would be the best strategy to approach this?
You can use a Text and then set setEditable(boolean) to enable/disable editing of the text box from the SWT.Selection Listener of the Button:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
// Create a read-only text field
final Text text = new Text(shell, SWT.BORDER);
text.setEditable(false);
text.setText("Some text here");
Button button = new Button(shell, SWT.PUSH);
button.setText("Change");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event event)
{
text.setEditable(!text.getEditable());
}
});
shell.open();
shell.pack();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Alternatively, you can use a StackLayout and have two versions of the same UI (or just parts of it) and switch between them when the button is pressed.

SWT Browser Control Right click Popup menu Function called on Button click

I am implementing a SWT Browser in my project to display HTML
Pages. When user right clicks on this browser it shows a popup menu with functionalities like "Print", "Print View". Can this be done with separate buttons, if I place them on a Toolbar?
Another functionality of the browser control is using "Ctrl+F", which bring a find dialogue. Can this dialogue be called using a button?
please help me?
You can achieve some of those goals by executing JavaScript when the Button is pressed.
Here is an example for the print dialog:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
final Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button button = new Button(shell, SWT.PUSH);
button.setText("Search");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
browser.execute("window.print()");
}
});
shell.pack();
shell.setSize(600, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Just search online how to achieve the other functionalities that you want to add by using javascript.

SWT Tray Item in gnome

I have an issue about the tray items display in Gnome Shell.
The item icon appears in the lower bar, and I'm fine with that, and just like the other ones, when moving the mouse over the icon, a text is displayed.
My problem is that this text cannot be changed: setting a text with .setText does not work, neither the class support any event, but Selected and MenuDetect, which detect the left and right click, respectively.
Has anybody experienced the same problem?
Thanks a lot in advance for your answers,
Gianluca
Not quite sure what problem you are facing, but this works for me:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Tray tray = display.getSystemTray();
TrayItem item;
if (tray != null) {
item = new TrayItem(tray, SWT.NONE);
item.setToolTipText("A");
item.setImage(display.getSystemImage(SWT.ICON_ERROR));
}
Button button = new Button(shell, SWT.PUSH);
button.setText("Change tray text");
button.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event arg0) {
if(tray != null)
{
TrayItem item = tray.getItem(0);
item.setToolTipText(item.getToolTipText() + "A");
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Note that I don't use setText(), but rather setToolTipText().

any way to determine the event like mouse click for title bar in eclipse Application

i am using eclipse rcp and swt for developing application.i have a list and i need to set its visibility false on a movement of title bar or when user click a titleBar. i am unable to find any event of titleBar. Is there any event of titleBar so that i can solve my problem? Or any thing that could probably solve my problem? i searched but could find for flex only as
function panelClick ( event:MouseEvent ) : void
{
trace( event.localX + '/' + event.localY );
}
is there same thing for swt using eclispe.
thanks in advance.
The following code will output "Move" and "Minimize" when the corresponding events happen:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Button dummy = new Button(shell, SWT.PUSH);
dummy.setText("Dummy");
shell.addListener(SWT.Move, new Listener() {
#Override
public void handleEvent(Event arg0) {
System.out.println("Move");
}
});
shell.addListener(SWT.Iconify, new Listener() {
#Override
public void handleEvent(Event arg0) {
System.out.println("Minimize");
}
});
shell.pack();
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
However, the SWT.MOVE event is only fired after the shell has been moved, i.e. when the "move" is over.

Categories