Jump to content

Another question from me! [GUI]


Recommended Posts

Posted (edited)

Hey everyone, how are you all doing?

 

This week i have been focussing on Java GUI. I have watched the JavaFX tutorial from Thenewboston. 

But now. I want to create a GUI for my script, but i have no idea how to make the GUI visible when the script has been selected. I have asked this once, and i got answers. But i tried to look up the topic, and i think it has been deleted.

 

Hope you all can help me.

 

 

Sebastian.

Edited by OSRS Sebastian
Posted (edited)

If you got another class you call the method from another class from onStart in the main class

I.e

onStart{

GUI.Initialise();

}

Gui represents the class. initialise represents the method you have that contains the gui components.

EDIT: and make sure your method in the gui class has:

jFrame.setVisible(true);

 

Edited by Viliuks
  • Like 1
Posted (edited)

If you got another class you call the method from another class from onStart in the main class

I.e

onStart{

GUI.Initialise();

}

Gui represents the class. initialise represents the method you have that contains the gui components.

 

I tried this. Got an error that i needed to create a method. I did, but the gui didn't pop up.

 

Edit: Didn't read your edit before posting this. But, when i do. jFrame gives an error. Maybe because i use JavaFX?

Edited by OSRS Sebastian
Posted

I tried this. Got an error that i needed to create a method. I did, but the gui didn't pop up.

 

Edit: Didn't read your edit before posting this. But, when i do. jFrame gives an error. Maybe because i use JavaFX?

no jFrame. is how you named your JFrame

its the name you gave JFrame jFrame = new JFrame();

Posted

You lost me there. 

 

I wanted to import this GUI.java to my main.java wich is the bot script. What you see, is one GUI tutorial from Thenewboston.

Maybe you can help me better now: 

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GUI extends Application{

	   Stage window;
	    Scene scene;
	    Button button;

	    public static void main(String[] args) {
	        launch(args);
	    }
	    
	    

	    @Override
	    public void start(Stage primaryStage) throws Exception {
	    	window = primaryStage;
	        window.setTitle("Choose something");
	        button = new Button("Click me");

	        ChoiceBox<String> choiceBox = new ChoiceBox<>();

	        choiceBox.getItems().add("Apples");
	        choiceBox.getItems().add("Bananas");
	        choiceBox.getItems().addAll("Bacon", "Ham", "Meatballs");

	        choiceBox.setValue("Apples");

	        button.setOnAction(e -> getChoice(choiceBox));

	        VBox layout = new VBox(10);
	        layout.setPadding(new Insets(20, 20, 20, 20));
	        layout.getChildren().addAll(choiceBox, button);

	        scene = new Scene(layout, 300, 250);
	        window.setScene(scene);
	        window.show();
	    }

	    //To get the value of the selected item
	    private void getChoice(ChoiceBox<String> choiceBox){
	        String food = choiceBox.getValue();
	        System.out.println(food);
	    }

		public static void Initialise() {
			
		}
}

Posted

 

You lost me there. 

 

I wanted to import this GUI.java to my main.java wich is the bot script. What you see, is one GUI tutorial from Thenewboston.

Maybe you can help me better now: 

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GUI extends Application{

	   Stage window;
	    Scene scene;
	    Button button;

	    public static void main(String[] args) {
	        launch(args);
	    }
	    
	    

	    @Override
	    public void start(Stage primaryStage) throws Exception {
	    	window = primaryStage;
	        window.setTitle("Choose something");
	        button = new Button("Click me");

	        ChoiceBox<String> choiceBox = new ChoiceBox<>();

	        choiceBox.getItems().add("Apples");
	        choiceBox.getItems().add("Bananas");
	        choiceBox.getItems().addAll("Bacon", "Ham", "Meatballs");

	        choiceBox.setValue("Apples");

	        button.setOnAction(e -> getChoice(choiceBox));

	        VBox layout = new VBox(10);
	        layout.setPadding(new Insets(20, 20, 20, 20));
	        layout.getChildren().addAll(choiceBox, button);

	        scene = new Scene(layout, 300, 250);
	        window.setScene(scene);
	        window.show();
	    }

	    //To get the value of the selected item
	    private void getChoice(ChoiceBox<String> choiceBox){
	        String food = choiceBox.getValue();
	        System.out.println(food);
	    }

		public static void Initialise() {
			
		}
}

in on start put: 

GUI.Start();

and you don't need the jFrame.setVisible(true), you already have window.show()

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...