I have recently started on intelij a beginner in Javafx. I am getting this symbol error for stackpane and i have no idea why am i getting it. I have tried switching my jdk between 8, 9, and 10 still have the same problem.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.StackPane;
public class JavaFX1 extends Application {
#Override
public void start(Stage primaryStage) {
Button button = new Button("Click Here");
primaryStage.setTitle("The Button");
StackPane layout = new StackPane();
layout.getChildren.add(button);
Scene scene = new Scene(layout, 300, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]){
Application.launch(args);
}
}
Try changing
this:layout.getChildren.add(button); to this:layout.getChildren().add(button);
Related
I use a combo that works nice. The only thing i would like to add is to diplay some items(~10) of this box by default at start . I tried to find this property but a hell to find this among all methods.
If somebody knows how to to that
I have the same question for Menubutton
Thanks
Just call show() on the ComboBox. Here is an example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ComboTest extends Application {
#Override
public void start(Stage stage) {
ComboBox<String> combo = new ComboBox<>();
for (int i = 1 ; i <= 20 ; i++) combo.getItems().add("Item "+i);
BorderPane root = new BorderPane();
root.setTop(combo);
Scene scene = new Scene(root, 650, 400);
stage.setScene(scene);
stage.show();
combo.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
The same thing works for MenuButton.
this is my code:
package HelloWorld;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
public class helloworld extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World");
Label l = new Label();
l.setText("Hello World");
VBox pane = new VBox();
pane.getChildren().add(l);
Scene scene = new Scene(pane, 1000, 1000);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(l.textProperty());
}
public static void main(String[] args) {
Application.launch(args);
}
}
The result is the window opening with the correct title, but the text is just utter gibberish and completely not understandable, as seen below:
Why is this happening and how can I fix it?
I was testing the MonologFX from JFXtras (v8.0-r5), but I got stuck with it!
Can anyone tell me how to check what was the button in the dialog that was pressed by the user? I tried in many ways, but no luck at all.
package javafx_jfxtras_monologfx;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import jfxtras.labs.dialogs.MonologFX;
import jfxtras.labs.dialogs.MonologFXButton;
import jfxtras.labs.dialogs.MonologFX.Type;
public class JavaFX_JFXtras_MonologFX extends Application
{
#Override
public void start(Stage stage)
{
MonologFX m = new MonologFX();
m.setModal(true);
m.setType(Type.QUESTION);
m.setTitleText("JFXtras MonologFX");
m.setMessage("Do you want to continue?");
m.setPos(698, 450);
MonologFXButton mb1 = new MonologFXButton();
mb1.setType(MonologFXButton.Type.YES);
mb1.setLabel("Continue");
m.addButton(mb1);
MonologFXButton mb2 = new MonologFXButton();
mb2.setType(MonologFXButton.Type.NO);
mb2.setLabel("Exit");
m.addButton(mb2);
Button btn = new Button();
btn.setText("Click the Button");
btn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
System.out.println("Hello :)");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("JavaFX - JFXtras MonologFX");
stage.setScene(scene);
stage.show();
m.show();
}
public static void main(String[] args)
{
launch(args);
}
}
But the controls in labs are experimental and Mark has not worked on this one for a long time. We don't take them out because someone may use them, but as of version 8u40 JavaFX has a dialog itself. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html|
So I am trying to learn Java now that I know JavaScript and PHP. I am working in Netbeans with JavaFX and I am trying to create a program that creates 5 buttons. (This is modifying the code that comes with Netbeans when creating a new JavaFX Application.) If I change the y-argument of the scene to be less than the y of all of the buttons, it will not display the remaining buttons and it will not be able scroll down. This is what I have so far. How do I enable it to scroll down so all buttons can be seen? I know that I can just change the scene back to its old height but I want to learn about scrolling with JavaFX.
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.GridPane;
import javafx.stage.Stage;
import javax.swing.JScrollPane;
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
Button[] btn=new Button[5];
for(int i=0;i<5;i++){
btn[i] = new Button();
btn[i].setText(i+"");
GridPane.setRowIndex(btn[i],i);
root.getChildren().addAll(btn[i]);
btn[i].setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("This is a button");
}
});
}
Scene scene = new Scene(root, 300, 50);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Using ScrollPane to set root as:
ScrollPane sp = new ScrollPane();
sp.setContent(root);
Scene scene = new Scene(sp, 300, 50);
I have developed a desktop database application. But the interface is not rich. So I want integrate javafx interface in my code. But I am new to javafx, please give examples or tutorials for java and javafx integrated programs. I search in javafx site and some other books but I didn't get the java and javafx combined code.
Give me a code that replace Jbutton by javafx button.
import javax.swing.*;
class Javabutton extends JFrame
{
JButton b;
Container con;
Javabutton()
{
b=JButton("hello");
setLayout(new FlowLayout());
setSize(400,500);
con=getContentPane();
con.add(b);
setVisible(true);
}
public static void main(String args[])
{
new Javabutton();
}
}
`
You need to use JFXPanel, see http://docs.oracle.com/javafx/2.0/swing/jfxpub-swing.htm
It's better to write pure javaFX code other than using JFX with swing.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Hello extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
#Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Button");
Group root = new Group();
Scene scene = new Scene(root, 300, 250);
Button btn = new Button();
btn.setLayoutX(100);
btn.setLayoutY(80);
btn.setText("OK");
btn.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent event)
{
System.out.println("Hello World");
}
});
root.getChildren().add(btn);
primaryStage.setScene(scene);
primaryStage.show();
}
}