Quickly build JavaFX applications with Eclipse and JavaFX Scene Builder

Source: Internet
Author: User
Tags readfile

http://blog.csdn.net/wingfourever/article/details/7726724

quickly build JavaFX applications with Eclipse and JavaFX Scene builder2012-07-08 18:25 18641 people read reviews (one) collection report Classification:JavaFX (*)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Knowing that JavaFX has been known, JavaFX has completely abandoned the previous script language since the 2.0 release, and is only implemented in pure Java. The advantage is that 1. Making it possible to develop JavaFX with the Java IDE, 2.Java and JavaFX APIs make it easier to deploy 3.JavaFX programs with each other.

E (FX) Clipse is a plugin on eclipse that is used to develop JavaFX. Furthermore, the full version of Eclipse containing the E (FX) Clipse plugin can be downloaded on the official website.

The official website is: http://www.efxclipse.org/.

In my personal sense, it's much more comfortable to develop on eclipse than to use NetBeans.

In addition, Oracle has launched JavaFX Scene Builder for the visual development of JavaFX.

First we download JavaFX Scene Builder from the official website.

To open the program, you can see the following screen:

, the top left is the JavaFX control list, the bottom left is the UI layer structure, the middle is the visual design area, and 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 the interface of a simple notepad. Above is a menubar, in the middle is a textarea used to display open text content. In

A ContextMenu is added to the textarea, the so-called right-click menu.

One thing to note here is that Fx:id is a very important attribute that needs to be obtained through fx:id in order to get the controls in the XML that JavaFX Scene Builder edited in the event logic layer.

Also, specify the method of the event in the events of the right property.

As shown, 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 new JavaFX Project in E (FX) Clipse. Remember to set the location of the JavaFX SDK (similar to Android development) in preference.

Create a class of MyApp that inherits from Javafx.application.Application.

[Java]View PlainCopy
  1. Import javafx.application.Application;
  2. Import Javafx.fxml.FXMLLoader;
  3. Import javafx.scene.Parent;
  4. Import Javafx.scene.Scene;
  5. Import Javafx.stage.Stage;
  6. Import Javafx.stage.StageStyle;
  7. Public class MYAPP extends application {
  8. public static void Main (string[] args) {
  9. Application.launch (MyApp).   class, args);
  10. }
  11. @Override
  12. public void Start (stage stage) throws Exception {
  13. Parent root = Fxmlloader.load (GetClass (). GetResource ("Study.fxml"));
  14. Scene scene = new Scene (Root, 400);
  15. Stage.initstyle (stagestyle.decorated);
  16. Stage.setscene (Scene);
  17. Stage.settitle ("JavaFX notepad");
  18. Stage.show ();
  19. }
  20. }

As shown, we use the Fxmlloader provided in JavaFX to load our edited JavaFX interface. Study.fxml should be placed in the same directory as the MyApp class, with GetClass (). GetResource () to get the resources for the class directory.

The above MyApp class also appears in several JavaFX classes, Parent, Scene, Stage. So what is the use of these classes?

The stage is the topmost container of JavaFX, and the original stage (the parameter after the Start method) is created based on the system platform (which is also a cross-platform focus). Of course, you can also create stage in other parts of the program.

Scene is a container for everything, including controls, and the application must specify the root node of the scene. You can either pass in the root node as it was initialized in the code above, or you can set the root node by using the Setroot method.

The parent is the base class for all nodes that contain child nodes. It is an abstract class that inherits from node. So the loader is actually used in the upward transformation.

From the above explanation, it is easy to know the tree structure used in JavaFX.

In addition, JavaFX uses a very common reflection mechanism to completely separate the UI layer from the event layer. Looking at the study.xml above, you can see that the root node has a Fx:controller property. This property is the class that specifies the event handling. For example, we now have the class for handling events in the application as Test.java. Then modify Fx:controller = "Org.wing.javafx.project01.Test" before the package name.

So, let's write our event-handling class below.

[Java]View PlainCopy
  1. Import Java.io.File;
  2. Import Javax.swing.JOptionPane;
  3. Import javafx.event.ActionEvent;
  4. Import Javafx.fxml.FXML;
  5. Import Javafx.scene.Scene;
  6. Import Javafx.scene.control.TextArea;
  7. Import Javafx.scene.layout.AnchorPane;
  8. Import Javafx.stage.FileChooser;
  9. Public class Test {
  10. @FXML
  11. private Anchorpane Layoutpane;
  12. @FXML
  13. private TextArea filecontent;
  14. private File result;
  15. @FXML
  16. private void Onmenuopen (ActionEvent event) {
  17. Filechooser Filechooser = new Filechooser ();
  18. result = Filechooser.showopendialog (Layoutpane.getscene (). GetWindow ());
  19. if (Result! = null) {
  20. Filecontent.settext (Filetools.readfile (result));
  21. }
  22. }
  23. @FXML
  24. private void Onmenusave (ActionEvent event) {
  25. if (Result! = null) {
  26. Filetools.writefile (result, Filecontent.gettext ());
  27. }
  28. }
  29. @FXML
  30. private void Onmenuclose (ActionEvent event) {
  31. System.exit (0);
  32. }
  33. @FXML
  34. private void Onmenudelete (ActionEvent event) {
  35. Filecontent.replaceselection ("");
  36. }
  37. @FXML
  38. private void Onmenuabout (ActionEvent event) {
  39. Joptionpane.showmessagedialog (null, "JavaFX Notepad is a notepad developed using JavaFX.  ","about ", joptionpane.plain_message);
  40. }
  41. @FXML
  42. private void Oncontextselectall (ActionEvent event) {
  43. Filecontent.selectall ();
  44. }
  45. }

Looking at the code above, you will find that the variables and methods that are mapped to JavaFX are @fxml labeled. The name of the variable needs to correspond to the Fx:id property of the control in Study.xml. The method of event handling is also the name of the event defined in the corresponding XML.

Opens a file selector in the Menuopen event, then gets the selected file, reads the contents of the text file, and finally sets it to textarea. As for Filetools, it is the class that is read temporarily under the text file.

In the Menusave event, save the contents of textarea to the file you just opened.

The above also invokes the method of Joptionpane in swing to display the message. This shows that the Java Api can be easily used in today's JavaFX.

In addition, the following is the Filetools code, very simple text file read and write.

[Java]View PlainCopy
  1. Import Java.io.BufferedReader;
  2. Import Java.io.BufferedWriter;
  3. Import Java.io.File;
  4. Import Java.io.FileReader;
  5. Import Java.io.FileWriter;
  6. Public class Filetools {
  7. public static String readFile (file file) {
  8. StringBuilder resultstr = new StringBuilder ();
  9. try {
  10. BufferedReader Breader = new BufferedReader (new FileReader (file));
  11. String line = Breader.readline ();
  12. while (line! = null) {
  13. Resultstr.append (line);
  14. line = Breader.readline ();
  15. }
  16. Breader.close ();
  17. } catch (Exception e) {
  18. E.printstacktrace ();
  19. }
  20. return resultstr.tostring ();
  21. }
  22. public static void WriteFile (file file, String str) {
  23. try {
  24. BufferedWriter bwriter = new BufferedWriter (new FileWriter (file));
  25. Bwriter.write (str);
  26. Bwriter.close ();
  27. } catch (Exception e) {
  28. E.printstacktrace ();
  29. }
  30. }
  31. }

Finally, let's run our JavaFX program.

Isn't it simple?

Since JavaFX Scene Builder's launch of this JavaFX visualizer, E (FX) Clipse, the advent of Eclipse-based plug-ins, allows Java programmers to quickly develop JavaFX, and now separates the event and UI layers very well. , the overall structure of the code is also clearer.

Let's look forward to the growth of JavaFX.

Reprint Please specify: http://blog.csdn.net/ml3947

Quickly build JavaFX applications with Eclipse and JavaFX Scene Builder

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: info-contact@alibabacloud.com 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.