Adding a TilePane instantiated in .java files to FXML - javafx-2

I'm trying to add a TilePane with ImageView children to a scene in JavaFX. Currently, my FXML is loading an empty TilePane.
The current FXML line that i have making the TilePane is
<TilePane id="MapPane" fx:id="mapPane" layoutX="3.0" layoutY="0.0" prefColumns="9" prefHeight="560.0" prefTileHeight="112.0" prefTileWidth="112.0" prefWidth="1277.0" visible="true"\>
where mapPane is the name of the variable in my .java file
controller:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package screens.gameScreen;
import screens.*;
import mule.*;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.*;
import java.awt.MouseInfo;
import java.awt.Point;
import com.sun.glass.ui.Robot;
/**
* FXML Controller class
*
* #author Stephen
*/
public class GameScreenController implements Initializable, ControlledScreen {
Robot robot = com.sun.glass.ui.Application.GetApplication().createRobot();
ScreenManager screenManager;
TileEngine tileEngine = new TileEngine();
#FXML
TilePane mapPane = tileEngine.createRandomMap(true);;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#Override
public void setScreenParent(ScreenManager screen) {
screenManager = screen;
}
#FXML
private void goToMain(ActionEvent event) {
screenManager.setScreen(mule.MULE.mainMenuScreenID);
}
}

you can use SceneBuilder for create FXML. This can easily solve your problem.

Related

Overriding translations from admin-web in Liferay 7

Thera are some tranlations in com.liferay.plugins.admin.web or com.liferay.portal.instances.web module which I'd like to override. With other modules I've followed succesfully this tutorial:
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/overriding-a-modules-language-keys
In this case, com.liferay.plugins.admin.web module has no servlet.context.name, which is required in class properties. Is there any way to override this tranlations? Thanks for help in advance!
The best solution is to create a translation module which extends from ResourceBundle:
package com.galian.extranet.resourcebundle;
import com.liferay.portal.kernel.language.UTF8Control;
import java.util.Enumeration;
import java.util.ResourceBundle;
import org.osgi.service.component.annotations.Component;
/**
* #author
*
*/
#Component(immediate = true, property = { "language.id=en_US" }, service = ResourceBundle.class)
public class DefaultCustomResourceBundle extends ResourceBundle {
#Override
public Enumeration<String> getKeys() {
return _resourceBundle.getKeys();
}
#Override
protected Object handleGetObject(String key) {
return _resourceBundle.getObject(key);
}
private final ResourceBundle _resourceBundle = ResourceBundle.getBundle("content.Language", UTF8Control.INSTANCE);
}
Your module project structure will be like this:

How do I 'initialize' my graphics?

My friend has make me a simple JFrame, and I am meant to work on it and develop it. I have encountered a problem within the first 30 mins of having it: it's not drawing the graphics!!
Here is the code that draws the graphics, and the code from the class that brings up the JFrame.
Thanks in advance.
GameCanvas.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.LinkedList;
/**
*
* #author Kris
*/
public class GameCanvas extends Canvas{
Player p;
LinkedList<Stalker> s = new LinkedList<>();
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
p.paintItAll(g2d);
for(int i = 0; i < s.size(); i++) {
s.get(i).paintItAll(g2d);
}
}
public GameCanvas() {
initComponents();
repaint();
}
public void initComponents() {
p = new Player(new Point(50,50));
s.add(new Stalker(new Point(50,100)));
}
public Point getPlayerPos() {
return p.getPos();
}
}
MainWindow.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* #author Kris
*/
public class MainWindow extends JFrame{
static Toolkit tk = Toolkit.getDefaultToolkit();
static int xSize = ((int) tk.getScreenSize().getWidth());
static int ySize = ((int) tk.getScreenSize().getHeight());
/**
* #param args the command line arguments
*/
public MainWindow() {
super("STALKER!!!!!!!!!!");
GameCanvas canvas = new GameCanvas();
add(canvas, BorderLayout.CENTER);
setSize(xSize, ySize);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
canvas.createBufferStrategy(2);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow();
}
});
}
}
You are using the old AWT Canvas class as the base component. This class follows the AWT painting mechanism, where there is no paintComponent method and you were supposed to override paint to do custom graphics.
Change the base class to JComponent and you can use the newer painting mechanism:
...
import javax.swing.JComponent;
public class GameCanvas extends JComponent {
This article compares how painting works in AWT and Swing if you want to know more about them.

javafx html editor

actually i'm looking for something very similar to this thread:
How to hide the controls of HTMLEditor?
so basically i try to add a custom button to the javafx html editor but with the difference that it's implemented through FXML.
So my question is:
Is there a "work-around" to add custom buttons to the html-editor when it's implemented through FXML?
Sample solution is :
htmlEditor.setVisible(false);
Platform.runLater(new Runnable() {
#Override
public void run() {
Node[] nodes = htmlEditor.lookupAll(".tool-bar").toArray(new Node[0]);
for (Node node : nodes) {
node.setVisible(false);
node.setManaged(false);
}
htmlEditor.setVisible(true);
}
});
I have modified the #jewelsea answer for javaFX9.
I have also added some customization to move toolbars. The main idea is to get all the components by css selector, then modify or hide them. Read the class HTMLEditorSkin to get the CSS classes names, like ".html-editor-align-center" for the align button.
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class HTMLEditorCustomizationSample2 extends Application {
// limits the fonts a user can select from in the html editor.
private static final ObservableList<String> limitedFonts = FXCollections.observableArrayList("Arial",
"Times New Roman", "Courier New", "Comic Sans MS");
private HTMLEditor htmlEditor;
public static void main(String[] args) {
launch(args);
}
#SuppressWarnings("unchecked")
#Override
public void start(Stage stage) {
htmlEditor = new HTMLEditor();
stage.setScene(new Scene(htmlEditor));
stage.show();
customizeEditor(htmlEditor);
}
private void customizeEditor(HTMLEditor htmlEditor) {
// hide controls we don't need.
Node seperator = htmlEditor.lookup(".separator");
seperator.setVisible(false);
seperator.setManaged(false);
hideByClass(htmlEditor, ".separator");
hideByClass(htmlEditor, ".html-editor-cut", ".html-editor-copy", ".html-editor-paste", ".html-editor-strike",
".html-editor-hr");
hideByClass(htmlEditor, ".html-editor-align-left"
, ".html-editor-align-center"
, ".html-editor-align-right"
, ".html-editor-align-justify", ".html-editor-outdent"
, ".html-editor-indent", ".html-editor-bullets"
, ".html-editor-numbers");
// Move the toolbars
Node top= htmlEditor.lookup(".top-toolbar");
GridPane.setConstraints(top,1,0,1,1);
Node bottom= htmlEditor.lookup(".bottom-toolbar");
GridPane.setConstraints(bottom,0,0,1,1);
Node web= htmlEditor.lookup("WebView");
GridPane.setConstraints(web,0,1,2,1);
// modify font selections.
int i = 0;
Set<Node> fonts = htmlEditor.lookupAll(".font-menu-button");
Iterator<Node> fontsIterator = fonts.iterator();
fontsIterator.next();
ComboBox<String> formatComboBox = (ComboBox<String>) fontsIterator.next();
formatComboBox.itemsProperty().addListener((obs, old, value) -> {
if (value.size() != limitedFonts.size()) {// should loop on array for equality
Platform.runLater(() -> {
value.clear();
// stop.set(true);
value.addAll(limitedFonts);
formatComboBox.setValue(limitedFonts.get(0));
});
}
});
// add a custom button to the top toolbar.
Node node = htmlEditor.lookup(".top-toolbar");
if (node instanceof ToolBar) {
ToolBar bar = (ToolBar) node;
ImageView graphic = new ImageView(
new Image("http://bluebuddies.com/gallery/title/jpg/Smurf_Fun_100x100.jpg", 16 , 16, true, true));
graphic.setEffect(new DropShadow());
Button smurfButton = new Button("", graphic);
bar.getItems().add(smurfButton);
smurfButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
htmlEditor.setHtmlText("<font face='Comic Sans MS' color='blue'>Smurfs are having fun :-)</font>");
}
});
}
}
private void hideByClass(HTMLEditor htmlEditor, String... selectors) {
for (String selector : selectors) {
Set<Node> nodes = htmlEditor.lookupAll(selector);
for (Node node : nodes) {
node.setVisible(false);
node.setManaged(false);
}
}
}
#Override
public void stop() throws Exception {
super.stop();
System.out.println(htmlEditor.getHtmlText());
}
}
Here is some sample code which customizes the HTMLEditor and adds a custom button to it. The sample code does not use fxml but really it's very similar if fxml is used. You could define the HTMLEditor in fxml and inject it into your Controller using the standard #FXML annotation. Once you have a reference to the editor, customize it in Java code using an appropriate variation of the sample code. For the added button, just create it in Java rather than fxml and it will be simpler.

Dialling a phone number by platformrequest

I have tried for two days to write a code that dial a phone number , but I failed
I have written a midlet and a form called main as a Displayable the form contains a textfield and
command .
When the application starts up the form should appears and calls the dialed number written in
textField when the command pressed.
the dialing process didn't work with me .
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable.*;
import javax.microedition.lcdui.Form.*;
import javax.microedition.midlet.MIDlet;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class DiallNumber extends MIDlet implements CommandListener
{
String num;
private Form main;
private Command call,dial;
private TextField phoneField;
Display display;
public DiallNumber()
{
main = new Form("main");
phoneField = new TextField("label","",10,phoneField.PHONENUMBER);
call = new Command("call",call.OK,0);
main.append(phoneField);
main.addCommand(call);
num=this.phoneField.getString().trim();
main.setCommandListener(this);
}
/**}
* From MIDlet. Called when the MIDlet is started.
*/
public void startApp()
{
// The initial display is the first form
display = Display.getDisplay(this);
display.setCurrent(main);
}
public void call( String number) {
try {
platformRequest("tel:"+number);
} catch (ConnectionNotFoundException ex) {
// TODO: Exception handling
}
}
public void commandAction(Command c, Displayable d){
if(d==main)
{
if(c==call)
call(""+num);
}
}
public void pauseApp()
{
// No implementation required
}
/*
* /
*/
/**
* From MIDlet. Called to signal the MIDlet to terminate.
*
* #param unconditional
* whether the MIDlet has to be unconditionally terminated
*/
public void destroyApp(boolean unconditional)
{
// No implementation required
}
/**
* From CommandListener. Called by the system to indicate that a command has
* been invoked on a particular displayable.
*
* #param command
* the command that was invoked
* #param displayable
* the displayable where the command was invoked
*/
}
My log trace
Copying 1 file to C:\Users\ELHADI\Documents\NetBeansProjects\DiallNumber2\dist\nbrun5217990045006831680
Copying 1 file to C:\Users\ELHADI\Documents\NetBeansProjects\DiallNumber2\dist\nbrun5217990045006831680
Jad URL for OTA execution: http://localhost:8082/servlet/org.netbeans.modules.mobility.project.jam.JAMServlet/C%3A/Users/ELHADI/Documents/NetBeansProjects/DiallNumber2/dist//DiallNumber2.jad
Starting emulator in execution mode
Installing suite from: http://127.0.0.1:49320/DiallNumber2.jad
[WARN] [rms ] javacall_file_open: _wopen failed for: C:\Users\ELHADI\javame-sdk\3.0\work\0\appdb\_delete_notify.dat
I have solved the problem
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable.*;
import javax.microedition.lcdui.Form.*;
import javax.microedition.midlet.MIDlet;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class DiallNumber extends MIDlet
{
private Display display;
private MIDlet midlet;
Form f = new form("f");
/**}
* From MIDlet. Called when the MIDlet is started.
*/
public void startApp()
{
display = Display.getDisplay(this);
display.setCurrent(f);
}
public void pauseApp()
{
// No implementation required
}
/*
* /
*/
/**
* From MIDlet. Called to signal the MIDlet to terminate.
*
* #param unconditional
* whether the MIDlet has to be unconditionally terminated
*/
public void destroyApp(boolean unconditional)
{
// No implementation required
}
class form extends Form implements CommandListener
{
private Command call;
private TextField phoneField;
private String n;
public form(String title )
{
super(title);
call =new Command("call",call.OK,0);
this.phoneField =new TextField("number","",20,phoneField.PHONENUMBER);
addCommand(call);
append(this.phoneField);
this.setCommandListener(this);
}
public void commandAction(Command c,Displayable d)
{
if(c==call)
{
try
{
n=phoneField.getString().trim();
platformRequest("tel:"+n);
}
catch(javax.microedition.io.ConnectionNotFoundException e)
{e.printStackTrace();}
}
}
}
}

ContextMenu not working on JavaFX2 TextArea?

This is not working for me, the context menu doesn't get displayed:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/**
*
* #author Alvaro
*/
public class TextAreaContextMenu extends Application {
Group root = new Group();
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(root));
TextArea t = new TextArea();
ContextMenu m = new ContextMenu();
m.setOnShowing(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent arg0) {
System.out.println("Showing...");
}
});
MenuItem item = new MenuItem("Item");
m.getItems().add(item);
t.setContextMenu(m);
root.getChildren().add(t);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Is this a bug? or am I doing something wrong?
I'm running jdk1.7.0_02, and I think JavaFX 2.0.2 SDK.
BTW, how do I find out, exactly which JavaFX SDK version I have installed?
Thanks in advance for any help.
Your code works for me with JavaFX 2.1 dev build on Windows. Right-click on text area shows menu with one element named "item".
Can you try 2.1 dev version?
You can find out your current version by running next code:
System.out.println(com.sun.javafx.runtime.VersionInfo.getVersion());

Resources