Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i have some doubts regarding this Code

 
M.Aamir khan
Greenhorn
Posts: 11
Netbeans IDE Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was Reading a Post about PopUpMenu and when i just look at the Code i found it so buggy so let me know if i am wrong or the Code is Really Buggy

Link to The Post


My Doubts

1. useless import of javafx.scene.effect.GaussianBlur in PopupMenuItem.java
and javafx.geometry.Orientation in PopupMenu.java

2. What does this Code means in PopupMenuItem.java


don’t you think it should be


or Simply



3. Where is “PopupMenuItem1″ in Code “TestPopup.java”


4. “stage1.setVisible(true);” in "TestPopup.java"
this Code is no more alive should be use “stage1.setVisible(true);”???
even docs have nothing like “setVisible(true)”
https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Stage.html

5. I never Seen a method Called “createPopupMenuItem()” in ArrayList
than where is Code Coming From?
flag = this.getChildren().add(popupArray.get(i).createPopupMenuItem()); in "PopupMenu.java"

6. Where is this “TestPopupControl” Coming from in
Image img =new Image(TestPopupControl.class.getResourceAsStream(“duke.jpg”)); in "TestPopup.java"
 
John Damien Smith
Ranch Hand
Posts: 298
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> i was Reading a Post about PopUpMenu and when i just look at the Code i found it so buggy so let me know if i am wrong or the Code is Really Buggy

When you just pick random code off blogs on the Internet the quality will be variable, sometimes the code will be exemplary, some times it will not, in this case it's not.
If you would like a good quality tutorial to follow for JavaFX, see the makery tutorial:
http://code.makery.ch/library/javafx-8-tutorial/
Or take a look a the official JavaFX tutorials from Oracle:
http://docs.oracle.com/javase/8/javase-clienttechnologies.htm
Or review the JavaFX source code:
http://hg.openjdk.java.net/openjfx/9-dev/rt/file/tip
Or JavaFX sample source:
http://hg.openjdk.java.net/openjfx/9-dev/rt/file/tip/apps/samples/Ensemble8
Or scene builder source:
http://hg.openjdk.java.net/openjfx/9-dev/rt/file/tip/apps/scenebuilder

Also, I recommend you follow the Java tutorials from Oracle which will explain things you have queries about like import usage and generics (skip the Swing parts as you are learning JavaFX):
https://docs.oracle.com/javase/tutorial/

> 1. useless import

Unused imports don't really matter as the compiler will just ignore them after it validates them. But, like most things in code, if it doesn't need to be there, then you may as well delete it.

> 2. event handler code

It is an old blog post. Over time conventions and capabilities change. The code was written before Java 8 lambda features were introduced to the language and is also coded to an older language standard which does not use generics. Java is built so that (almost always) old code will still compile and run on new Java editions. So the code written without generics or lambdas will still execute and run fine on a modern Java runtime. So the code is not really wrong. If you were to write the code today, you would (usually) use a lambda shorthand:

this.setOnMouseEntered(event -> {
// some code
});

or

this.setOnMouseEntered(event -> this.handleMouseEntered(event));

or

this.setOnMouseEntered(PopupMenuItem::handleMouseEntered)

where handleMouseEntered is defined as a method on PopupMenuItem:

public void handleMouseEntered(MouseEvent event) {
// some code
}

All of the above are equally valid and accomplish the same thing, which to use just depends on personal preference. Normally I'd use the inline lambda in the case that the code to handle the event is just one or two lines and the separate function for the event handler if there was more than a few lines of code required for the event handler.

> 3. Where is “PopupMenuItem1″ in Code “TestPopup.java”

It doesn't exist, the code provided is just some sample code, it is not a fully executable application.

> 4. “stage1.setVisible(true);” in "TestPopup.java"

It is an old blog post coded against a pre-release version of JavaFX 2 before the APIs were finalized.
Before finalizing the JavaFX 2 API, that API was removed and replaced with stage.show() and stage.hide().
Java policy is that once an API is released, it is pretty much there forever, but this was obviously written against a development version of the system, which is why it is not binary compatible with today's system.
I recommend only using JavaFX resources which were written in the last couple of years.

> 5. popupArray.get(i).createPopupMenuItem()

It is calling createPopupMenuItem on element i in the array. It is an array of PopupMenuItems which have a createPopupMenuItem function defined on them, so it is invoking the method on the PopupMenuItem in the array, not on the array itself.

> 6. Where is this “TestPopupControl” Coming from in

It is not in the code provided on the page because the page just contains some sample snippets, not a compilable application.

----

So having said all the above, I suggest you ignore the blog post, use more modern resources, use resources from quality sources (like Oracle, Makery or published peer-reviewed books on JavaFX).
For example, buy and study:
http://www.apress.com/9781430264606 JavaFX 8: Introduction by Example.
or Cay Horstmann's books:
http://www.horstmann.com/javaimpatient/index.html (core language)
http://www.horstmann.com/java8/index.html (java 8 features, including JavaFX)

You will learn more, more efficiently and find better examples from books like those than from studying blogs (just my opinion).

----

Some alternative methods for creating popups:
http://stackoverflow.com/questions/12717969/javafx-2-custom-popup-pane






 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic