JavaFX - How to add ColorPicker to my scene - colors

How can I add a ColorPicker to my application?
I tryed with this code:
public class MyColorPicker extends Application {
#Override
public void start(Stage primaryStage) {
VBox box = new VBox();
ColorPicker cp = new ColorPicker();
box.getChildren().add(cp) // Error: cp Cannot be converted to Node
Scene scene = new Scene(box, 300, 200);
primaryStage.setTitle("colorPicker");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

JavaFX task not reruning once canceled or finishing once

I am working on a basic Java FX task exercise. It counts from 1 to 150 on a thread. The current value is presented on a label and updates a progress bar.
There is a button to start the task, to cancel it and to view canceled status of the task.
The thing that puzzles me is as to why I cannot re run the task after having canceled the thread once(same thing happens if I let the task finnish).
I want to be able to rerun the task . Then I need to make it so that it will resume(though that shouldn't be that hard after figuring out how to rerun the task)
Source ;
public class JavaFX_Task extends Application {
#Override
public void start(Stage primaryStage) {
final Task task;
task = new Task<Void>() {
#Override
protected Void call() throws Exception {
int max = 150;
for (int i = 1; i <= max; i++) {
if (isCancelled()) {
break;
}
updateProgress(i, max);
updateMessage(String.valueOf(i));
Thread.sleep(100);
}
return null;
}
};
ProgressBar progressBar = new ProgressBar();
progressBar.setProgress(0);
progressBar.progressProperty().bind(task.progressProperty());
Label labelCount = new Label();
labelCount.textProperty().bind(task.messageProperty());
final Label labelState = new Label();
Button btnStart = new Button("Start Task");
btnStart.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
new Thread(task).start();
}
});
Button btnCancel = new Button("Cancel Task");
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
task.cancel();
}
});
Button btnReadTaskState = new Button("Read Task State");
btnReadTaskState.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
labelState.setText(task.getState().toString());
}
});
VBox vBox = new VBox();
vBox.setPadding(new Insets(5, 5, 5, 5));
vBox.setSpacing(5);
vBox.getChildren().addAll(
progressBar,
labelCount,
btnStart,
btnCancel,
btnReadTaskState,
labelState);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The Task documentation is pretty clear on this.
As with FutureTask, a Task is a one-shot class and cannot be reused. See Service for a reusable Worker.
There is an example of restartable concurrent services in the Service documentation.

JavaFX is locking the app when after ActionEvent is done

I am building a menu (file, view, help, etc.). For ex. in File you have upload and exit (both working fine). In help I have some topics: ACF, ARIMA, HoltWinter, the idea is when you chose one of these a text to be displayed in a GridPane. The problem is when you chose one, it displays its text, but it also locks the whole app and you can do nothing, except close the app.
This is a file chooser and it's working just fine:
MenuItem upload = new MenuItem("Upload");
upload.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(primaryStage);
}
});
And here is an event, which is blocking the app:
MenuItem acfH = new MenuItem("ACF");
acfH.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
fr.md.pack.TextClass.acfText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.acfText.setText(fr.md.pack.StringClass.aboutACF);
grid.add(fr.md.pack.TextClass.acfText,1,10);
}
});
Any suggestions how to solve the issue?
Thanks in advance.
Here is the whole code:
public class MD extends Application {
#Override
public void start(final Stage primaryStage) throws IOException {
primaryStage.setTitle("Crude oil price prediction");
primaryStage.getIcons().add(new Image("file:resources/images/DukeWithHelmet.png"));
final GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Group root = new Group();
MenuBar menuBar = new MenuBar();
Scene scene = new Scene(root, 500,500, Color.WHITE);
scene.getStylesheets().add(this.getClass().getResource("style.css")
.toExternalForm());
primaryStage.setTitle("Crude oil price predicition");
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
Menu file = new Menu("File2");
MenuItem upload = new MenuItem("Upload");
upload.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(primaryStage);
}
});
MenuItem exit = new MenuItem("Exit");
exit.setMnemonicParsing(true);
exit.setAccelerator(new KeyCodeCombination(KeyCode.X,KeyCombination.CONTROL_DOWN));
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Platform.exit();
}
});
Menu test = new Menu("Test");
MenuItem arima = new MenuItem ("ARIMA");
arima.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
MenuItem acf = new MenuItem ("ACF");
acf.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
MenuItem pacf = new MenuItem ("PACF");
pacf.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
MenuItem hw = new MenuItem ("HoltWinters");
hw.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
Menu help = new Menu("Help");
MenuItem arimaH = new MenuItem ("ARIMA");
arimaH.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.arimaText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.arimaText.setText(fr.md.pack.StringClass.aboutArima);
grid.add(fr.md.pack.TextClass.arimaText, 1, 10);
}
});
MenuItem acfH = new MenuItem("ACF");
acfH.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
fr.md.pack.TextClass.acfText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.acfText.setText(fr.md.pack.StringClass.aboutACF);
grid.add(fr.md.pack.TextClass.acfText,1,10);
}
});
MenuItem pacfH = new MenuItem ("PACF");
pacfH.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.pacfText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.pacfText.setText(fr.md.pack.StringClass.aboutPACF);
grid.add(fr.md.pack.TextClass.pacfText, 1, 10);
}
});
MenuItem hwH = new MenuItem ("HoltWinters");
hwH.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.hwText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.hwText.setText(fr.md.pack.StringClass.aboutHW);
grid.add(fr.md.pack.TextClass.hwText, 1, 10);
}
});
Menu about = new Menu("About");
MenuItem thisS = new MenuItem ("About this software");
thisS.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.textRef.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.aboutSoft.setText(fr.md.pack.StringClass.aboutSofttext);
grid.add(fr.md.pack.TextClass.aboutSoft, 1, 40);
}
});
MenuItem R = new MenuItem ("About R");
R.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.textRef.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.aboutR.setText(fr.md.pack.StringClass.aboutRtext);
grid.add(fr.md.pack.TextClass.aboutR, 1, 40);
}
});
MenuItem RServe = new MenuItem ("About RServe");
RServe.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.textRef.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.textRef.setText(fr.md.pack.StringClass.aboutText);
grid.add(fr.md.pack.TextClass.textRef, 1, 40);
}
});
file.getItems().addAll(upload,exit);
test.getItems().addAll(arima,acf,pacf,hw);
help.getItems().addAll(arimaH,acfH,pacfH,hwH);
about.getItems().addAll(thisS, R, RServe);
menuBar.getMenus().addAll(file,test,help,about);
root.getChildren().addAll(menuBar,grid);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The classes used are just containing the strings to display:
package fr.md.pack;
public class StringClass {
final static String aboutText = "Малко информация за RServe.";
final static String aboutRtext = "Малко инфо за Р";
final static String aboutSofttext = "Малко инфо за софта";
final static String aboutArima = "Малко инфо за ARIMA";
final static String aboutACF = "Малко инфо за ACF";
final static String aboutPACF = "Малко инфо за PACF";
final static String aboutHW = "Малко инфо за Holt Winters";
}
Well, I just installed Java 8 and surprise....everything is working just fine. I used till now Java 1.7 update 45 and it was locking my application as soon as I try to use the Action event. With Java 8 it's working but not without some bugs....

Stage is hidden when dialog is shown

I have this code which displays confirmation dialog to exit application.
public class DialogPanels
{
public void initClosemainAppDialog(final Stage primaryStage)
{
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent event)
{
event.consume(); // Do nothing on close request
// Dialog Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
// Frage - Label
Label label = new Label("Exit from the program");
// Button "Yes"
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
//primaryStage.close();
//dialog.close();
//Platform.exit();
System.exit(0);
}
});
// Button "No"
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
primaryStage.show();
dialog.close();
}
});
// Layout for the Button
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(okBtn);
hbox.getChildren().add(cancelBtn);
// Layout for the Label and hBox
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.getChildren().add(label);
vbox.getChildren().add(hbox);
// Stage
Scene scene = new Scene(vbox);
dialog.setScene(scene);
dialog.show();
}
});
}
}
The problem is that when close the main application the dialog box is displayed and the main stage is hidden. I want to display the dialog box in front of the main stage. Can you help me to correct this?
UPDATE
I tested this code, it's working but when the dialog is displayed the mainstage is not responsible(frozen). How I an make the mainstage responsible when I display dialog?
Consume the closing event and set the owner of the stage if you do not want to see another window when the windows are minimized:
#Override
public void handle(WindowEvent event)
{
event.consume(); // Do nothing on close request
// Dialog Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
// other stuff
}
});
You need to set the proper relationships between primaryStage and dialog stage. Here's a hint to get you going:
...
dialog.initOwner(primaryStage);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.showAndWait();
You can find more information in Oracle's JavaFX 2 JavaDocs.
More example code (edit)
I'm using setOnHiding(..) instead of setOnCloseRequest(..):
stage.setOnHiding(new AskUserIfHeReallyWantsToQuitWindowHandler(stage));
I extracted your code into a seperate event handler class and fixed the issues I mentioned (sorry, I am little short on time right now):
public class AskUserIfHeReallyWantsToQuitWindowHandler implements EventHandler<WindowEvent> {
private final Stage primaryStage;
public AskUserIfHeReallyWantsToQuitWindowHandler(final Stage primaryStage) {
Objects.requireNonNull(primaryStage);
this.primaryStage = primaryStage;
}
#Override
public void handle(final WindowEvent event) {
event.consume();
final Stage dialog = new Stage();
final Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(final ActionEvent event) {
dialog.close();
primaryStage.close();
}
});
// Button "No"
final Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(final ActionEvent event) {
dialog.close();
Platform.runLater(new Runnable() {
#Override
public void run() {
primaryStage.show();
}
});
}
});
// Layout for the Button
final HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(okBtn);
hbox.getChildren().add(cancelBtn);
// Layout for the Label and hBox
final VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.getChildren().add(new Label("Do your really want to exit?"));
vbox.getChildren().add(hbox);
// Stage
final Scene scene = new Scene(vbox);
dialog.setScene(scene);
dialog.initOwner(primaryStage);
dialog.initModality(Modality.NONE);
dialog.showAndWait();
}
}

JavaFx 2.2 & Fullscreen mode

Good day!
I try my first application JavaFx. I set the full-screen mode and press the button shows a dialog. When the dialog appears the main window loses its full-screen.
Code:
public class Test1 extends Application {
#Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage dialogStage = new Stage(StageStyle.UTILITY);
dialogStage.initModality(Modality.APPLICATION_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create().
children(new Text("Hi"), new Button("Ok.")).
alignment(Pos.CENTER).padding(new Insets(5)).build()));
dialogStage.show();
System.out.println(dialogStage.getOwner()==primaryStage.getOwner());
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Rectangle2D r = Screen.getPrimary().getBounds();
Scene scene = new Scene(root, r.getWidth(), r.getHeight());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
OS: Windows 7
You need to
dialogStage.initOwner(primaryStage);

How to make drag and drop action in JavaFX 2.2 with Swing?

I have a problem with drag and drop event on JFXPanel that on located JPanel. When i push drag message to DragBoard, javaFX part of application doesnt work anymore. I think its about swing event mechanizm but i am not sure. There is no problem with other events. It made me confused. Is there any solution to this problem? Thanks in advance.
public class MyScene extends Scene {
public MyScene(VBox vBoxMainLayout) {
super(vBoxMainLayout);
HBox hBox = new HBox();
hBox.setPrefSize(10000, 10000);
hBox.setSpacing(40);
Button buttonSource = new Button("Source");
buttonSource.setMinSize(60, 30);
buttonSource.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
Dragboard db = startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
String message = "Drag operatation is done";
content.putString(message);
db.setContent(content);
event.consume();
}
});
buttonSource.setOnDragDone(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
event.consume();
}
});
TextArea textAreaTarget = new TextArea();
textAreaTarget.setMinSize(200, 500);
hBox.getChildren().add(buttonSource);
hBox.getChildren().add(textAreaTarget);
vBoxMainLayout.getChildren().add(hBox);
}
}
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
VBox vBoxMainLayout = new VBox();
MyScene myScene = new MyScene(vBoxMainLayout);
JFrame frame = new JFrame();
JFXPanel arg0 = new JFXPanel();
arg0.setScene(myScene);
frame.getContentPane().add(arg0);
frame.setVisible(true);
}
}
It was a known deadlock in JavaFX 2.1 and pushed to 2.2 (thats what i learned from oracle ) but i guess it stil not solved.

Resources