Javafx mac terminal external process - macos

Using JavaFX, I am trying to run a program a.out on Terminal of Mac OSX. The following code does not work on Mac. The same code works on Windows by writing cmndM={"cmd","/c","start","a.exe"}. What's wrong on Mac?
protected void onRunClick(ActionEvent evt) throws IOException {
String exeM="./a.out";
Runtime runtime=Runtime.getRuntime();
String[] cmndM= {"/bin/sh","-c",exeM}; Process pm=null; File dirM=new File(pth);
try {
pm=runtime.exec(cmndM, null, dirM);}
}catch (IOException e) {
msg.setText("Error in running simulation.");
}
}

It might be related to location of the file.
I'd have tried with fixed location first, just to be sure it works.
Also, make sure the file is actually there, inside pth.
package javafxapplication1;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
String exeM="/tmp/a.out";
Runtime runtime=Runtime.getRuntime();
String[] cmndM= {"/bin/sh","-c",exeM};
Process pm=null;
File dirM=new File("/tmp");
try {
pm=runtime.exec(cmndM, null, dirM);
}catch (IOException e) {
System.out.println("Error");
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

How to disable macOS genie effect when JavaFX FileChooser opens?

When opening a JavaFX FileChooser within a Stage (only way to make it modal), there is an annoying "Genie" effect (see code and movie below). How can it be disabled ?
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.stage.Stage;
import javafx.stage.FileChooser;
import javafx.concurrent.Task;
public class Test {
private static void initFX() {
// This method is invoked on the JavaFX thread
Stage stage = new Stage();
FileChooser fileChooser = new FileChooser();
stage.setMaxHeight(0);
stage.setWidth(0);
stage.setTitle("Choose an file");
stage.setResizable(false);
stage.setAlwaysOnTop(true);
stage.show();
fileChooser.showOpenDialog(stage);
stage.hide();
}
public static void main(String[] args) {
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX();
}
});
}
}

Dialogue Positioning Javafx

I am trying to create a Javafx game and I want the user to be able to input their name using a dialogue. The problem is that when I try to position the dialogue using setY() or setX() nothing changes. I should note that I am using an online IDE so if this is the problem please somebody tell me.
import javafx.stage.Modality;
import javafx.scene.control.*;
import javafx.scene.control.Dialog;
import javafx.geometry.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
VBox page = new VBox();
Scene scene = new Scene(page, 600, 700);
primaryStage.setScene(scene);
primaryStage.show();
setUserName();
}
public static void setUserName() {
Dialog<String> dialog = new Dialog<String>();
dialog.setTitle("Dialog");
ButtonType type = new ButtonType("Ok", ButtonData.OK_DONE);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.setContentText("Hello World");
dialog.setY(100);
dialog.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}

Set text color of JavaFX TextField without CSS

I've been working a lot in JavaFX lately, by writing code only -- no CSS no FXML and I want to keep it that way. I've managed to do all of the stuff I wanted that way except that now I can't set the text color of TextField without using CSS, so I'm asking for advice here. Is there any way to do it, no matter how hacky it is?
Note that I'm still using Java 8 version.
Considering your points : "No CSS" and "how hacky it can be.." below is one way to achieve the text coloring.
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TextField_TextColor_Demo extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
TextField textField = new TextField();
textField.setSkin(new TextFieldSkin(textField){
#Override
protected void layoutChildren(double x, double y, double w, double h) {
super.layoutChildren(x, y, w, h);
if(textField.getProperties().get("colorChanged")==null) {
textFill.setValue(Color.RED);
textField.getProperties().put("colorChanged",true);
}
}
});
StackPane root = new StackPane(textField);
Scene scene = new Scene(root, 300,300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String... args){
Application.launch(args);
}
}
I could not make it work. But made another solution. Although the caret color also changes.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
public class TextField_TextColor_Demo extends Application {
#Override
public void start(Stage stage) throws Exception {
TextField txt = new TextField();
CustomTextFieldSkin skin = new CustomTextFieldSkin(txt);
txt.setSkin(skin);
skin.setHighlight(Color.RED);
HBox box = new HBox(txt);;
Scene scene = new Scene(box, 300, 100);
stage.setScene(scene);
stage.show();
}
public class CustomTextFieldSkin extends TextFieldSkin{
public CustomTextFieldSkin(TextField textField) {
super(textField);
}
public void setHighlight(Paint value) {
setTextFill(value);
}
}
public static void main(String... args){
Application.launch(args);
}
}
Seems it is possible to color the caret separately, according these two answers:
https://stackoverflow.com/a/47876289/7989121
https://stackoverflow.com/a/27323035/7989121
Both use caretPath though, which I could not use due it not being visible according to my IDE

How can I move a Label with a KeyEvent? (JavaFX)

I just learned how to make a GUI application with JavaFX with a FXML file. There is one thing that I don't understand though. When I try to add a KeyListener to a Label or the layout in my FXML file, the code doesn't get executed. It is a simple task like System.out.println("worked");, nothing complicated(eventually I want to move the Label with the keylistener, but now I just wanted something simple where I could easily see if it worked). I read somewhere that you need to add the listener on Frame level, but I don't know how. I would really appreciate it if someone could help me
Main.java:
package sample;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root, 600, 600);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>()
{
#Override
public void handle(KeyEvent event) {
Controller controller = new Controller();
controller.moveLabel(event);
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
My controller class:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
public class Controller {
#FXML
private Label label;
#FXML
public void moveLabel(KeyEvent e){
switch (e.getCode()){
case RIGHT:
label.setTranslateX(3);
break;
default:
System.out.println("not possible");
}
}
}
My FXML file:
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10"
vgap="10">
<Label text="Text" fx:id="label" ></Label>
</GridPane
Error message:
Exception in thread "JavaFX Application Thread"
java.lang.NullPointerException
at sample.Controller.moveLabel(Controller.java:15)
at sample.Main$1.handle(Main.java:22)
at sample.Main$1.handle(Main.java:18)
at
com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handle
BubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent
(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run
(GlassViewEventHandler.java:217)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run
(GlassViewEventHandler.java:149)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$352
(GlassViewEventHandler.java:248)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock
(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent
(GlassViewEventHandler.java:247)
at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
at com.sun.glass.ui.View.notifyKey(View.java:966)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147
(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Add a method to your controller to move the label, e.g.
public class Controller {
private final double moveDelta = 10 ;
#FXML
private Label labelTest ;
public void moveLabel(int deltaX, int deltaY) {
labelTest.setTranslateX(labelTest.getTranslateX() + moveDelta * deltaX);
labelTest.setTranslateY(labelTest.getTranslateY() + moveDelta * deltaY);
}
}
Then get a reference to the controller in the start() method (you need to use the non-static load() method from FXMLLoader to do this), and call the method from the key handler:
#Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root, 600, 600);
Controller controller = loader.getController();
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
switch(event.getCode()){
case RIGHT:
controller.moveLabel(1, 0);
break;
default:
System.out.println("not possible");
}
}
});
primaryStage.setScene(scene);
primaryStage.show();
}

Add image to a button at a specific position JavaFX

When I add image and text to a button, by default elements are set horizontally. How can I change this behavior to get text under image ?
Set the contentDisplayProperty on the button.
button.setContentDisplay(ContentDisplay.TOP);
Here is an executable example:
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ButtonGraphicTest extends Application {
#Override public void start(final Stage stage) throws Exception {
final Label response = new Label();
final ImageView imageView = new ImageView(
new Image("http://icons.iconarchive.com/icons/eponas-deeway/colobrush/128/heart-2-icon.png")
);
final Button button = new Button("I love you", imageView);
button.setStyle("-fx-base: coral;");
button.setContentDisplay(ContentDisplay.TOP);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
response.setText("I love you too!");
}
});
final VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(button, response);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10; -fx-font-size: 20;");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
// icon license: (creative commons with attribution) http://creativecommons.org/licenses/by-nc-nd/3.0/
// icon artist attribution page: (eponas-deeway) http://eponas-deeway.deviantart.com/gallery/#/d1s7uih

Resources