I'm working on a school project creating a simple battleship game and i want to use the Drag And Drop function to drag buttons from HBox on the bottom of the screen to the GridPane that the player uses to place ships on. But i can't get it to work properly. Here's a picture of my board per now.
I have tried to use the button.setOnMouseDragged(e -> {CODE HERE}); but it doesnt work.
Here is the code i used for my window
public class GridOrganizer {
private BorderPane borderPane;
public GridOrganizer() {
borderPane = new BorderPane();
borderPane.setStyle("-fx-background-color: grey;");
borderPane.setPrefHeight(600);
borderPane.setPrefWidth(600);
createGrid();
}
public void createGrid() {
//Creates the grids where the game is played and buttons/ships to place on grid
GridPane playerGrid = new GridPane();
GridPane enemyGrid = new GridPane();
Insets padding = new Insets(10);
//Create playergrid
for (int i = 0; i < 10; i++) {
playerGrid.getColumnConstraints().add(new ColumnConstraints(50)); //50 wide
playerGrid.getRowConstraints().add(new RowConstraints(50));
}
//Create enemygrid
for (int i = 0; i < 10; i++) {
enemyGrid.getColumnConstraints().add(new ColumnConstraints(50)); //50 wide
enemyGrid.getRowConstraints().add(new RowConstraints(50));
}
//looping through row and columns and adds buttons
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
Button button = new Button();
button.setPrefHeight(50);
button.setPrefWidth(50);
GridPane.setConstraints(button, j, i); //(button, column, row)
playerGrid.getChildren().add(button); //add button on each index
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
//HIT or MISS
System.out.println("Row: " + GridPane.getRowIndex(button) + ", Column: " + GridPane.getColumnIndex(button));
button.setStyle("-fx-background-color: grey;");
}
});
}
}
//..same with enemy grid
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
Button button = new Button();
button.setPrefHeight(50);
button.setPrefWidth(50);
GridPane.setConstraints(button, j, i); //(button, column, row)
enemyGrid.getChildren().add(button); //add button on each index
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
System.out.println("Row: " + GridPane.getRowIndex(button) + ", Column: " + GridPane.getColumnIndex(button));
button.setStyle("-fx-background-color: grey;");
}
});
}
}
//Make buttons for the ships
Button rowboat = new Button("Rowboat");
Button sailboat = new Button("Sailboat");
Button submarine = new Button("Submarine");
Button destroyer = new Button("Destroyer");
Button battleship = new Button("Battleship");
//Size the ship buttons to match game description
battleship.setPrefHeight(50);
battleship.setPrefWidth(250); //size 5
destroyer.setPrefHeight(50);
destroyer.setPrefWidth(200); //size 4
submarine.setPrefHeight(50);
submarine.setPrefWidth(150); //size 3
sailboat.setPrefHeight(50);
sailboat.setPrefWidth(150); //size 3
rowboat.setPrefHeight(50);
rowboat.setPrefWidth(100); //size 2
//Drags button
rowboat.setOnMouseDragged(e -> {
//CODE HERE
});
//Drops button on grid
//CODE HERE
sailboat.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
//PLACE SHIP
}
});
submarine.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
//PLACE SHIP
}
});
destroyer.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
//PLACE SHIP
}
});
battleship.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
//PLACE SHIP
}
});
HBox ships = new HBox(); //Horizontal box
ships.getChildren().addAll(rowboat, sailboat, submarine, destroyer, battleship); //Add buttons to horizontal box
//Add grids and ship buttons to pane with padding
borderPane.setLeft(enemyGrid);
BorderPane.setMargin(enemyGrid, padding);
borderPane.setRight(playerGrid);
BorderPane.setMargin(playerGrid, padding);
borderPane.setBottom(ships);
BorderPane.setMargin(ships, padding);
}
public Pane getGrid() {
return borderPane;
}
}
The plan here is to start a drag event with the data needed to create your button when the drag event finished. In your code, it looks like you have added the Buttons into the Grid already. That means you need to only transfer a String to change the Button's text. In my code, I am using a StackPane when creating the Grid. I then create and add the Buttons later. You approach may be better. I have looked that far. I have added an MCVE (Altered code from here):
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* Demonstrates a drag-and-drop feature.
*/
public class HelloDragAndDrop extends Application
{
#Override
public void start(Stage stage)
{
//Source Buttons.
final Button boat1 = new Button("boat1");
final Button boat2 = new Button("boat2");
final Button boat3 = new Button("boat3");
final Button boat4 = new Button("boat4");
//Adding OnDragDetected to source Buttons.
setOnDragDetected(boat1);
setOnDragDetected(boat2);
setOnDragDetected(boat3);
setOnDragDetected(boat4);
//Adding onDragDone to source Buttons.
setOnDragDone(boat1);
setOnDragDone(boat2);
setOnDragDone(boat3);
setOnDragDone(boat4);
//Creating GridPane
GridPane gridPane = new GridPane();
gridPane.setVgap(5);
gridPane.setHgap(5);
gridPane.setPadding(new Insets(5, 5, 5, 5));
gridPane.setStyle("-fx-background-color: black;");
//Adding StackPane to every Cell in the GridPane and Adding the Target Events to each StackPane.
for (int i = 0; i < 6; i++) {
StackPane stackPane = new StackPane();
stackPane.setPrefSize(150, 50);
stackPane.setStyle("-fx-background-color: yellow;");
setOnDragOver(stackPane);
setOnDragEntered(stackPane);
setOnDragExited(stackPane);
setOnDragDropped(stackPane);
gridPane.add(stackPane, i / 3, i % 3);
}
HBox root = new HBox(new VBox(boat1, boat2, boat3, boat4), gridPane);
stage.setTitle("Hello Drag And Drop");
Scene scene = new Scene(root, 400, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
Application.launch(args);
}
//source events handlers
public void setOnDragDetected(Button source)
{
source.setOnDragDetected((MouseEvent event) -> {
/* drag was detected, start drag-and-drop gesture*/
System.out.println("onDragDetected");
/* allow any transfer mode */
Dragboard db = source.startDragAndDrop(TransferMode.ANY);
/* put a string on dragboard */
ClipboardContent content = new ClipboardContent();
content.putString(source.getText());
db.setContent(content);
event.consume();
});
}
public void setOnDragDone(Button source)
{
source.setOnDragDone((DragEvent event) -> {
/* the drag-and-drop gesture ended */
System.out.println("onDragDone");
/* if the data was successfully moved, clear it */
// if (event.getTransferMode() == TransferMode.MOVE) {
// source.setText("");
// }
event.consume();
});
}
//target event handlers
public void setOnDragOver(StackPane target)
{
target.setOnDragOver((DragEvent event) -> {
/* data is dragged over the target */
System.out.println("onDragOver");
/* accept it only if it is not dragged from the same node
* and if it has a string data */
if (event.getGestureSource() != target
&& event.getDragboard().hasString()) {
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
event.consume();
});
}
public void setOnDragEntered(StackPane target)
{
target.setOnDragEntered((DragEvent event) -> {
/* the drag-and-drop gesture entered the target */
System.out.println("onDragEntered");
/* show to the user that it is an actual gesture target */
if (event.getGestureSource() != target
&& event.getDragboard().hasString()) {
target.setStyle("-fx-background-color: green;");
}
event.consume();
});
}
public void setOnDragExited(StackPane target)
{
target.setOnDragExited((DragEvent event) -> {
/* mouse moved away, remove the graphical cues */
target.setStyle("-fx-background-color: transparent;");
event.consume();
});
}
public void setOnDragDropped(StackPane target)
{
target.setOnDragDropped((DragEvent event) -> {
/* data dropped */
System.out.println("onDragDropped");
/* if there is a string data on dragboard, read it and use it */
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasString()) {
//target.setText(db.getString());
Button tempBoat = new Button(db.getString());
tempBoat.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
target.getChildren().clear();
target.getChildren().add(tempBoat);
success = true;
}
/* let the source know whether the string was successfully
* transferred and used */
event.setDropCompleted(success);
event.consume();
});
}
}
In this code, the Buttons that represent the Boats all have onDragDetected and onDragDone event handlers attached. They are considered Source/start of a Drag event. For every cell in the GridPane, a StackPane is added. These StackPanes are considered the Target/areas to drop drag events. Each StackPane has onDragOver, onDragEntered, onDragDropped, and onDragExited attached. Once a drag event is complete, the StackPane/Target that receives the event gets a new Button as a child with the same name as the Source Button.
Related
I'm a really new programmer so idk if this question sounds really stupid but..
This is my main:
package culminating;
import javafx.application.Application;
& all other necessary imports...
public class CulminatingMAIN extends Application {
//Set Global variables
int count = 0;
String name;
String gender = "Boy";
Label testLabel = new Label(gender + " has been selected");
#Override
public void start(Stage primaryStage) throws Exception {
/**
* ************************ SCENE 1 WORK *************************
*/
TextField nameTextField = new TextField();
nameTextField.setMaxWidth(100);
Label nameLabel = new Label("Please enter your name.");
Label genderLabel = new Label();
Label titleLabel = new Label("Math Adventure!");
titleLabel.setFont(Font.font("Arial", FontWeight.BOLD, 30));
Rectangle titleRectangle = new Rectangle();
titleRectangle.setFill(Color.TOMATO);
titleRectangle.setWidth(280);
titleRectangle.setHeight(60);
titleRectangle.setStroke(Color.BLACK);
titleRectangle.setStrokeWidth(2.0);
StackPane root = new StackPane(titleRectangle, titleLabel);
//Set VBox properties
VBox vbox1 = new VBox(25);
vbox1.setAlignment(Pos.TOP_CENTER);
vbox1.setPadding(new Insets(60, 0, 0, 0));
vbox1.setStyle("-fx-background-color: lightskyblue");
HBox genderBtnBox = new HBox(25);
genderBtnBox.setAlignment(Pos.CENTER);
//Set Scene 1 buttons
Button enterNameBtn = new Button("Enter");
Button goToScene2Btn = new Button("Continue");
//Set Radio Button functionality here
final ToggleGroup genderGroup = new ToggleGroup();
RadioButton rb1 = new RadioButton("Boy");
rb1.setToggleGroup(genderGroup);
rb1.setUserData("Boy");
rb1.setSelected(true);
RadioButton rb2 = new RadioButton("Girl");
rb2.setToggleGroup(genderGroup);
rb2.setUserData("Girl");
//Add panes, labels and buttons to the VBox
vbox1.getChildren().addAll(root, nameLabel, nameTextField, enterNameBtn, genderLabel, genderBtnBox);
Scene scene = new Scene(vbox1, 500, 500);
primaryStage.setScene(scene);
primaryStage.setTitle("Culminating Project");
primaryStage.show();
/**
* ************************ SCENE 2 WORK *************************
*/
//THIS IS ROUGH WORK SO FAR
//Here, testing out new scene to see that it loads properly (and it does)
Circle testCircle = new Circle();
testCircle.setRadius(30);
testCircle.setFill(Color.YELLOW);
StackPane testPane = new StackPane(testCircle, testLabel);
Scene scene2 = new Scene(testPane, 500, 500);
/**
* ************************ EVENTS *************************
*/
//Stores user-entered name and prompts for user gender. Adds Continue button
enterNameBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if ((count < 1) && (!nameTextField.getText().isEmpty())) {
name = nameTextField.getText();
genderLabel.setText("Hi " + name + "! Please select whether you are a boy or girl.");
genderBtnBox.getChildren().addAll(rb1, rb2);
vbox1.getChildren().add(goToScene2Btn);
count++;
}
}
});
//When pressed, changes the scene so that scene 2 is set instead
goToScene2Btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.setScene(scene2);
}
});
//Radio button selection is stored in gender variable
genderGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#Override
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) {
if (genderGroup.getSelectedToggle() != null) {
gender = genderGroup.getSelectedToggle().getUserData().toString();
testLabel.setText(gender + " has been selected");
}
}
});
if (gender.equals("boy")){
{
}
}
else if (gender.equals("girl")){
{
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Now I have another class called CharacterGraphic, which I want to call and make the graphic I created in it appear.
package culminating;
& all the other imports
public class CharacterGraphic extends Culminating_JavaFX {
public void start(Stage primaryStage) throws Exception {
String gender = "boy";
Pane pane = new Pane();
pane.setStyle("-fx-background-color: LIGHTBLUE");
pane.setPrefSize(200, 200);
Circle head = new Circle();
head.setRadius(50);
head.setCenterX(240);
head.setCenterY(120);
head.setFill(Color.BURLYWOOD);
etc etc (all other graphics i made)
How do I do this???? And where would I do this?? Any answers really, really appreciated!
I have a grid pane which contains a number of buttons (normally something between 10 and 25) with five buttons per row (and however many are left in the last row). The number of buttons (and the buttons itself) might change during program execution. When that happens, the new buttons should be displayed. How can I achieve that? Here is a mini-example:
public class GridButtons extends Application {
List<String> buttonTexts = new ArrayList<>();
GridPane buttonGrid = new GridPane();
GridPane bgGrid = new GridPane();
#Override
public void start(Stage primaryStage) {
Button changeButtonsButton = new Button("Change BTNs");
changeButtonsButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
changeButtonTexts();
}
});
bgGrid.add(changeButtonsButton, 0, 0);
changeButtonTexts();
bgGrid.add(buttonGrid, 1, 0);
Scene scene = new Scene(bgGrid, 440, 140);
primaryStage.setScene(scene);
primaryStage.show();
}
public void updateButtonsGrid() {
buttonGrid = new GridPane();
for (int i = 0; i < buttonTexts.size(); i++) {
Button button = new Button(buttonTexts.get(i));
button.setMinWidth(70);
button.setMaxWidth(70);
buttonGrid.add(button, i % 5, i / 5);
System.out.println(buttonTexts.get(i));
}
// now the new GridPane should be displayed -> how?
}
public void changeButtonTexts() {
buttonTexts.clear();
Random random = new Random();
int buttonCount = random.nextInt(15) + 10;
for (int i = 0; i < buttonCount; i++) {
buttonTexts.add("Button " + i);
}
updateButtonsGrid();
}
public static void main(String[] args) {
launch(args);
}
}
Or are there better options than using a GridPane? Using a ListView<Button> and an ObservableList<Button> would work, but then the buttons are not displayed in a tabular form with five buttons in each row.
Creating a new GridPane is probably not what you want here. In the function updateButtonsGrid, change the line
buttonGrid = new GridPane();
to
buttonGrid.getChildren().clear();
If, for some reason, you are absolutely sure you need a new instance of GridPane, remove the old one from bgGrid, and add the new one. In your current code example the new instance of GridPane is never added to the scene graph.
For example, I created a button labeled "1". Whenever this button is pressed, 1 is appended to a textField. However, I can add 1 to a textField simply by typing 1 on my keyboard. When doing so, I'd like by button to get view as if it was pressed instead if a key.
I've been thinking thant may be it's possible to manage this issue in this way:
rootNode.setOnKeyTyped(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
textField.appendText(event.getCharacter());
if(event.getCharacter().equals("1")){
// here button should be pressed
}
}
});
Is there some method, that can change appearance of the button?
Thanks is advance.
#James_D , You program works correctly, but I connt apply your solution to my program. Maybe it's because I customized my buttons. Have a look at a part of my code:
HashMap<String, Button> buttons = new HashMap<>();
int btnVal = 1;
for(int j = 5 ; j >= 3; j --){
for(int i = 1; i <= 3; i ++){
Button btn = createNumberButton(Integer.toString(btnVal++), inputField, i, j);
rootNode.getChildren().add(btn);
buttons.put(Integer.toString(btnVal), btn);
}
}
rootNode.setOnKeyPressed(event -> {
Button btn = buttons.get(event.getText());
if (btn != null) {
System.out.println(event.getText());
btn.arm();
inputField.appendText(event.getText());
}
});
rootNode.setOnKeyReleased(event -> {
Button btn = buttons.get(event.getText());
if (btn != null) {
btn.disarm();
}
});
Use button.arm() (and button.disarm() to "release" it).
SSCCE:
import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class PressButtonOnTyping extends Application {
#Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
TextField textField = new TextField();
textField.setDisable(true);
root.add(textField, 0, 0, 3, 1);
Map<String, Button> buttons = new HashMap<>();
for (int i = 1 ; i <= 9; i++) {
Button button = createButton(i, textField);
buttons.put(Integer.toString(i), button);
root.add(button, (i-1)%3, 1+ (i-1) / 3);
}
Button zeroButton = createButton(0, textField);
root.add(zeroButton, 1, 4);
buttons.put("0", zeroButton);
root.setOnKeyPressed(e -> {
Button button = buttons.get(e.getText());
if (button!=null) {
button.arm();
textField.appendText(e.getText());
}
});
root.setOnKeyReleased(e -> {
Button button = buttons.get(e.getText());
if (button!=null) {
button.disarm();
}
});
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
private Button createButton(int value, TextField target) {
String s = Integer.toString(value);
Button button = new Button(s);
button.setOnAction(e -> target.appendText(s));
return button ;
}
public static void main(String[] args) {
launch(args);
}
}
I am new to stackoverflow and also to Java programming, though I do have some small programming background.
I am building a little program using JavaFX 8.0.0-b132 and JavaSE 8...
The program has a GUI and this GUI has a ChoiceBox. The architecture for the layout is:
AnchorPane - VBox - TitledPane - GridPane - HBox - ChoiceBox
I have modified the ChoiceBox extensively using CSS.
Everything is working as it should except for one problem:
When I start the program and open the ChoiceBox (per mouse click or with show()) for the first time, it will display it's ContextMenu at the bottom edge as it should; however, when I close and open it again (with mouse or programmatically) it will display the ContextMenu on top of the ChoiceBox open-button (wrong y position), such that the bottom of the currently selected cell coincides with the bottom of the open-button (meaning it will change position after I select another cell and open it again).
How do I prevent this and make the ContextMenu appear always at the bottom as it does at first?
Here is a snippet of the GUI part of the program with the affected control:
EDIT: problem solved: apparently this behavior is the intended ChoiceBox behavior; I posted in the JavaFX forums that it would be better to make the popup behave like that from the start i.e. the first time it is opened so as not to seem buggy.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.collections.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Side;
public class bugdemo extends Application {
// Elements needed in both threads
RadioButton radioBoxersOne = new RadioButton("One");
RadioButton radioBoxersTwo = new RadioButton("Two");
TextField warmupText = new TextField("skip warm up");
TextField noCyclesText = new TextField("3");
TextField cycleTimeText = new TextField("45");
TextField restingTimeText = new TextField("30");
String[] warmupTFtext = {"skip warm up", "\u00FCberspringen"};
public static void main(String... args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
// root container
AnchorPane anchor = new AnchorPane();
// Options TitledPane
GridPane trainingOptions = new GridPane();
ToggleGroup toggleGroup = new ToggleGroup();
radioBoxersOne.setToggleGroup(toggleGroup);
radioBoxersTwo.setToggleGroup(toggleGroup);
radioBoxersOne.setSelected(true);
HBox hbToggle = new HBox(15, radioBoxersOne, radioBoxersTwo);
hbToggle.setPadding(new Insets(5,0,0,0));
ObservableList<String> langList = FXCollections.observableArrayList("English", "Deutsch");
ChoiceBox<String> langBox = new ChoiceBox<String>(langList);
langBox.setValue("English");
HBox hbLangBox = new HBox(0, langBox);
hbLangBox.setPadding(new Insets(5,0,0,0));
Label warmupOptionsLabel = new Label("Warm up time (min): ");
Label noCyclesOptionsLabel = new Label("Number of cycles: ");
Label cycleOptionsLabel = new Label("Cycle time (sec): ");
Label restingOptionsLabel = new Label("Resting time (sec): ");
Label noBoxersOptionsLabel = new Label("Number of boxers: ");
Label langOptionsLabel = new Label("Language: ");
trainingOptions.add(warmupOptionsLabel, 0, 0);
trainingOptions.add(noCyclesOptionsLabel, 0, 1);
trainingOptions.add(cycleOptionsLabel, 0, 2);
trainingOptions.add(restingOptionsLabel, 0, 3);
trainingOptions.add(noBoxersOptionsLabel, 0, 4);
trainingOptions.add(langOptionsLabel, 0, 5);
trainingOptions.add(warmupText, 1, 0);
trainingOptions.add(noCyclesText, 1, 1);
trainingOptions.add(cycleTimeText, 1, 2);
trainingOptions.add(restingTimeText, 1, 3);
trainingOptions.add(hbToggle, 1, 4);
trainingOptions.add(hbLangBox, 1, 5);
TitledPane TPane_in = new TitledPane("Options", trainingOptions);
// Options TPane and Buttons together as UI (input)
VBox UI = new VBox(10, /* hbButtons ,*/ TPane_in);
anchor.getChildren().add(UI);
AnchorPane.setLeftAnchor(UI, 25.0);
AnchorPane.setTopAnchor(UI, 100.0);
// EventHandlers for the radio-buttons
radioBoxersOne.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
/* if (!singleBoxer){
trainingProgress.getChildren().remove(boxerLabel);
trainingProgress.getChildren().remove(boxerText);
}
changeTextFlow();
singleBoxer = true; */
}
});
radioBoxersTwo.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
/* singleBoxer = false;
trainingProgress.add(boxerLabel, 0, 3);
trainingProgress.add(boxerText, 1, 3);
changeTextFlow(); */
}
});
// "EventHandler" for the language ChoiceBox
langBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue ov, Number value, Number new_value) {
/* language = (int)new_value;
changeLanguage(); */
}
});
// initialize scene and show stage
Scene scene = new Scene(anchor, 800, 650);
primaryStage.setScene(scene);
primaryStage.setResizable(false); // resizeable on/off
// import CSS file
// scene.getStylesheets().add(Letsbox.class.getResource("Training.css").toExternalForm());
primaryStage.show();
langBox.show();
langBox.hide();
langBox.show();
}
}
In this example no CSS is used, but the problem persists...
I tried to fix it with:
/* langBox.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
ContextMenu cm = langBox.getContextMenu();
cm.show(cm.getOwnerNode(), Side.BOTTOM, 0.0, 0.0);
}
}); */
but it throws a NullPointerException...
Please tell me how I could fix this :)
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group(), 450, 250);
Button notification = new Button();
notification.setPrefSize(80, 60);
notification.setAlignment(Pos.CENTER);
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item2 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item3 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item4 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item5 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item6 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item7 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item8 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
final ContextMenu contextMenu = new ContextMenu(item1, item2,item3, item4,item5, item6,item7, item8);
contextMenu.setMaxSize(50, 50);
contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("showing");
}
});
contextMenu.setOnShown(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("shown");
}
});
// contextMenu.hide();
notification.setContextMenu(contextMenu);
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("To: "), 0, 0);
grid.add(notification, 1, 0);
Group root = (Group) scene.getRoot();
root.getChildren().add(grid);
stage.setScene(scene);
stage.show();
notification.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me)->{
if(me.getButton()==MouseButton.PRIMARY ){
System.out.println("Mouse Left Pressed");
System.out.println(notification.getScaleX());
System.out.println(notification.getScaleY());
System.out.println(me.getScreenX());
System.out.println(me.getScreenY());
contextMenu.show(notification,Side.TOP,0,0);
//contextMenu.show(notification,me.getScreenX(),me.getScreenY());
}else{
contextMenu.hide();
}
});
}
}
I use JavaFX, project has 2 Anchorpane (pane and paneDrop) , one of them has a button, when I drag this button create new button , when dragdropped setOnDragDetected method to new button. Problem is, when I try drag one of new butoons only last creating button move.
#FXML
private Button source;
#FXML
private AnchorPane pane;
#FXML
private AnchorPane paneDrop;
private Button b;
int i = 1;
int moveI = 0;
#FXML
private void dragDetected(MouseEvent event) {
System.out.println("onDragDetected");
b = new Button(i + "");
i++;
pane.getChildren().add(b);
b.setLayoutX(source.getLayoutX());
b.setLayoutY(source.getLayoutX());
/* drag was detected, start drag-and-drop gesture*/
paneDrop.setOnDragOver(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent k) {
paneDrop.setOnDragDropped(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent k1) {
System.out.println("ondragExited");
pane.getChildren().remove(b);
paneDrop.getChildren().add(b);
b.setLayoutX(k1.getSceneX() - paneDrop.getLayoutX());
b.setLayoutY(k1.getSceneY() - paneDrop.getLayoutY());
b.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent t) {
/* drag was detected, start a drag-and-drop gesture*/
/* allow any transfer mode */
paneDrop.setOnDragOver(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent t) {
System.out.println("ondragOver");
paneDrop.setOnDragDropped(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent t1) {
System.out.println("ondragOver");
if (t1.getGestureSource() != pane
&& t1.getDragboard().hasString()) {
t1.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
t1.consume();
}
});
b.setLayoutX(t.getSceneX());
b.setLayoutY(t.getSceneY() - 224);
if (t.getGestureSource() != pane
&& t.getDragboard().hasString()) {
t.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
t.consume();
}
});
Dragboard db = b.startDragAndDrop(TransferMode.MOVE);
System.out.println("ttttttttttttt");
/* Put a string on a dragboard */
ClipboardContent content = new ClipboardContent();
content.putString(b.getText());
db.setContent(content);
t.consume();
}
});
k1.consume();
}
});
System.out.println("ondragOver");
b.setLayoutX(k.getSceneX() - pane.getLayoutX());
b.setLayoutY(k.getSceneY() - pane.getLayoutY());
if (k.getGestureSource() != pane
&& k.getDragboard().hasString()) {
k.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
k.consume();
}
});
/* allow any transfer mode */
Dragboard db = source.startDragAndDrop(TransferMode.MOVE);
/* put a string on dragboard */
ClipboardContent content = new ClipboardContent();
//System.out.println(source.getText());
content.putString(source.getText());
db.setContent(content);
event.consume();
}