I have a method that displays a DirectoryDialog. But I call it within a JFrame. When I call the method below, it opens up a "Shell", which is basically like another window, and then pops up the DirectoryDialog.
How do I get rid of this JFrame-like shell. I already have a JFrame, and I want the JFrame to be the parent, not some random shell that I need to create additionally.
In other words, I need to get rid of the additional window that pops up when I call this method.
public File[] fileDialog(boolean folder){
if(folder){
Display display = new Display();
Shell shell = new Shell(display);
shell.open();
DirectoryDialog dialog = new DirectoryDialog(shell);
dialog.setFilterPath("C:\\"); // Windows specific
String file = dialog.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
if(file == null)
return new File[0];
else if(file.length() == 0)
return new File[1];
else{
File[] files = new File[1];
files[0] = new File(file);
return files;
}
}
}
Related
I used SWT browser. I opened page and there is a Button which verify that browser have option to open new browser window. Standard SWT browser have problem with it. Above is how button is defined.
<button class="btn btn-action btn-slim size-w-90pct" data-e2e="openDealerBtn" ng-if="igDefaultRowController.account.isPdSupported" ng-class="{'btn-disabled': igDefaultRowController.shouldDisableOpenPlatformButton}" ng-disabled="igDefaultRowController.shouldDisableOpenPlatformButton" ng-click="igDefaultRowController.openDealer()" ig-click-tracking="pureDealBtn-CFD" id="openDealerButton-XQ7JI"> <span class="btn-label" ig-i18n="" key="AccountOverview.openDealer"><span ng-bind-html="value">Open classic platform</span></span> </button>
[SOLVED!] How to expand SWT browser to open more than one tab ?
I used TabFolder for more tabs.
It is possible to catch URL after click on this button and open in new SWT browser tab ?
SWT uses one of the browsers that are available on the operating system and embeds the main "view" of the browser (the bit that displays the html) in your application. That does mean, however, that it doesn't come with all the fancy stuff like tabs.
As you already discovered yourself, you can get around this by using a TabFolder.
The question now is: how do you know when a tab should be opened. This code (adopted from Snippet270) should help you with this:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Main Window");
shell.setLayout(new FillLayout());
final Browser browser;
try
{
browser = new Browser(shell, SWT.NONE);
}
catch (SWTError e)
{
System.out.println("Could not instantiate Browser: " + e.getMessage());
display.dispose();
return;
}
initialize(display, browser);
shell.open();
browser.setUrl("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_links_target");
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/* register WindowEvent listeners */
static void initialize(final Display display, Browser browser)
{
browser.addOpenWindowListener(e ->
{
Shell shell = new Shell(display);
shell.setText("New Window");
shell.setLayout(new FillLayout());
Browser browser1 = new Browser(shell, SWT.NONE);
initialize(display, browser1);
e.browser = browser1;
});
browser.addVisibilityWindowListener(new VisibilityWindowListener()
{
#Override
public void hide(WindowEvent e)
{
Browser browser = (Browser) e.widget;
Shell shell = browser.getShell();
shell.setVisible(false);
}
#Override
public void show(WindowEvent e)
{
Browser browser = (Browser) e.widget;
final Shell shell = browser.getShell();
if (e.location != null) shell.setLocation(e.location);
if (e.size != null)
{
Point size = e.size;
shell.setSize(shell.computeSize(size.x, size.y));
}
shell.open();
}
});
browser.addCloseWindowListener(e ->
{
Browser browser1 = (Browser) e.widget;
Shell shell = browser1.getShell();
shell.close();
});
}
This will open the link in a new Shell with a new Browser. You can change this, so it creates a new tab and adds the new browser to the new tab.
EDIT
Here's a working example using TabFolder:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Main Window");
shell.setLayout(new FillLayout());
TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
addNewBrowser(tabFolder, "<a href='http://www.google.co.uk' target='_blank'>Click here!</a>");
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static Browser addNewBrowser(TabFolder folder, String html)
{
TabItem item = new TabItem(folder, SWT.NONE);
Composite c = new Composite(folder, SWT.NONE);
item.setControl(c);
c.setLayout(new FillLayout());
Browser browser = new Browser(c, SWT.NONE);
if (html != null)
{
browser.setText(html);
item.setText("Original tab");
}
else
{
item.setText("New tab");
}
browser.addOpenWindowListener(e ->
{
e.browser = addNewBrowser(folder, null);
});
browser.addVisibilityWindowListener(new VisibilityWindowListener()
{
#Override
public void hide(WindowEvent e)
{
Browser browser = (Browser) e.widget;
Shell shell = browser.getShell();
shell.setVisible(false);
}
#Override
public void show(WindowEvent e)
{
Browser browser = (Browser) e.widget;
final Shell shell = browser.getShell();
if (e.location != null) shell.setLocation(e.location);
if (e.size != null)
{
Point size = e.size;
shell.setSize(shell.computeSize(size.x, size.y));
}
shell.open();
}
});
browser.addCloseWindowListener(e ->
{
Browser browser1 = (Browser) e.widget;
Shell shell = browser1.getShell();
shell.close();
});
folder.setSelection(item);
return browser;
}
I have created an editor, in which I display a canvas. I would like to allow DnD for svg files.
The fact is that I allow to drop files with this part of code :
int operations = DND.DROP_COPY | DND.DROP_MOVE;
Transfer[] types = new Transfer[]{FileTransfer.getInstance()};
DropTarget dt = new DropTarget(parent, operations);
dt.setTransfer(types);
dt.addDropListener(new DropTargetAdapter() {
public void drop(DropTargetEvent event) {
//ToDo
}
But it allows any type of files, and I only want .svg files.
How could I create a condition, which will check if the file dropped is a .svg file or not, and if it is, create a new canvas with the imported file ?
I would like to recover the path of the dropped file too, any idea of how I could do that ?
I don't really know how I could restrict the imported file to svg.
Assuming that all your files have a valid .svg extension, check the file extension in the adapter and only use those files that have the right extension while ignoring files with other extensions.
Then do what you need to do with the files:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Stackoverflow");
shell.setLayout(new FillLayout());
Label label = new Label(shell, SWT.NONE);
DropTarget target = new DropTarget(label, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY);
Transfer[] transfers = new Transfer[]{FileTransfer.getInstance()};
target.setTransfer(transfers);
target.addDropListener(new DropTargetAdapter()
{
public void drop(DropTargetEvent event)
{
if (event.data == null)
{
event.detail = DND.DROP_NONE;
return;
}
String[] paths = (String[]) event.data;
List<File> files = new ArrayList<>();
for (String path : paths)
{
int index = path.lastIndexOf(".");
if (index != -1)
{
String extension = path.substring(index + 1);
if (Objects.equals(extension, "svg"))
files.add(new File(path));
}
}
System.out.println(files);
}
});
shell.pack();
shell.open();
shell.setSize(400, 300);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
I wanted to create filter in java for browse file dialogue it should allow only either abc.exe or xyz.exe
I am using swt.widgets.FileDialog
currently I am filtering for *.exe with following string
String[] extensionFilter = { "*.exe" };
fileDialog .setFilterExtensions(extensionFilter);
How can I change this to allow only only abc.exe xyz.exe?
It should search for abc.exe and xyz.exe
example: When you allow multiple extensions(.exe,.dat) it will search for all files with that(.exe,.dat) extensions similarly I want to search for abc.exe and xyz.exe
Thanks in advance
You can use setFilterExtension(String[]), however you have to know how to format the String:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Choose");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
FileDialog dialog = new FileDialog(shell);
dialog.setFilterExtensions(new String[] { "abc.exe;xyz.exe" });
System.out.println(dialog.open());
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
The single String "abc.exe;xyz.exe" tells it that both these are allowed.
Try this
String[] extensionFilter = { "abc.exe","xyz.exe" };
You can use setFilterNames
Which is explained here
It allows String type array variable only. No need to bother about regEx
String[] extensionFilter = { "abc.exe;xyz.exe" };
fileDialog.setFilterExtensions( extensionFilter );
I created an eclipse wizard which i want to test now with the SWTBot. I already used the SWTWorkbenchBot which finally works but i want to test the wizard now without the eclipse workbench. Thats why i created a shell in my testclass where i want to put on my wizardpage, but all i could see, was a empty shell without my wizardPage.
So i created a new shell class which included this code:
public static void main(String args[]) {
try {
Display display = Display.getDefault();
HorrorShell shell = new HorrorShell(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
*
* #param display
*/
public HorrorShell(Display display) {
super(display, SWT.SHELL_TRIM);
setLayout(new FillLayout());
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
ManualSettingsWizardPage page = new ManualSettingsWizardPage();
page.createControl(this);
}
With the shell class it works, my wizardpage was shown but if i try to run my testclass as SWTBotTest or as JUnitTest it wont show me anything but a empty shell.
Here's the code in my testclass:
private ManualSettingsWizardPage wizard;
private SWTBotShell botShell;
private Shell shell;
private Display display;
private SWTBot bot;
#Before
public void setUp() {
botShell = new SWTBotShell(shell);
bot = new SWTBot();
wizard = new ManualSettingsWizardPage();
display = Display.getDefault();
shell = new Shell(display);
shell.open();
shell.layout();
}
#Test
public void bot() throws Exception {
bot = botShell.bot();
shell.setBounds(200, 200, 400, 400);
shell.setLayout(new FillLayout());
wizard.createControl(shell);
}
I think your problem stems from the fact that you are creating GUI components from the SWTBot-Thread. They should be created by the UIThread, though.
Normally, you'd test some plugin which would open a wizard as the result of choosing an action, e.g. "new xyz". First step would be to put your wizard code into a plugin and register a new action that would fire up the wizard. then you could try finding the shell with SWTBot and executing the desired actions.
My try as follows,which doesn't come up with anything:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Image image = new Image(display,
"D:/topic.png");
GC gc = new GC(image);
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
gc.drawText("I've been drawn on",0,0,true);
gc.dispose();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
// TODO Auto-generated method stub
}
See the SWT-Snippets for examples. This one uses an image label
Shell shell = new Shell (display);
Label label = new Label (shell, SWT.BORDER);
label.setImage (image);
You are missing one thing in your code. Event Handler for paint. Normally when you create a component it generates a paint event. All the drawing related stuff should go in it.
Also you need not to create the GC explicitly.. It comes with the event object :)
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ImageX
{
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
shell.setLayout(new FillLayout ());
final Image image = new Image(display, "C:\\temp\\flyimage1.png");
shell.addListener (SWT.Paint, new Listener ()
{
public void handleEvent (Event e) {
GC gc = e.gc;
int x = 10, y = 10;
gc.drawImage (image, x, y);
gc.dispose();
}
});
shell.setSize (600, 400);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
if(image != null && !image.isDisposed())
image.dispose();
display.dispose ();
}
}