I can't click on the buttons - java

I'm making a sales system, and in the area where the total sums are described, I can't click on the buttons.
I tried to remove buttons from the gridpane and they work for me but when I integrate them into the gridpane they lose focus or stop working
Pane root = new Pane();
Separator top = new Separator(Orientation.HORIZONTAL);
top.setPrefWidth(1310);
Separator left = new Separator(Orientation.VERTICAL);
left.setPrefHeight(150);
left.setPadding(new Insets(0,0,10,0));
Separator low = new Separator(Orientation.HORIZONTAL);
low.setPadding(new Insets(140,10,0,0));
low.setPrefWidth(1323);
Separator right = new Separator(Orientation.VERTICAL);
right.setPrefHeight(150);
right.setPadding(new Insets(0,0,10,1310));
Label descuento = new Label("Descuento: ");
Label sub = new Label("Sub-Total: ");
Label iva = new Label("Iva: ");
Label total = new Label("Total: ");
Label rsub = new Label("0000.00");
Label riva= new Label("16%");
Label rtotal = new Label("0.0000");
Button btn_generate = new Button("generate");
MenuItem opc = new MenuItem("Opc");
MenuItem opc1= new MenuItem("opc1");
MenuButton menu1 = new MenuButton("Type",null,opc,opc1);
MenuItem opc2 = new MenuItem("opc2");
MenuItem opc3 = new MenuItem("opc3");
MenuButton menu2 = new MenuButton("Opc",null,opc2,opc3);
GridPane Gtotal = new GridPane();
Gtotal.setHgap(20);
Gtotal.setVgap(10);
Gtotal.setPadding(new Insets(10,0,0,10));
ColumnConstraints column1 = new ColumnConstraints(rtotal.getPrefWidth(),rtotal.getPrefWidth(),rtotal.getPrefWidth());
ColumnConstraints column2 = new ColumnConstraints(total.getPrefWidth()+450,total.getPrefWidth()+450,total.getPrefWidth()+450);
ColumnConstraints column3 = new ColumnConstraints(riva.getPrefWidth(),riva.getPrefWidth(),riva.getPrefWidth());
ColumnConstraints column4 = new ColumnConstraints(iva.getPrefWidth()+330,iva.getPrefWidth()+330,iva.getPrefWidth()+330);
ColumnConstraints column5 = new ColumnConstraints(rsub.getPrefWidth(),rsub.getPrefWidth(),rsub.getPrefWidth());
ColumnConstraints column6 = new ColumnConstraints(sub.getPrefWidth(),sub.getPrefWidth(),sub.getPrefWidth());
Gtotal.getColumnConstraints().add(0,column1);
Gtotal.getColumnConstraints().add(0,column2);
Gtotal.getColumnConstraints().add(0,column3);
Gtotal.getColumnConstraints().add(0,column4);
Gtotal.getColumnConstraints().add(0,column5);
Gtotal.getColumnConstraints().add(0,column6);
GridPane.setConstraints(descuento, 0, 0);
GridPane.setConstraints(menu1, 1, 0);
GridPane.setConstraints(menu2, 2, 0);
GridPane.setConstraints(sub, 0, 1);
GridPane.setConstraints(rsub, 1, 1);
GridPane.setConstraints(iva, 2, 1);
GridPane.setHalignment(iva, HPos.RIGHT);
GridPane.setConstraints(riva, 3, 1);
GridPane.setConstraints(total, 4, 1);
GridPane.setHalignment(total, HPos.RIGHT);
GridPane.setConstraints(rtotal, 5, 1);
GridPane.setConstraints(btn_generate, 5, 2);
Gtotal.getChildren().addAll(descuento,sub,rsub,iva,riva,total,rtotal,btn_generate,menu1,menu2);
root.getChildren().addAll(top,left,Gtotal,low,right);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
I hope that when running the program I can click on the buttons.

Marcos Antonio You are really making work for yourself not using Scene Builder
That said here is 3 lines of code that make a button fire an event
Button btnOK = new Button("OK");
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction((ActionEvent evt) -> {

The problem was the separators that overlapped the other layout by the padding

Related

How can I add content from a helper method (including buttons, textfields, actions) to a tab created in a stage?

public void employeeMenu()
{
//ADD LOG OUT BUTTON SOMEWHERE
//setting stage
StackPane empMenuStage = new StackPane();
VBox empBox = new VBox(new Label("Employee Menu"));
empMenuStage.getChildren().add(empBox);
Scene empScene = new Scene(empMenuStage, 500, 500);
Stage empStage = new Stage();
empStage.setScene(empScene);
empStage.setTitle("Employee Menu");
TabPane tabPane = new TabPane();
Tab bookingReport = new Tab("Booking Report");
Tab checkOutGuest = new Tab("Check Out Guest");
Tab createGuestAccount = new Tab("Create Guest Account");
Tab editGuestInformation = new Tab("Edit Guest Info");
Tab createNewEmpAccount = new Tab("Create new Employee Account");
Tab editEmployeeAccount = new Tab("Edit Employee Account");
Tab editRoomStatus = new Tab("Edit Room Status");
tabPane.getTabs().add(bookingReport); //mitchell
tabPane.getTabs().add(checkOutGuest); //emma
tabPane.getTabs().add(createGuestAccount); //joey
tabPane.getTabs().add(editGuestInformation);
tabPane.getTabs().add(createNewEmpAccount);
tabPane.getTabs().add(editEmployeeAccount);
tabPane.getTabs().add(editRoomStatus);
VBox tabBox = new VBox(tabPane);
Scene scene = new Scene(tabBox, 500, 500);
empStage.setScene(scene);
empStage.setTitle("Employee Menu");
empStage.close();
empStage.show();
}
public Tab displayEmpMenu(Stage empStage)
{
//button for check out a value guest
//then enter name of value guest
//Enter room of value guest
//Confirmation Message (pick Y or N)
Tab test = new Tab("hi");
Button valueGuest = new Button("Check out Value Guest");
Button generalGuest = new Button("Check out General Guest");
valueGuest.setOnAction(e -> checkOutVG());
generalGuest.setOnAction(e -> checkOutGeneral());
GridPane layout = new GridPane();
layout.setPadding(new Insets(10, 10, 10, 10 ));
layout.setVgap(8);
GridPane.setConstraints(valueGuest, 0, 0);
GridPane.setConstraints(generalGuest,0, 1 );
layout.getChildren().addAll(valueGuest, generalGuest);
Scene scene = new Scene(layout, 650, 200 );
empStage.setScene(scene);
empStage.setTitle("Check Out Guest");
empStage.show();
//button for check out a general guest
//button for return to employee menu
return test;
}
public void checkOutVG()
{
TextField vgName = new TextField("Enter name of value guest");
String vgN = vgName.getText();
Label name = new Label("Value Guest Name");
TextField vgRoom = new TextField("Enter room of value guest");
Label roomNum = new Label("Value Guest Room");
double room = Double.parseDouble(vgRoom.getText());
int room1 = (int)room;
freeThisRoom(room1);
Label vgRoom1 = new Label("Value Guest Room");
TextField vgconfirm = new TextField("Confirmation: "
+ vgN + " is checking out of "
+ room1 + "(Y/N)");
vgconfirm.getText();
String text = "";
if (vgconfirm.getText().equalsIgnoreCase("Y"))
{
text = "Done. " + vgN + "is checked out of "
+ room1;
}
else
{
text = "You entered no. Please re-enter information.";
}
}
public void checkOutGeneral()
{
TextField name = new TextField("Enter name of general guest");
Label ggname = new Label("General Guest Name");
String name1 = name.getText();
TextField room = new TextField("Enter room number of general guest");
Label roomNum = new Label("General Guest Room");
double ggNum = Double.parseDouble(room.getText());
int ggRoom = (int)ggNum;
freeThisRoom(ggRoom);
TextField vgconfirm = new TextField("Confirmation: "
+ name + " is checking out of "
+ ggRoom + "(Y/N)");
vgconfirm.getText();
String text = "";
if (vgconfirm.getText().equalsIgnoreCase("Y"))
{
text = "Done. " + name + "is checked out of "
+ ggRoom;
}
else
{
text = "You entered no. Please re-enter information.";
}
}
I need help adding this content to the check out guest tab in the employee menu method. I'm new to javafx so if anyone could help me figure this out it would be greatly appreciated! I'm not sure what I am doing wrong or what I need to fix but I'm kind of lost. I've been scouring the web/textbooks for a solution but either I'm missing something big or I haven't come across the solution yet.

Is there a simple way to create several TextFields with JavaFX?

I am attempting to create about a dozen TextFields and was wondering if there is a quick and easy way to do this.
tfTime1 = new TextField();
tfActivity1 = new TextField();
tfTime2 = new TextField();
tfActivity2 = new TextField();
tfTime3 = new TextField();
tfActivity3 = new TextField();
tfTime4 = new TextField();
tfActivity4 = new TextField();
tfTime5 = new TextField();
tfActivity5 = new TextField();
tfTime6 = new TextField();
tfActivity6 = new TextField();
I feel like there's a more efficient way of doing this that I am not aware of
You could use a simple for loop to add new TextFields to a List:
List<TextField> timeTextFields = new ArrayList<>();
for (int i = 0; i < 5; i++) {
timeTextFields.add(new TextField("Time #" + i));
}

JavaFX Input from Buttons

I'm creating a simple ATM machine GUI using javaFX but I can't seem to know how to take an input from a button. For instance, I created a virtual number pad but I don't know how to take input from clicked buttons.
public static void displayNumbers() {
Stage window2 = new Stage();
GridPane grid = new GridPane();
window2.setTitle("Enter Value");
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button enterButton = new Button("Enter");
grid.add(one, 0, 0);
grid.add(two, 1, 0);
grid.add(three, 2, 0);
grid.add(four, 0, 1);
grid.add(five, 1, 1);
grid.add(six, 2, 1);
grid.add(seven, 0, 2);
grid.add(eight, 1, 2);
grid.add(nine, 2, 2);
grid.add(zero, 1, 3);
grid.add(enterButton, 2, 4);
}
To make a Button do something in JavaFX, you need to add an EventHandler. For example, let's tell button one to print something out to console:
one.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("You just clicked Button #1!");
}
});
This can be simplified with a lambda expression in JDK8+:
one.setOnAction(event -> System.out.println("You just clicked Button #1!"));
If you need to execute more than one line of code when clicking the button, just use a code block instead:
one.setOnAction(event -> {
// Do lots of things
});

incompatible types: Scene cannot be converted to Parent JavaJDK netbeans

i have create a simple form with java jdk that contain a button, when user click the button it show the 2nd form and close the 1st form, but i have error like this in netbeans:
error: incompatible types: Scene cannot be converted to Parent
Scene secondScene = new Scene(scene2, 300,250);
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
now the full code of start :
public void start(Stage primaryStage) {
GridPane grid1 = new GridPane();
Button butt1 = new Button();
butt1.setText("Applique KPPV");
grid1.add(butt1, 2, 6);
Scene scene1 = new Scene(grid1, 300, 250);
primaryStage.setTitle("KNN With JDK");
primaryStage.setScene(scene1);
primaryStage.show();
butt1.setOnAction(e->{
GridPane grid = new GridPane();
// Labels & inputs Settings
Label number1 = new Label("Data 1 :");
Label number2 = new Label("Data 2 :");
Label number3 = new Label("Data 3 :");
Label number4 = new Label("Data 4 :");
Label number5 = new Label("Data 5 :");
Label numberk = new Label("K :");
Label resultat1 = new Label("The Result");
TextField txt1 = new TextField();
TextField txt2 = new TextField();
TextField txt3 = new TextField();
TextField txt4 = new TextField();
TextField txt5 = new TextField();
TextField txtk = new TextField();
grid.add(number1, 0, 0);
grid.add(number2, 0, 1);
grid.add(number3, 0, 2);
grid.add(number4, 0, 3);
grid.add(number5, 0, 4);
grid.add(numberk, 0, 5);
grid.add(resultat1, 0, 7);
grid.add(txt1, 1, 0);
grid.add(txt2, 1, 1);
grid.add(txt3, 1, 2);
grid.add(txt4, 1, 3);
grid.add(txt5, 1, 4);
grid.add(txtk, 1, 5);
Button butt = new Button();
butt.setText("Applique KPPV");
grid.add(butt, 0, 6);
// My Main Method
///////////////////////////////////
// ADDED THESE 5 LINES. NOW IT SHOWS A WINDOW WITH THESE VALUES FILLED IN
txt1.setText("0.0");
txt2.setText("0.0");
txt3.setText("0.0");
txt4.setText("0.0");
txt5.setText("0.0");
txtk.setText("10");
double insertedInt = Double.parseDouble(txt1.getText());
double insertedInt2 = Double.parseDouble(txt2.getText());
double insertedInt3 = Double.parseDouble(txt3.getText());
double insertedInt4 = Double.parseDouble(txt4.getText());
double insertedInt5 = Double.parseDouble(txt5.getText());
///////////////////////////////////
Scene scene = new Scene(grid, 300, 250);
// Final Action
butt.setOnAction(event -> {
double[] query = { insertedInt, insertedInt2, insertedInt3, insertedInt4, insertedInt5 };
int k = Integer.parseInt(txtk.getText());
List<City> cityList = new ArrayList<City>();
List<Result> resultList = new ArrayList<Result>();
cityList.add(new City(instances[0], "IRIS0"));
cityList.add(new City(instances[1], "IRIS1"));
cityList.add(new City(instances[2], "IRIS2"));
cityList.add(new City(instances[3], "IRIS3"));
cityList.add(new City(instances[4], "IRIS4"));
cityList.add(new City(instances[5], "IRIS5"));
cityList.add(new City(instances[6], "IRIS6"));
cityList.add(new City(instances[7], "IRIS7"));
cityList.add(new City(instances[8], "IRIS8"));
cityList.add(new City(instances[9], "IRIS9"));
// find distances
for (City city : cityList) {
double dist = 0.0;
for (int j = 0; j < city.cityAttributes.length; j++) {
dist += Math.pow(city.cityAttributes[j] - query[j], 2);
}
double distance = Math.sqrt(dist);
resultList.add(new Result(distance, city.cityName));
}
Collections.sort(resultList, new DistanceComparator());
String[] ss = new String[k];
for (int x = 0; x < k; x++) {
ss[x] = resultList.get(x).cityName;
}
String majClass = findMajorityClass(ss);
// System.out.println("The Nearest IRIS Class is : "+majClass);
resultat1.setText(majClass);
});
Scene secondScene = new Scene(scene, 500,500);
Stage secondStage = new Stage();
secondStage.setScene(secondScene); // set the scene
secondStage.setTitle("Second Form");
secondStage.show();
primaryStage.close(); // close the first stage (Window)
});
}

How to center a Button using a FormLayout in SWT?

How can I position a Button (vertically) at the center using a FormLayout (see here)? Using:
final Button button = new Button(shell, SWT.NONE);
button.setText("Button");
final FormData layoutData = new FormData();
layoutData.top = new FormAttachment(50);
layoutData.left = new FormAttachment(0);
layoutData.right = new FormAttachment(100);
button.setLayoutData(layoutData);
I end up with
Which is not surprising, since I told it to put the top of the button at the center (layoutData.top = new FormAttachment(50);). How can I instead put the center of the button at the center?
You can specify an offset with the constructor:
new FormAttachment(int numerator, int offset)
Looks like this:
You can compute the offset using:
final Button button = new Button(shell, SWT.NONE);
button.setText("Button");
final FormData layoutData = new FormData();
/* Compute the offset */
int offset = -button.computeSize(SWT.DEFAULT, SWT.DEFAULT).y / 2;
/* Create the FormAttachment */
layoutData.top = new FormAttachment(50, offset);
layoutData.left = new FormAttachment(0);
layoutData.right = new FormAttachment(100);
button.setLayoutData(layoutData);

Categories