Prompt Text for javaFX ChoiceBox - javafx-8

I want to add "Please Select" as the promptText for javaFX ChoiceBox component.Can any one please help me.
(Note:- This is not the default selected value. It is the value that display before get select any thing)
Thanks

You might want to use ComboBox instead. It is similar to ChoiceBox but has a promptText property.
public class Main extends Application {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
ComboBox combo = new ComboBox();
combo.getItems().addAll("Item 1", "Item 2");
combo.setPromptText("Please Select");
vbox.getChildren().add(combo);
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

How to run JavaFX Window in other Class

as in title, i need help with Running JavaFX in other class. For exmaple, from Main i want to type something like 'new TempClass()' and then i expect new Window but i got nothing xD Thank you in advance for solve the problem! <3
Main:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
new TempClass();
}
public static void main(String[] args) {
launch(args); // when i launch(args) the 'start' func starting btw
}
}
TempClass:
public class TempClass extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane root = new Pane();
root.setPrefSize(300,300);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is one of few ways which solved my problem:
Main:
public class Main {
public static void main(String[] args) {
Application.launch(TempClass.class, args);
}
}
TempClass:
public class TempClass extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane root = new Pane();
root.setPrefSize(300,300);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}

RCP E4 Statusbar is to small

I create a statusbar for my e4 rcp application but the statusbar is to small.
In my Appliication.e4xmi is the following configured:
Trimmed Window -> TrimBars -> Window Trim (Bottom) -> Toolbar -> Tool Control (link to my StatusBar class)
Linked StatusBar class:
public class StatusBar {
private Label label;
#Inject
private IEventBroker eventBroker;
public static final String STATUSBAR = "statusbar";
#Inject
#Optional
public void getEvent(#UIEventTopic(STATUSBAR) String message) {
updateInterface(message);
}
#PostConstruct
public void createControls(Composite parent) {
label = new Label(parent, SWT.LEFT);
label.setBackground(parent.getBackground());
}
public void updateInterface(String message) {
try {
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
try {
label.setText(message);
} catch (Exception e) {
logger.error(e.fillInStackTrace());
}
}
});
} catch (Exception exception) {
logger.error(exception.fillInStackTrace());
}
}
View of the Statusbar
You don't need a Toolbar for a ToolControl. Put the ToolControl as the immediate child of the Trim Bar.
Put the label in a composite to get it layed out properly:
#PostConstruct
public void createGui(final Composite parent)
{
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new GridLayout());
Label label = new Label(body, SWT.LEFT);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
....
}
You may also want to specify a tag value of stretch in ToolControl definiton in the e4xmi to make the control use all available horizontal space in the trim bar.

How to position controls binding there position into the stage's size using the controller class in JavaFx?

I found some thing similar in this link
How to call functions on the stage in JavaFX's controller file
and here is what I found in one of the answers
StageTrackingSample.java
public class StageTrackingSample extends Application {
#Override public void start(final Stage stage) throws Exception {
final FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"stagetracking.fxml"
)
);
final Parent root = (Parent) loader.load();
final StageTrackingController controller = loader.getController();
controller.initData(stage);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
StageTrackingController.java
public class StageTrackingController {
#FXML private Label stageX;
public void initialize() {}
public void initData(final Stage stage) {
stageX.textProperty().bind(
Bindings.format(
"(%1$.2f, %2$.2f)",
stage.xProperty(),
stage.yProperty()
)
);
}
}
I wanted to position the progressIndicator in the middle of the window, so I tried this in my controller class
Controller.java
public void initInterface(Stage stage) {
progressIndicator.layoutXProperty().bind(stage.widthProperty().divide(2));
progressIndicator.layoutYProperty().bind(stage.heightProperty().divide(2));
}
and this in Main.java
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
final FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
final Parent root = loader.load();
final Controller controller = loader.getController();
final Scene scene = new Scene(root);
controller.initInterface(primaryStage);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
it doesn't work even when I tried passing the scene or the anchorpane(which is defined in the fxml file) as a parameter into initInterface method, it seems that it has problem with binding progressIndicator properties
by using the layoutXProperty and layoutYProperty and binding them to the Stage's width and height you must be trying to put it in the lower right hand corner of the stage. You can achieve this much easier, and JavaFX insists you do so, by using layouts in your scene and making the scene fill the entire area in question.
"From the Javadocs for the layoutX property:
If the node is managed and has a Region as its parent, then the layout region will set layoutX according to its own layout policy. If the node is unmanaged or parented by a Group, then the application may set layoutX directly to position it.
What this means is that the LayoutX/Y properties are controlled by the parent (and so it should be able to 'set' them). However, when you bind them they cannot be set anymore resulting in " A bound value cannot be set" exception."
Here is a good tutorial on regions and how to get things to layout in SceneBuilder as you please. If you're not using SceneBuilder I recommend it.
https://www.youtube.com/watch?v=zvgWgpGZVKc&list=PL6gx4Cwl9DGBzfXLWLSYVy8EbTdpGbUIG&index=35

get scene from MenuItem

How i can get scene from MenuItem? i tried this code:
public class MainController implements Initializable {
#FXML
private MenuBar menuBar;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML
public void show(ActionEvent event) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(menuBar.getScene().getWindow());
stage.show();
}
}
the above code does not generate an error, but it does not display the window!!!
Well, in your Controller class add a variable for Scene with the getters and setters.
Then you can do something like this:
#Override
public void start(Stage primaryStage)throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml"));
Parent root = loader.load();
MainController mainControls = loader.getController();
Scene scene = new Scene(root, 300, 250);
mainControls.setScene(scene);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
Now the scene is available to all items in that class.
It looks like you're trying to create a Dialog of sorts?
If so don't use the Main scene... create a new Stage and show that with it's content...
Although I believe the JDK comes with Dialog support now.

Launch JavaFX application from another class

I need to start a javafx Application from another "container" class and call functions on the Application, but there doesn't seem to be any way of getting hold of a reference to the Application started using the Application.launch() method. Is this possible?
Thanks
Suppose this is our JavaFX class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class OKButton extends Application {
#Override
public void start(Stage stage) {
Button btn = new Button("OK");
Scene scene = new Scene(btn, 200, 250);
stage.setTitle("OK");
stage.setScene(scene);
stage.show();
}
}
Then we may launch it from another class like this:
import javafx.application.Application;
public class Main {
public static void main(String[] args) {
Application.launch(OKButton.class, args);
}
}
I had the same problem as this and got round it using this hack:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.concurrent.CountDownLatch;
public class StartUpTest extends Application {
public static final CountDownLatch latch = new CountDownLatch(1);
public static StartUpTest startUpTest = null;
public static StartUpTest waitForStartUpTest() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return startUpTest;
}
public static void setStartUpTest(StartUpTest startUpTest0) {
startUpTest = startUpTest0;
latch.countDown();
}
public StartUpTest() {
setStartUpTest(this);
}
public void printSomething() {
System.out.println("You called a method on the application");
}
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
Label label = new Label("Hello");
pane.setCenter(label);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
and then the class you are launching the application from:
public class StartUpStartUpTest {
public static void main(String[] args) {
new Thread() {
#Override
public void run() {
javafx.application.Application.launch(StartUpTest.class);
}
}.start();
StartUpTest startUpTest = StartUpTest.waitForStartUpTest();
startUpTest.printSomething();
}
}
Hope that helps you.
Launch JavaFX in other Class using Button:
class Main extends Application{
public void start(Stage s)throws Exception{
event();
s.show();
}
public void event(){
btn.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent ae){
Stage s = new Stage();
new SubClassName().start(s);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
class SubClassName{
public void start(Stage s){
Pane pane = new Pane();
Scene addFrame = new Scene(pane,280,450);
s.setScene(addFrame);
s.show();
}
}
I'm not sure what you're trying to achieve, but note that you can e.g call from another class Application.launch to start the JavaFX Application thread and Platform.exit to stop it.
The above ways of invoking other javafx class from another sometimes work. Struggling to find an ultimate way to do this brought me to the following walk around:
Suppose this is the javafx class that exteds Application we wish to show from a different class, then we should add the following lines
class ClassToCall extends Application{
//Create a class field of type Shape preferably static...
static Stage classStage = new Stage();
#Override
public void start(Stage primaryStage){
// Assign the class's stage object to
// the method's local Stage object:
classStage = primaryStage ;
// Here comes some more code that creates a nice GUI.....
// ......
}
}
And now from the other place in the project, in order to open the window
that the above class creates do the following:
// Suppose we want to do it with a button clicked:
btn1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//create an object of the class you wish to invoke its
//start() method:
ClassToCall ctc = new ClassToCall();
// Then call its start() method in the following way:
ctc.start(ClassToCall.classStage);
}// End handle(ActionEvent event)
});// End anonymous class