Is it possible to use all the Batik components in an application developed in Eclipse RCP?
Can you please point me to relevant documentation.
Have a look at the following link:
http://sourceforge.net/projects/svgplugin/
Also you can use the SWT/AWT Bridge. See the SWT Snippet Page.
>> SWT/AWT & Batik Sample Code
import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.apache.batik.swing.JSVGCanvas;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class BatikTest
{
public static void main(String[] args)
{
// Uncomment the below lines and set proper values if you are behind a proxy server
///System.setProperty("http.proxyHost", "");
///System.setProperty("http.proxyPort", "");
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(200, 120);
shell.setText("SWT Batik Example");
shell.setLayout(new GridLayout());
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite composite = new Composite(shell, SWT.EMBEDDED);
composite.setLayout(new GridLayout());
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
java.awt.Frame locationFrame = SWT_AWT.new_Frame(composite);
locationFrame.add(createComponents(new File("batik3D.svg")));
locationFrame.pack();
//shell.pack();
shell.open();
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
private static JComponent createComponents(File f)
{
// Create a panel and add the button, status label and the SVG canvas.
final JPanel panel = new JPanel(new BorderLayout());
JSVGCanvas svgCanvas = new JSVGCanvas();
panel.add("Center", svgCanvas);
try {
svgCanvas.setURI(f.toURI().toURL().toString());
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return panel;
}
}
>>Output
It is possible to use batik in eclipse RCP apps, as e4 uses the CSS engine. See http://www.eclipse.org/orbit for the latest stable build that includes a number of the batik bundles. Ex, in our RCP app we use the following + some supporting w3c bundles:
org.apache.batik.css_1.6.0.v201011041432.jar
org.apache.batik.util_1.6.0.v201011041432.jar
org.apache.batik.util.gui_1.6.0.v201011041432.jar
org.apache.batik.xml_1.6.0.v201011041432.jar
Related
I am using SWT to create an application GUI, and I don't really need to resize the components, but it does bother me that when the window is maximized, the components stay left-aligned. Is there a way to fix this with SWT or do I need to utilize a different set of GUI tools?
Thanks in advance. I am using SWT 4.8 for this application.
EDIT: Images
Small: https://imgur.com/CPbAlaZ
Maximized: https://imgur.com/4d6YXcl
Provided images are a basic application using the following code
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class TestWindow {
protected Shell shlSwtApplicationExample;
private Text text;
/**
* Launch the application.
* #param args
*/
public static void main(String[] args) {
try {
TestWindow window = new TestWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlSwtApplicationExample.open();
shlSwtApplicationExample.layout();
while (!shlSwtApplicationExample.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlSwtApplicationExample = new Shell();
shlSwtApplicationExample.setSize(705, 529);
shlSwtApplicationExample.setText("SWT Application Example");
Composite composite = new Composite(shlSwtApplicationExample, SWT.NONE);
composite.setBounds(10, 10, 669, 465);
text = new Text(composite, SWT.BORDER);
text.setBounds(22, 10, 334, 295);
Button btnNewButton = new Button(composite, SWT.NONE);
btnNewButton.setBounds(49, 384, 137, 26);
btnNewButton.setText("New Button");
Button button = new Button(composite, SWT.NONE);
button.setText("New Button");
button.setBounds(300, 384, 137, 26);
}
}
I would not recommend using setBounds since it does not resize the components when you resize the application. Use Layouts, like for example below I have used GridLayout for both the Shell and the Composite which will properly arrange the UI when resize happens.
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class TestWindow {
protected Shell shlSwtApplicationExample;
private Text text;
/**
* Launch the application.
* #param args
*/
public static void main(String[] args) {
try {
TestWindow window = new TestWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents(display);
shlSwtApplicationExample.open();
shlSwtApplicationExample.layout();
shlSwtApplicationExample.setLayout(new GridLayout(1, false));
while (!shlSwtApplicationExample.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
* #param display
*/
protected void createContents(Display display) {
shlSwtApplicationExample = new Shell(display);
shlSwtApplicationExample.setLayout(new GridLayout(1, false));
Composite txtcomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
txtcomposite.setLayout(new GridLayout(1, false));
txtcomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite btncomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
btncomposite.setLayout(new GridLayout(2, false));
btncomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
text = new Text(txtcomposite, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnNewButton = new Button(btncomposite, SWT.NONE);
btnNewButton.setText("New Button");
Button button = new Button(btncomposite, SWT.NONE);
button.setText("New Button");
shlSwtApplicationExample.setText("SWT Application Example");
//shlSwtApplicationExample.setSize(705, 529);
}
}
Since you are using setBounds you will need to add a Control listener to the shell to be told about resize and move events. You will then have to recalculate the positions on each resize event.
shlSwtApplicationExample.addControlListener(
new ControlListener() {
#Override
public void controlMoved(ControlEvent event) {
// No action
}
#Override
public void controlResized(ControlEvent event) {
Rectangle rect = shlSwtApplicationExample.getClientArea();
// TODO Call new `setBounds` on each control based on the
// client area size
}
});
This might be a good time to learn about using Layouts instead of setBounds (see here). Layouts will automatically deal with resizes.
I have some code snipped out of a much bigger app, which renders some white text on a black background in a JavaFX WebView. The background colour of the page is set to transparent, using some code from Transparent background in the WebView in JavaFX
import java.lang.reflect.Field;
import org.w3c.dom.Document;
import com.sun.webkit.WebPage;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class TestWebView extends Application {
public void start(Stage stage) throws Exception {
StackPane stackpane = new StackPane();
Scene scene = new Scene(stackpane, stage.getWidth(), stage.getHeight(), Color.BLACK);
stage.setScene(scene);
scene.setFill(Color.BLACK);
stackpane.setStyle("-fx-background-color: BLACK");
WebView webview = new WebView();
stackpane.getChildren().add(webview);
WebEngine webengine = webview.getEngine();
webengine.documentProperty().addListener(new WebDocumentListener(webengine));
webengine.loadContent("<p style='color:white'>Hello World</p>");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
protected class WebDocumentListener implements ChangeListener<Document> {
private final WebEngine wdl_webEngine;
public WebDocumentListener(WebEngine webEngine) {
wdl_webEngine = webEngine;
}
#Override
public void changed(ObservableValue<? extends Document> arg0, Document arg1, Document arg2) {
try {
Field f = wdl_webEngine.getClass().getDeclaredField("page");
f.setAccessible(true);
com.sun.webkit.WebPage page = (WebPage) f.get(wdl_webEngine);
page.setBackgroundColor((new java.awt.Color(0, 0, 0, 0)).getRGB());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Testing on MacOS 10.11.6, with Oracle's JDK:
With JDK 1.8.0_152, this code works nicely - I get white text on black. (And the transparency works too when I layer things underneath it in the stackpane)
With JDK 9 (9+181), com.sun.webkit.WebPage is no longer accessible, so I have to compile and run it with --add-exports javafx.web/com.sun.webkit=ALL-UNNAMED - but having done that, I get black text on a black screen. I can tell the text is there by selecting the text and dragging it, which makes the text appear white while being dragged.
Ideally, I'd like to keep a single codebase that works for both JDK 8 and 9. (Java's usually been good to me with backward compatibility). Or as a second best, how do I get the white text I'm expecting in JDK 9?
Can anyone point me in the right direction? Many thanks in advance.
I had the same issue, I solved it by going further in the reflective process :
Field f = webEngine.getClass().getDeclaredField("page");
f.setAccessible(true);
Object page = f.get(webEngine);
Method m = page.getClass().getMethod("setBackgroundColor", int.class);
m.setAccessible(true);
m.invoke(page, (new java.awt.Color(0, 0, 0, 0)).getRGB());
I have a problem when I try to use a coolBar in a composite and then I embed this composite in an application. The coolBar simply doesn't appear. This problem doesn't occours with another tools, like toolBar and other composites. What can I doing wrong or forgetting?
Before following the code, I refer my system:
Win7
Eclipse:Version: Indigo Service Release 2 Build id: 20120216-1857
Google WindowBuilder 1.5.0 Google
Plugin 3.1.0
SWT Designer 1.5.0
Google Web Toolkit 2.4.0
Composite code:
package xx.xxx.xx.pcommJavaGUI.composites;
import org.eclipse.swt.widgets.Composite;
public class TestComposite extends Composite {
public TestComposite(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(1, false));
CoolBar coolBar = new CoolBar(this, SWT.FLAT);
CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
Button btnTest = new Button(coolBar, SWT.NONE);
coolItem.setControl(btnTest);
btnTest.setText("Test");
Tree tree = new Tree(this, SWT.BORDER);
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
}
#Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
And the application Window code:
package xx.xxx.xx.pcommJavaGUI.composites;
import org.eclipse.swt.SWT;
public class TestApplication {
protected Shell shell;
public static void main(String[] args) {
try {
TestApplication window = new TestApplication();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(1, false));
TestComposite tc = new TestComposite(shell, SWT.NONE);
GridData gd_tc = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
tc.setLayoutData(gd_tc);
}
}
Thanks for helping.
You have to set the size of CoolItem manually.
First of all pack(); your Button to set it to it's default size.
Afterwards set the size of the CoolItem to the size of the Button.
The Button:
Button btnTest = new Button(coolBar, SWT.NONE);
coolItem.setControl(btnTest);
btnTest.setText("Test");
// If you do not call this, btnTest.getSize() will give you x=0,y=0.
btnTest.pack();
Set the size of CoolItem:
Point size = btnTest.getSize();
coolItem.setControl(btnTest);
coolItem.setSize(coolItem.computeSize(size.x, size.y));
Links:
CoolBar Examples
API: Control.pack();
It might be just because you aren't setting layout data for the coolbar. See this article to understand how layouts work.
Is there a way to use Firebug lite "bookmarklet" feature within eclipse SWT browser?
Depends on the system browser which your SWT browser is using. For Win7 and IE8, you can have something like this:
Output
Code
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Shell;
public class FirebugLite
{
public static void main(String[] args) {
new FirebugLite().start();
}
public void start()
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData.widthHint = SWT.DEFAULT;
gridData.heightHint = SWT.DEFAULT;
shell.setLayoutData(gridData);
shell.setText("Firebug Lite for SWT ;)");
final Browser browser = new Browser(shell, SWT.NONE);
GridData gridData2 = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData2.widthHint = SWT.DEFAULT;
gridData2.heightHint = SWT.DEFAULT;
browser.setLayoutData(gridData2);
Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
button.setText("Install");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
browser.setUrl("javascript:(function(F,i,r,e,b,u,g,L,I,T,E){if(F.getElementById(b))return;E=F[i+'NS']&&F.documentElement.namespaceURI;E=E?F[i+'NS'](E,'script'):F[i]('script');E[r]('id',b);E[r]('src',I+g+T);E[r](b,u);(F[e]('head')[0]||F[e]('body')[0]).appendChild(E);E=new%20Image;E[r]('src',I+L);})(document,'createElement','setAttribute','getElementsByTagName','FirebugLite','4','firebug-lite.js','releases/lite/latest/skin/xp/sprite.png','https://getfirebug.com/','#startOpened');");
}
});
browser.setUrl("http://stackoverflow.com/questions/12003602/eclipse-swt-browser-and-firebug-lite");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Note >> I have used the setUrl() API. You can try the execute() but I am not sure whether it would work.
I can not run on Linux. The exception web page appeared: 'URL cannot be shown'.
I tweaked Favonius's solution a bit. In my case we wanted to see inside iframes. I modified the setUrl to load firebug inside the last iframe. In my case it did what we wanted.
browser.setUrl("javascript: function lastIframeDocument(curr){while(curr.getElementsByTagName('iframe')[0]!=null){curr=curr.getElementsByTagName('iframe')[0].contentWindow.document;}return curr;}(function(F,i,r,e,b,u,g,L,I,T,E){if(F.getElementById(b))return;E=F[i+'NS']&&F.documentElement.namespaceURI;E=E?F[i+'NS'](E,'script'):F[i]('script');E[r]('id',b);E[r]('src',I+g+T);E[r](b,u);(F[e]('head')[0]||F[e]('body')[0]).appendChild(E);E=new%20Image;E[r]('src',I+L);})(lastIframeDocument(document),'createElement','setAttribute','getElementsByTagName','FirebugLite','4','firebug-lite.js','releases/lite/latest/skin/xp/sprite.png','https://getfirebug.com/','#startOpened');");
How to implement something kinda internal frame in JavaFx 2.0 specifically?
My attempt is as so..
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
ConnectDb connection;
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) throws Exception {
final Stage stage1 = new Stage();
StackPane pane = new StackPane();
Button btn = new Button("Click Me");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
connection = new ConnectDb();
try {
connection.start(stage1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Fire some thing..");
}
});
pane.getChildren().add(btn);
stage.setScene(new Scene(pane ,200, 300));
stage.show();
}
}
ConnectDb.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ConnectDb extends Application {
#Override
public void start(Stage stage) throws Exception {
StackPane pane = new StackPane();
Button btn = new Button("Click On Button which is me");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Something here..");
}
});
pane.getChildren().add(btn);
stage.setScene(new Scene(pane ,200, 300));
stage.show();
}
}
First of all, for your approach, you don't really need to (and therefore should not) extend ConnectDb from Application as you just use the start method to create new stages. You just need one Application class (in your case Main). You could just as well create the new stage/scene in your first event handler.
Secondly, there is no real MDI support in JavaFX 2.1. Right now, you can just have multiple Stages (which is the equivalent to having multiple windows/frames). But you cannot have something like an internal frame in a desktop pane.
I guess you could take the following actions:
Just use multiple Stages (windows) with the drawback that they will float quite uninspiredly on your desktop
Use Swing as a container (with JDesktopPane and JInternalFrame) and integrate JavaFX (here's a nice How-To)
Implement your own framework that emulates MDI behavior
Find a framework that provides MDI behavior
Wait for a future release of JavaFX that hopefully provides MDI support (as far as I know, there's a change request pending...)
Create parent AncorPane.
Add several children AnchorPanes to it. They will serve as internal frames. Add different content to these.
Set children AnchorPanes invisible.
Add buttons to hide, resize or close children AnchorPanes. When needed, call function to set all children AnchorPanes invisible, except for one.