This question already has answers here:
JavaFX: Change application language on the run
(3 answers)
JavaFX 2 and Internationalization
(3 answers)
Closed 9 months ago.
I'm using resource bundles to manage languages in my app. In my main class i have:
#Override
public void start(Stage primaryStage) throws Exception{
Locale.setDefault(new Locale("pl"));
ResourceBundle bundle = ResourceBundle.getBundle("bundles.messages");
Parent root = FXMLLoader.load(Gui.class.getResource("difficulty_choice_window.fxml"), bundle);
Scene scene = new Scene(root);
primaryStage.setTitle("SudokuGame");
primaryStage.setScene(scene);
primaryStage.show();
}
And setting language from this level, works perfectly fine, so i wanted to add a feature to change the language in the application's run time, hence i assigned this line, to a button that's suppoused to change the language:
Locale.setDefault(new Locale("en"));
As you might guess - it doesn't work. What would be the correct way to do it?
Related
Just getting started with TestFX and I'm wondering how to refer to the top Node (i.e. the Stage).
Here I see it says
All TestFX tests should use verifyThat(Node, Matcher, Function) when
writing tests, so that the developer can use DebugUtils to provide
additional info as to why a test failed.
This is a useful pointer... so say I want to say verify that "the JavaFX dialog/window is at least 600 pixels wide and at most 400 pixels high"?
NB for what it's worth I'm using the org.junit approach because the examples on the TestFX github site seem to. Actually I'm a Groovy fan and would hope to switch to the Spock TestFX implementation before too long.
NB2 it occurs to me that one way to get the Stage in testing code is to make the Stage a field of the class under test:
class ClickApplication extends Application {
Stage allTheWorldsA
public void start(Stage stage) {
Parent sceneRoot = new ClickPaneGG()
Scene scene = new Scene(sceneRoot, 500, 1000)
stage.setScene(scene)
stage.show()
allTheWorldsA = stage
}
... but somehow this feels the wrong way to do things: if the Stage is passed as a parameter of start it feels like the designers didn't want you to make a class field of it.
Is there a way to clone a scene in JavaFX ?
I currently want to keep a reference of the previous scene but can't find a simple way of doing it (except passing in the previous scene as a parameter to every new class responsible for scene creation).
Heres what I was thinking of doing (this is a snipped of my view controller)
public void switchScene(Scene newScene){
previousScene = currentScene; // something which achieves this without aliasing
currentScene = newScene;
window.setScene(newScene);
}.
However as one can observe this leads to aliasing problems.
Any way to solve this problem ?
Hi Want to implement Internationalization concept(for Hindi language) with JavaFx which runs effectively on lower processor.
Configuration :-
Processor - Intel Atom CPUD425# 1.80GHz x 2
RAM - 2 GB
OS - 32 Bit Fedora 15
Implemented Internationalization concept with Resource Bundle. But it is quite slow on that low platform.
And same slowness occurs for English and Hindi Language(2-4sec) though implemented correctly as suggested in How to implement language support for JavaFX in FXML documents?.
So, tried other approach as took one global variable, set that variable to 1 if language changed to Hindi.
And for each fxml controllers initialize method check for
that variable if it is 1 then set fxml label and button text to directly Hindi string as shown below.
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
if(Configuration.getLangSelected()==1){
label1.setText("आप क्या करना पसंद करेंगे?");
button1.setText("ई-कैश प्राप्त करें");
button2.setText("सेवा का उपयोग करें");
button3.setText("वापस");
}
}
It improves performance than resource bundle way for low hardware configuration and Flow is fine for English But for Hindi rendering took time(2.8sec).
But 2.8 sec is not accepted here. Is there other way to accomplish this task in minimum time?
For curiousity, As replaced Hindi string with English String in initialization as follows:-
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
if(Configuration.getLangSelected()==1){
label1.setText("What you want to do?");
button1.setText("Get E-cash");
button2.setText("Use Service");
button3.setText("Back");
}
}
Same files loaded fast for this change.
Is there any problem as we can not set directly other language string to the labels and buttons such things,
Is there way to set text directly and loading that fxml fastly?
Found work around for this as :- Build separate flow(screens) for hindi with one extra change, use "Text"() inside label. Not setting any text to label. Setting Other language text i.e. Hindi text to that "Text" in fxml. Loading time dropped, Previous time needed :- 2.8 sec, and now 1.2 sec.
Im trying to build a Chat Program. i can do it when i use java awt package but with java fx i seem to be a bit confused. when you first build a java fx project all your methods even the public static void main(string args[]) is in there and there's a place where you Start the primaryStage of the coding...I have no problem coding the visual side of my program its just i dont know where i should setup the Network part of my program and where to put it when im done..
**CODE: This is Just a View**
Server extends Application{
public void start(Stage primaryStage){
//Where you setup the visual of your program
}
public static void main(String args[]){
launch(args); // Where the program will run
}
public void ServerConnection(){
//where i put the codes to setup my streams and SOCKET
}
The ServerConnection method contains other methods as well But all of those will go to the ServerConnectionMethod now My question is Where will i place my ServerConnection Method so that it will run along with the my primary Stage
Sorry for the long post..have a String ="potato";
If a JavaFX application is launched correctly, it won't use the main() method at all - you can remove it temporarily (as an experiment) and check, but chances are it's not serving any purpose other than for backwards compatibility. You certainly should not rely on the main method doing anything special in the case of an FX app; it should only call launch() and nothing else.
Instead, your main class should extend Application, and the JavaFX runtime will create an instance of it for you, create a primary stage, and call the start method providing you with a reference to that stage. From this method you can do anything you like, but bear in mind it is on the UI thread (so you should create an additional thread for any long running task, the same as you would in any other toolkit such as Swing.)
You could run this setupConnection method at the beginning of the start(Stage primaryStage) method. This way it will be executed before showing the stage. You could also just run both from the main method, but as berry120 said: You don't need to call the launch(args) method in the main, if it extends Application you're fine with just the start method.
When you are done, you could use a
stage.setOnCloseRequest(e -> {
//code to execute, something like socket.close();
});
And you could change the stage.show() to stage.showAndWait()
This question already has an answer here:
Unity Create UI control from script
(1 answer)
Closed 5 years ago.
How can I create a normal Button with text label in Unity 4.6, without using any prefab or cloning existing game objects? I will use the button for debug purposes so I don't want to clutter up the design hierarchy with this button.
You can use this for UI 4.6
public void CreateButton(Transform panel ,Vector3 position, Vector2 size, UnityEngine.Events.UnityAction method)
{
GameObject button = new GameObject();
button.transform.parent = panel;
button.AddComponent<RectTransform>();
button.AddComponent<Button>();
button.transform.position = position;
button.GetComponent<RectTransform>().SetSize(size);
button.GetComponent<Button>().onClick.AddListener(method);
}
The following applies only to the legacy "gui" system from Unity3 (before about 2012). It is now unavailable.
Add this function to any script and it will display 100 x 100 pixel sized button on the left upper corner. Then just change the Debug.Log to your debug code.
void OnGUI() // deprecated, use ordinary .UI now available in Unity
{
if(GUI.Button(new Rect(0, 0, 100, 100), "Debug!")){
Debug.Log("Do some debugging");
}
}
you need to create a prefab of the element of UI you want to instantiate.
And after you can manipublate your ui element like a gameobject and use GetComponet method for manipulate properties and methods.
See the FAQ http://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html