I have searched the web for quite a while now, and have not come across a simple solution for making a button, which opens a file browsing dialog in an Eclipse program. I am using WindowBuilder at the moment, and it seems odd to me, that one has to do so much to add a simple "Browse..." button to their GUI.
I hope someone can help me on this matter, thanks!
I use this code:
private Button browse;
browse = new Button(outerGroup, SWT.PUSH);
browse.setText("Browse ...");
browse.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,0));
browse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(shell, SWT.NULL);
String path = dialog.open();
if (path != null) {
//do stuff with path
}
}
});
Related
I want to drag a text box from vertical layout container and drop it in a flow layout container. On drop, it should appear as a Rich Text Area. Please find the code below. I tried to debug the code and it is getting entered only inside dragging. Debugging is not coming inside droptarget. Can you please help me?
final TextButton textButton = new TextButton();
textButton.setText("Text Box");
DragSource source = new DragSource(textButton) {
#Override
protected void onDragStart(DndDragStartEvent event) {
super.onDragStart(event);
}
};
DropTarget dropTarget = new DropTarget(flowLayoutContainer);
dropTarget.setOperation(Operation.COPY);
dropTarget.addDropHandler(new DndDropHandler() {
#Override
public void onDrop(DndDropEvent event) {
final RichTextArea textBox1 = new RichTextArea();
flowLayoutContainer.add(textBox1);
}
});
I'm currently developing an app, and for this, I'm using Java RCP with SWT.
What I want :
I have a window, and when I click on a Button, i need a whole window to be opened. The window works perfectly and looks like this :
Window1
When I press it, a new window opens. It looks like this :
Window2 (Yup, the middle pic has its importance)
How it's currently done :
The Window 1 is a TrimmedWindow done with the Application.e4xmi, with some Parts in. The button is included in one of these parts. Here's its code :
#PostConstruct
public void postConstruct(Composite parent) {
Button b = new Button(parent, SWT.BORDER);
b.setText("Press me !");
b.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
parent.getShell().dispose();
new Game(Display.getCurrent());
}
});
}
The Window 2, as you can see, is a new class called Game. Its constructor is as follows :
public Game(Display display) {
this.display = display;
this.shell = new Shell(this.display);
this.setData();
shell.setText("I don't work properly");
shell.setMinimumSize(800, 600);
this.buildUI();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!this.display.readAndDispatch ()) this.display.sleep ();
}
this.display.dispose ();
}
What's the problem ? :
When I launch the project with Eclipse, everything goes WELL. I mean, really. I click, it opens, it loads, yay ! But the idea after this is I export the project as an executable. So I do it. Here's my .exe file. And let's start. And it doesn't work. When I press the button, nothing happens. Not even an error message, nothing.
I've found some solution that says the problem comes from the display, because RCP is single threaded. So I followed the instructions, and here's another version of the Game constructor :
public Game() {
this.display = Display.getDefault();
this.display.asyncExec(new Runnable() {
#Override
public void run() {
shell = new Shell(display);
setData();
shell.setText("I work better but...");
shell.setMinimumSize(800, 600);
buildUI();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
});
}
And so here comes the new problem : I can't call an Image after this (hence the pic I showed on Window 2). Because Image (SWT) requires a display to be constructed, and because display doesn't work well without the Runnable instance, I can't use an image after this. And I need my Image here (and also somewhere else after this).
Edit : I have an error message in that case. It says :
org.eclipse.swt.SWTException : Failed to execute runnable
(java.lang.IllegalArgumentException : Argument cannot be null)
Any solutions anyone please ?
Thanks in advance.
Kosnyru.
If you add a Trimmed Window (or just a plain Window) to the 'Windows and Dialogs' section of the Application.e4xmi with 'To Be Rendered' turned off (leave Visible on) you can then show it using:
#Inject
EModelService modelService;
#Inject
MApplication app;
MUIElement window = modelService.find("window id", app);
window.setToBeRendered(true);
Hi is there any way to create something similar to Window.alert() in GWT?
Basically I wanted to customize the Window.alert()'s "Ok" button to say something else but
as I researched there is no way to customize the alert boxes.
Thanks.
Window.alert() is already available in GWT. It opens a native dialog box which contais OK button localized by the browser's locale. This alert box can not be changed.
Use PopupPanel or DecoratedPopupPanel.
You could use the PopupPanel.
I usually code a generic dialog box, that is created once and when I need it again the html content and title is replaced. You can also add a OK/Cancel button combination, this all is rather straightforward.
private DialogBox dialog = null;
private HTML dialogHtml = new HTML();
public void onDialog(final String title, final String html) {
if (dialog == null) {
dialog = new DialogBox();
dialog.getElement().getStyle().setZIndex(99);
dialog.setWidth("500px");
dialog.setGlassEnabled(true);
dialog.setAnimationEnabled(true);
dialog.setModal(true);
VerticalPanel vp = new VerticalPanel();
vp.add(dialogHtml);
HorizontalPanel hp = new HorizontalPanel();
hp.setWidth("100%");
Button close = new Button("close");
close.setWidth("200px");
close.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
dialog.hide();
}
});
hp.add(close);
hp.setCellHorizontalAlignment(close, HasHorizontalAlignment.ALIGN_CENTER);
hp.getElement().getStyle().setMarginTop(40, Unit.PX);
vp.add(hp);
vp.setSpacing(10);
dialog.add(vp);
}
dialogHtml.setHTML(html);
dialog.setHTML(title); // the actual title
dialog.show();
dialog.center();
}
The HTML content is something very simple, i.e.
<div style="width: 500px; overflow: auto;">...</div>
for my app I need the space key to call a function independent from the focused widget, everywhere in the app but only if the according tab is opend. I found that one can add a filter to the display, like this:
getShell().getDisplay().addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(Event arg0) {
if( arg0.character == 32 ) { /**SPACE*/
if( mainTabs.getSelection().equals(analyseSoundFilesTab)) {
soundController.playButtonClickHandler();
}
}
}
});
That works fine most of the time, but if I give a button the focus via the "tab" or "shift tab", its kinda strange - the space bar will than activate a "button pressed", as if one clicks the button with the mouse. Im a bit stuck now, I don't know how to avoid this...
For the buttons, I have implemented a SelectionListener.
Regards.
You can use TraverseListener and disabled press event detection using doin field. Here is a sample code:
display.addFilter(SWT.KeyDown, new Listener() {
public void handleEvent(Event e) {
if (e.character == 32) {
System.out.printf("Space detected %s\n", e);
}
}
});
Button b1 = new Button(shell, SWT.PUSH);
b1.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent se) {
System.out.printf("Button pressed %s\n", se);
}
});
b1.addTraverseListener(new TraverseListener() {
#Override
public void keyTraversed(TraverseEvent te) {
System.out.printf("Traverse detected %s\n", te);
te.doit = true;
}
});
If addTraverseListener() didn't exist, your space button was detected after filter, so you would see "Space detected..." and after that "Button pressed...". Now that you set te.doit = true, you say to SWT to do space bar traversal (which does nothing actually) instead of firing key listener. You may optionally check te.detail to only prevent mnemonic traversals.
Choosing the 'Space key' is the real problem, because it is a general feature in most (all?) OS's that pressing space is equal to selecting the widget that has focus.
A way out would be using subclassed Button widgets that ignoring Space.
But it would confuse a lot of users, just because they expect that a focussed button is selected when they hit space and do not expect some other action.
My task is necessary and shouldn't be canceled, how do I ask ProgressMonitor not to display the "Cancel" button, so when it finishes, it will auto close the panel.
Frank
I was thinking maybe I can ask it to
return the components in it and delete
the button
Using the ProgressMonitorDemo from the Swing tutorial (linked to by BalusC) I made the following changes:
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName() ) {
int progress = (Integer) evt.getNewValue();
progressMonitor.setProgress(progress);
// Added this
AccessibleContext ac = progressMonitor.getAccessibleContext();
JDialog dialog = (JDialog)ac.getAccessibleParent();
java.util.List<JButton> components =
SwingUtils.getDescendantsOfType(JButton.class, dialog, true);
JButton button = components.get(0);
button.setVisible(false);
// end of change
String message =
String.format("Completed %d%%.\n", progress);
progressMonitor.setNote(message);
taskOutput.append(message);
if (progressMonitor.isCanceled() || task.isDone()) {
Toolkit.getDefaultToolkit().beep();
if (progressMonitor.isCanceled()) {
task.cancel(true);
taskOutput.append("Task canceled.\n");
} else {
taskOutput.append("Task completed.\n");
}
startButton.setEnabled(true);
}
}
}
You will need to download the Swing Utils class as well.
The code should only be executed once, otherwise you get a NPE when the dialog closes. I'll let you tidy that up :).
That's not possible. You can however create a custom progress monitor as outlined in this tutorial.