Use eclipse and javafx scene builder to quickly build javafx applications

Source: Internet
Author: User

All those who know about javafx know that javafx has completely abandoned the previous script language since version 2.0 and is implemented only in Java. The advantage is that 1. It is possible to develop javafx using Java ide. 2. It is easier to call Java and javafx APIs. 3. It is easier to deploy javafx programs.

E (FX) Clipse is a plug-in used to develop javafx on Eclipse. In addition, you can download the full eclipse version that contains the E (FX) Clipse plug-in on the official website.

Official Website: /.

I personally feel that development on Eclipse is much more comfortable than using netbeans.

In addition, Oracle launched the javafx scene builder for visual development of javafx.

First, download javafx scene builder from the official website.

Open the program and you will see the following screen:

In the upper-left corner is the javafx control list, in the lower-left corner is the UI Layer Structure, in the middle is the visual design area, and on the right is the control property.

So, let's build a simple Notepad program!

First, use javafx scene builder to create the following interface.

This is a simple notepad interface. The above is a menubar, with a textarea in the middle used to display the opened text content. In

A contextmenu is added to textarea, that is, the context menu.

Note that FX: ID is a very important attribute. To obtain the control in XML edited by javafx scene builder at the event logic layer, you must obtain the control using FX: ID.

In addition, you must specify the event Method in the events attribute on the right.

As shown in, the open event in menu corresponds to the onmenuopen method.

In this way, a simple notepad interface is created. We saved it as study. XML in javafx scene builder.

Next, we create a javafx project in E (FX) Clipse. Remember to set the javafx SDK location in preference (similar to Android development ).

Create a class MyApp that inherits from javafx. application. application.

Import javafx. application. application; import javafx. fxml. fxmlloader; import javafx. scene. parent; import javafx. scene. scene; import javafx. stage. stage; import javafx. stage. stagestyle; public class MyApp extends application {public static void main (string [] ARGs) {application. launch (MyApp. class, argS) ;}@ override public void start (stage) throws exception {Parent Root = fxmlloader. load (getclass (). getresource ("study. fxml "); scene = new scene (root, 600,400); stage. initstyle (stagestyle. decorated); stage. setscene (scene); stage. settitle ("javafx Notepad"); stage. show ();}}

As shown in, we use the fxmlloader provided in javafx to load the edited javafx interface. Study. fxml should be placed in the same directory as the MyApp class, and the resources of this directory should be obtained through getclass (). getresource.

The above MyApp class also contains several classes in javafx, parent, scene, and stage. So what are the purposes of these classes?

Stage is the top-level container of javafx, and the original stage (that is, the parameter after the start method) is created based on the system platform (also the focus of cross-platform ). Of course, you can also create stages elsewhere in the program.

Scene is a container that includes controls and other content. The application must specify the root node of scene. You can either pass in the root node as in the above Code initialization, or set the root node through the setroot method.

Parent is the base class of all nodes that contain subnodes. It is an abstract class inherited from node. Therefore, the loader actually uses the upward transformation.

From the above explanation, we can easily know the tree structure used in javafx.

In addition, javafx uses a common reflection mechanism to completely separate the UI Layer from the event layer. View the above study. XML, you can see that the root node has a FX: controller attribute. This attribute is the class of the specified event. For example, the class for processing events in our application is test. java. Modify FX: controller = "org. Wing. javafx. project01.test" with the package name.

So, let's write down our event processing class.

Import Java. io. file; import javax. swing. joptionpane; import javafx. event. actionevent; import javafx. fxml. fxml; import javafx. scene. scene; import javafx. scene. control. textarea; import javafx. scene. layout. anchorpane; import javafx. stage. filechooser; public class test {@ fxml private anchorpane layoutpane; @ fxml private textarea filecontent; private file result; @ fxml private void onmenuopen (actionevent event) {Filechooser = new filechooser (); Result = filechooser. showopendialog (layoutpane. getscene (). getwindow (); If (result! = NULL) {filecontent. settext (filetools. readfile (result) ;}@fxml private void onmenusave (actionevent event) {If (result! = NULL) {filetools. writefile (result, filecontent. gettext () ;}@ fxml private void onmenuclose (actionevent event) {system. exit (0) ;}@ fxml private void onmenudelete (actionevent event) {filecontent. replaceselection ("") ;}@ fxml private void onmenuabout (actionevent event) {joptionpane. showmessagedialog (null, "javafx notepad is a notepad developed using javafx. "," About ", joptionpane. plain_message) ;}@ fxml private void oncontextselectall (actionevent event) {filecontent. selectall ();}}

Looking at the code above, you will find that the variables and Methods mapped to javafx are marked with @ fxml. The variable name must correspond to the FX: Id attribute of the control in study. xml. The event processing method also corresponds to the event name defined in XML.

In the menuopen event, open a file selector, obtain the selected file, read the content of the text file, and set it to textarea. For filetools, It is the class for reading temporary text files.

In the menusave event, save the content in textarea to the file you just opened.

The preceding method also calls joptionpane in swing to display the message. It can be seen that Java APIs can be easily used in today's javafx.

In addition, the following is the filetools code, which is simple for reading and writing text files.

import;import;import;import;import;public class FileTools {public static String readFile(File file) {StringBuilder resultStr = new StringBuilder();try {BufferedReader bReader = new BufferedReader(new FileReader(file));String line = bReader.readLine();while (line != null) {resultStr.append(line);line = bReader.readLine();}bReader.close();} catch (Exception e) {e.printStackTrace();}return resultStr.toString();}public static void writeFile(File file, String str) {        try {  BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));  bWriter.write(str);  bWriter.close();} catch (Exception e) {e.printStackTrace();}}}

Finally, run our javafx program.

Is it easy?

Because of the release of javafx visualization tool javafx scene builder, the emergence of E (FX) Clipse eclipse-based plug-ins allows Java programmers to quickly develop javafx, now the event layer and UI Layer are well separated, and the overall structure of the Code is clearer.

Let's look forward to the growth of javafx.

Reprinted Please note:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.