Disclaimer: I am very new to Java, but I've been building .NET applications for 13 years.
I'm trying to build this Java application that does some basic calculations for tutoring. It's honestly not that big of a program, but I can't even get it to the Hello, World! state! I have a requirement that's making it difficult:
GUI should be built using javax.swing components jButton, jLabel, jTextField, jTextArea, jFrame, and jPanel.
So, I downloaded NetBeans 7.3 and created a JavaFX in Swing application. The default code obviously works but it uses Button instead of JButton:
private void createScene() {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
fxContainer.setScene(new Scene(root));
}
so I when I change it to use a JButton I have to also change the type the root is built from. While banging my head against the wall I found an example here (not directly related) that used the JRootPane and I thought that might work in place of the StackPane. So I refactored the code like this:
private void createScene() {
JButton btn = new JButton();
btn.setText("Say 'Hello World'");
JRootPane root = new JRootPane();
root.getContentPane().add(btn);
fxContainer.setScene(new Scene(root));
}
and that code is fine except for fxContainer.setScene(new Scene(root)); because root isn't a Parent.
FYI, the application class implements JApplet and has a main and init that looks like this:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}
JFrame frame = new JFrame("Tutoring Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new TutoringCalculator();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
#Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
#Override
public void run() {
createScene();
}
});
}
How can I fulfill the requirement stated above? Am I really going about this whole thing the wrong way?
SSCCE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tutoringcalculator;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
*
* #author Owner
*/
public class TutoringCalculator extends JApplet {
private static final int JFXPANEL_WIDTH_INT = 300;
private static final int JFXPANEL_HEIGHT_INT = 250;
private static JFXPanel fxContainer;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}
JFrame frame = new JFrame("Tutoring Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new TutoringCalculator();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
#Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
#Override
public void run() {
createScene();
}
});
}
private void createScene() {
JButton btn = new JButton();
btn.setText("Say 'Hello World'");
JRootPane root = new JRootPane();
root.getContentPane().add(btn);
fxContainer.setScene(new Scene(root));
}
}
This code works either as an applet or desktop app. here:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
// <applet code=TutoringCalculator width=400 height=400></applet>
public class TutoringCalculator extends JApplet {
// The size of an applet is set by the HTML!
//private static final int JFXPANEL_WIDTH_INT = 300;
//private static final int JFXPANEL_HEIGHT_INT = 250;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Tutoring Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new TutoringCalculator();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
private JPanel swingContainer;
#Override
public void init() {
swingContainer = new JPanel(new BorderLayout());
add(swingContainer, BorderLayout.CENTER);
createScene();
}
private void createScene() {
JButton btn = new JButton();
btn.setText("Say 'Hello World'");
JRootPane root = new JRootPane();
root.getContentPane().add(btn);
swingContainer.add(root);
}
}
I could not be bothered figuring why you are using a RootPane so I left that as is.
Further tips
The spec. mentions JFrame but not JApplet. Since applets are significantly harder to develop, debug and deploy, I suggest you focus entirely on getting it working in a frame.
For frame positioning, you cannot go by setLocationByPlatform(true). See this answer for demo.
Related
i updated the code because many people doesn't understand so write a simple representation for that.
here is the problem whenever i clicked the button it open a new frame but i dont want this it does't open a new frame it remain open the same frame.
code for main frame :
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JavaProject2_27 {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaProject2_27 window = new JavaProject2_27();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JavaProject2_27() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnClicked = new JButton("Clicked");
btnClicked.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JavaProject2_28 obj=new JavaProject2_28();
obj.getJavaProject2_28();
}
});
btnClicked.setBounds(150, 99, 89, 23);
frame.getContentPane().add(btnClicked);
}
}
code for the second frame :
import java.awt.EventQueue;
import javax.swing.JFrame;
public class JavaProject2_28 {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaProject2_28 window = new JavaProject2_28();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JavaProject2_28() {
initialize();
}
public void getJavaProject2_28()
{
frame.setVisible(true);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
snapshot
I was checking your earlier program, still cannot find the implementation of getDataJavaProject2_23(); method in those three classes that you provided.
First of all answer to your question few things to mention:
Use proper naming convention, as a example: JavaProject2_28 this class name didnot make any sense to us or even for you if you look at this after a week or two.
You can divide your program for more classes, as a example: database data handling, GUI and etc.
And another important thing is multiple JFrames for one application is not good because of even if you look at your task bar when running your program you can see there is multiple icons, it is difficult to deal with your application. There is more disadvatages about this. read this to get clear idea. Instead of multiple JFrames you can use one JFrame and multiple JPanel with appropriate layouts.
Answer for your updated question:
whenever i clicked the button it open a new frame but i dont want this
it does't open a new frame it remain open the same frame.
I checked your program it is work fine(after press the button open up the other frame).
You can do some changes that are unnecessary:
Remove this function
public void getJavaProject2_28()
{
frame.setVisible(true);
}
And add frame.setVisible(true); to the initialize() method.
Solution:
Add frame.setVisible(true) to button action.
Like below:
}
});
btnClicked.setBounds(150, 99, 89, 23);
frame.getContentPane().add(btnClicked);
frame.setVisible(true);
}
And in the main frame(which is button button added) change like below:
JButton btnClicked = new JButton("Clicked");
btnClicked.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JavaProject2_28 obj=new JavaProject2_28();
}
});
Also you do not need the main() method in both if you are only open second JFrame in main frame button.
I solved It Please A look :
Parent Class:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ParentClass {
private JFrame frame;
private int Count;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ParentClass window = new ParentClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ParentClass() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnClicked = new JButton("Clicked");
btnClicked.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
Count++;
System.out.println("Count = "+Count);
if(Count==1)
{
ChildClass obj=new ChildClass();
obj.ChildClassVisibility();
}
}
});
btnClicked.setBounds(150, 99, 89, 23);
frame.getContentPane().add(btnClicked);
}
}
ChildClass:
import java.awt.EventQueue;
import javax.swing.JFrame;
public class ChildClass {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChildClass window = new ChildClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ChildClass() {
initialize();
}
public void ChildClassVisibility()
{
frame.setVisible(true);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Im trying to encapsulate the GUI calls to a single class. My window appears but the background remains the default color instead of red.
ChatProgram.java
package ChatPkg;
public class ChatProgram {
/**
* Launch the application.
*/
public static void main(String[] args) {
ChatWindow.initialize();
ChatWindow.RunWindow();
}
}
ChatWindow.java
package ChatPkg;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
public final class ChatWindow {
static JFrame frame;
/**
* Create the application.
*/
private ChatWindow() { }
public static void RunWindow() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Initialize the contents of the frame.
*/
public static void initialize() {
frame = new JFrame("Chat program");
frame.setBounds(100, 100, 450, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox chckbxCvbc = new JCheckBox("cvbc");
frame.getContentPane().add(chckbxCvbc);
// Set background color
frame.getContentPane().setBackground(Color.red);
}
}
Yes i am new to Java and none of the google results solved my problem.
You should NOT add GUI components directly to the content pane of the JFrame. Also, you shouldn't modify its properties (like you tried changing the background).
You always need a JPanel that acts as a container on which the graphical elements are added. Here is how you could write your initialize function:
public static void initialize() {
frame = new JFrame("Chat program");
frame.setBounds(100, 100, 450, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JCheckBox chckbxCvbc = new JCheckBox("cvbc");
panel.add(chckbxCvbc);
// Set background color and add panel to the Jframe
panel.setBackground(Color.RED);
frame.getContentPane().add(panel);
}
I have a simple JavaFX Application that open a Browser and shows google page. After exit the Application and free all objects, I can see that the JavaFX objects like Scene, Stage, WebView and WebEngine are alive in the heap memory after call GC.
I can see this objects with JProfiler and other Profiler tools.
This is my Test code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestMemoryLeak
{
private Stage anotherStage;
private Application app;
public void initFrame()
{
JButton btnStart = new JButton("Start");
ActionListener a1 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread javaFxThread = new Thread(new Runnable() {
#Override
public void run() {
new JFXPanel();
Platform.runLater(new Runnable() {
#Override
public void run() {
try
{
app = MyApplication.class.newInstance();
anotherStage = new Stage();
app.start(anotherStage);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
});
javaFxThread.setDaemon(true);
javaFxThread.start();
}
};
btnStart.addActionListener(a1);
JButton btnStop = new JButton("Stop");
ActionListener a2 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {
public void run() {
try
{
anotherStage.close();
anotherStage = null;
app.stop();
Platform.exit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
};
btnStop.addActionListener(a2);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(btnStart);
frame.getContentPane().add(btnStop);
frame.setTitle("Memory Leak");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
new TestMemoryLeak().initFrame();
}
}
And my JavaFX Application:
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class MyApplication extends Application {
private Scene scene;
#Override
public void start(Stage stage)
{
// create the scene
stage.setTitle("Web View");
scene = new Scene(new Browser(), 800, 600, Color.web("#666970"));
stage.setScene(scene);
stage.show();
}
}
class Browser extends Region {
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
public Browser() {
// load the web page
engine.load("http://www.google.es");
//add the web view to the scene
getChildren().add(browser);
}
#Override
protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
}
}
To test the application click on Start Button to show google web page, click on Stop Button to stop the application, run a Profiler tool, and call gc, the JavaFX classes are alive.
I am using java version "1.7.0_51" and windows 8.1
Is there something wrong in my code? Or this is the normal behavior?
Thanks for your responses.
I am working on a swing application using JavaFX controls.
in this application i have three controls buttons WebView and JTable.
on clicking on button1 table should be added on the screen and web view should be removed while on clicking on other button
table should be removed and web view should be added.
I am using the following code.
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.web.*;
import javafx.stage.Stage;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class webviewtestclass extends JFrame implements ActionListener {
JFXPanel fxpanel,fxpanel1;
static String filepath;
JTable table;
public webviewtestclass()
{
setLayout(null);
JButton b1= new JButton("OK");
JButton b2= new JButton("OKK");
add(b1);
add(b2);
b1.setBounds(20,50,50,30);
b2.setBounds(70,50,50,30);
b1.addActionListener(this);
b2.addActionListener(this);
fxpanel= new JFXPanel();
add(fxpanel);
fxpanel.setBounds(10,50,1000,800);
Object obj=new Object[50][20];
String title[]={"head1","head2"};
table=new JTable(title,obj);
add(table);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("OK"))
{
remove(fxpanel);
add(table);
}
if(ae.getActionCommand().equals("OKK"))
{
remove(table);
add(fxpanel);
}
Platform.runLater(new Runnable() {
public void run()
{
initFx(fxpanel);
}}
);
}
private static void initFx(final JFXPanel fxpanel)
{
String htmlpath="d:/lcrux/html/";
Group group= new Group();
Scene scene= new Scene(group);
fxpanel.setScene(scene);
WebView webview = new WebView ();
group.getChildren().add(webview);
webview.setMinSize(1200,800);
webview.setMaxSize(1200,800);
webview.setVisible(true);
final WebEngine eng = webview.getEngine();
File htmlFile = new File(htmlpath+filepath);
try
{
eng.load(htmlFile.toURI().toURL().toString());
}
catch(Exception ex)
{
}
}
public static void main(final String args[])
{
webviewtestclass frm=new webviewtestclass();
frm.show();
}
}
Put both components in a CardLayout to swap between them. Code using a CardLayout can be seen in this answer.
I've recently completed a project in Java,and showed it to my advisor.
Forget about the detail about the project,my final output of the program is to simply output two webpages(one is translated by Google,the other is the Chinses webpage that I retrieved).
The two statements that I wrote are:
Process p=Runtime.getRuntime().exec("cmd /c start http://news.baidu.com/ns?word="+key1+"+"+keyy+"&bt=0&et=0&si=&rn=20&tn=newsA&ie=gb2312&ct=1&cl=2&f=12");
Process r=Runtime.getRuntime().exec("cmd /c start http://translate.google.cn/translate?u=http%3A%2F%2F"+u.substring(7)+"&sl=en&tl=zh-CN&hl=zh-CN&ie=UTF-8");
They will pop up two IE windows to show the webpage.
My advisor is satisfied with my result,but he is not happy with the output format.He would like to see these two webpages shown in a GUI window instead of IE windows.(Preferably with some panel seperating the two pages in this GUI).
I am just suddenly stuck on this point,how would I put those two webpages into a GUI frame in Java (in two seperate text boxes or sth. similar).
I am using Eclipse IDE.
Please kindly help ,either with ideas or code,thanks.
You would need a web browser component for the rendering of your HTML.
See the thread here for a list of possible browser component.
You can see nice screenshots on the The DJ Project web page as well.
It depends on what GUI toolkit you are using:
Swing - see ccheneson answer
SWT/JFrame - it has it's own browser component
I have used QT Webkit to do something similar to this before and it actually worked out extremely well.
http://qt.nokia.com/doc/qtjambi-4.4/html/com/trolltech/qt/qtjambi-index.html
If you want to access an HTTPS site using this method however it's a little more difficult to get it completely working.
JavaFX is the best option.It can work together with Swing.
package com.test.swing.pro;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
public class SwingJFxBrower {
private static WebEngine engine = null;
static JTextField urlText = null;
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Swing and JavaFX : Browser");
JButton back = new JButton("<<<<");
JButton go = new JButton("Go");
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
goBack();
}
});
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadUrlCustom();
}
});
back.setPreferredSize(new Dimension(100, 25));
go.setPreferredSize(new Dimension(100, 25));
frame.add(back, BorderLayout.NORTH);
JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
urlText = new JTextField(100);
urlText.setPreferredSize(new Dimension(100, 25));
java.awt.Font font = new java.awt.Font("Courier", java.awt.Font.BOLD, 12);
urlText.setFont(font);
urlLabel.setPreferredSize(new Dimension(100, 25));
frame.add(urlLabel, BorderLayout.WEST);
frame.add(urlText, BorderLayout.CENTER);
frame.add(go, BorderLayout.EAST);
final JFXPanel jfxPanel = new JFXPanel();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
jfxPanel.setPreferredSize(new Dimension(screenSize.width - 150, screenSize.height - 200));
frame.add(jfxPanel, BorderLayout.SOUTH);
frame.setLayout(new FlowLayout());
Platform.runLater(() -> {
WebView webView = new WebView();
jfxPanel.setScene(new Scene(webView));
engine = webView.getEngine();
loadUrl();
});
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(screenSize.width - 100, screenSize.height - 150);
frame.setResizable(false);
centreWindow(frame);
}
private static void loadUrl() {
engine.load("https://duckduckgo.com/");
}
private static void loadUrlCustom() {
String url = (urlText.getText());
boolean validUrl = isValidURL(url);
if (validUrl) {
urlText.setBackground(java.awt.Color.WHITE);
urlText.setForeground(java.awt.Color.BLACK);
Platform.runLater(new Runnable() {
#Override
public void run() {
engine.load(url);
}
});
} else {
urlText.setBackground(java.awt.Color.RED);
urlText.setForeground(java.awt.Color.WHITE);
}
}
public static void goBack() {
try {
final WebHistory history = engine.getHistory();
Platform.runLater(new Runnable() {
public void run() {
try {
history.go(-1);
} catch (Exception ignore) {
}
}
});
} catch (Exception ignore) {
}
}
public static boolean isValidURL(String url) {
URL u = null;
try {
u = new URL(url);
} catch (MalformedURLException e) {
return false;
}
try {
u.toURI();
} catch (URISyntaxException e) {
return false;
}
return true;
}
public String goForward() {
final WebHistory history = engine.getHistory();
ObservableList<WebHistory.Entry> entryList = history.getEntries();
int currentIndex = history.getCurrentIndex();
Platform.runLater(new Runnable() {
public void run() {
history.go(1);
}
});
return entryList.get(currentIndex < entryList.size() - 1 ? currentIndex + 1 : currentIndex).getUrl();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
public static void centreWindow(Window frame) {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
}