Creating two Button Tables in JavaFX - java

What i would like is to create two arrays of Buttons like this picture.
What i have done so far :
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
Scene scene = new Scene(grid2, 1000, 1000);
grid.setPadding(new Insets(10, 10, 10, 10));
for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
grid.add(new Button(), i,j);
}
public static void main(String[] args) {
launch(args);
}
}
This creates the first desired array starting from the top left corner.However i can't seem to be able to place the second one.Adding a button in a location like (40,40) puts it in a different place.How could this be done?

Related

JavaFX change label after button click

im trying to show one line of matrix each time. But when button is pressed i want to show next line of that matrix. My idea was that i show line with index "index" and create action on button press that add 1 to variable "index". It doesnt semms to be good idea, because its not working. Its showing only the first line, and never changes.
public class GUI extends Application {
int index = 0;
public static int save[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}};
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Label label = new Label(Arrays.toString(save[index]));
Button next = new Button();
next.setText("Next");
next.setOnAction(e -> {
dalsi();
});
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(8);
grid.setHgap(10);
GridPane.setConstraints(label, 5, 6);
GridPane.setConstraints(next, 6, 13);
grid.getChildren().addAll(label, next);
Scene scene = new Scene(grid, 250, 180);
primaryStage.setScene(scene);
primaryStage.setTitle("QuickSort");
primaryStage.show();
}
public void dalsi() {
if (index < Quicksort.delka - 1) {
index++;
}
}
}
To make label text change, you need to call setText on the label when you want the text to change.
Make your label a member variable for the class, then write:
label.setText(
Arrays.toString(save[index])
);
after you call index++ in your dalsi() method.

javafx events not working

public class Testing extends Application {
#Override
public void start(Stage stage)
{
Button button1 = new Button("First button");
Button button2 = new Button("Second button");
EventHandler<ActionEvent> aHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event)
{
button2.setText("Working");
}
};
button1.addEventHandler(ActionEvent.ACTION, aHandler);
HBox hbox = new HBox(40,button1, button2);
Scene scene = new Scene(hbox, 840, 400);
stage.setScene(scene);
stage.setTitle("Testing");
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
You can see that this is a javafx Testing class where I am testing eventHandlers and it works fine but when I split the code and add it into on its own methods then the eventHandlers does not work like in the code below
public class Testing extends Application {
#Override
public void start(Stage stage)
{
EventHandler<ActionEvent> aHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event)
{
button2().setText("Working");
}
};
button1().addEventHandler(ActionEvent.ACTION, aHandler);
stage.setScene(scene());
stage.setTitle("Testing");
stage.show();
}
public Button button1()
{
Button btn = new Button("First button");
return btn;
}
public Button button2()
{
Button btn = new Button("Second button");
return btn;
}
public HBox hbox()
{
HBox hbox = new HBox(40,button1(), button2());
return hbox;
}
public Scene scene()
{
Scene scene = new Scene(hbox(), 840, 400);
return scene;
}
public static void main(String[] args)
{
launch(args);
}
}
Now this code does not work. Please help.
Please note: If any one have another idea to encapsulate eventHandlers then please mention it if you can because my goal is to define eventHandlers in one class and registering it in another class.
Thank you.
Of course it's not working, you are creating an instance of Button every call to button1() and button2(). The instance of button1 and button2 in the HBox are different instances from the one you added the event handler.
I definitely recommend not splitting like what you are doing. This kind of splitting makes it hard to troubleshoot problems, and you are creating new instances whenever you call any of those methods. Stick to what you are doing originally.

JavaFX ScrollPane setVvalue() not working properly

I am testing the JavaFX ScrollPane class and realized that it is not working as I expect, I don't know why. I have the following code:
public class Client3 extends Application {
int indexMsg = 0;
Button send;
GridPane root;
ScrollPane msgPane;
GridPane msgPaneContent;
FlowPane writePane;
TextField writeMsg;
Scene scene;
#Override
public void start(Stage primaryStage) {
root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setVgap(10);
root.setPadding(new Insets(10, 10, 10, 10));
msgPane = new ScrollPane();
msgPane.setPrefSize(280, 280);
msgPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
msgPaneContent = new GridPane();
msgPaneContent.setPrefWidth(270);
msgPaneContent.setVgap(10);
writePane = new FlowPane(10, 10);
writePane.setAlignment(Pos.CENTER);
writePane.setPrefWidth(280);
writeMsg = new TextField();
writeMsg.setPrefWidth(150);
writeMsg.setPromptText("Write your message");
writePane.getChildren().add(writeMsg);
GridPane.setConstraints(msgPane, 0, 0);
GridPane.setConstraints(writePane, 0, 1);
msgPane.setContent(msgPaneContent);
root.getChildren().addAll(msgPane, writePane);
writeMsg.setOnAction((ev) -> {
if (!writeMsg.getText().isEmpty()) {
TextArea msg = new TextArea(writeMsg.getText());
msg.setMaxWidth(135);
msg.setPrefRowCount(msg.getLength() / 21 + 1);
msg.setWrapText(true);
GridPane.setConstraints(msg, 0, indexMsg);
indexMsg++;
writeMsg.deleteText(0, writeMsg.getText().length());
msgPaneContent.getChildren().add(msg);
msgPane.setVvalue(1.0);
}
});
scene = new Scene(root, 300, 300);
primaryStage.setTitle("Chat App");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Basically, I have a GridPane as the root with a ScrollPane and a GridPane as its children. The ScrollPane has a children GridPane. There is a TextField with an EventHandler which generates a TextArea inside the GridPane (the ScrollPane's children). Each TextArea object is created in the vertical direction, downwards. I want to set the scrollbar always at its maximum value (setVvalue(1.0)) each time a new TextArea is added. The thing is that it doesn't seem to work as it should because the vertical value is never set to the maximum after handling the event, but it seems to be set to the maximum value that it had before handling it (the bottom of the previous TextArea added).
Any solution for this? Thanks in advance.

Padding button and rectangle

My button is located under the rectangle. It is currently like in the picture on the left. I want to pad the button like in the picture on the right.
How can I do that? Thanks for reading.
Here's my code.
public class Rect extends Application {
private Button btn1 = new Button("Rotate");
private Rectangle r1 = new Rectangle(50, 100);
#Override
public void start(Stage primaryStage) throws Exception {
GridPane gridPane = new GridPane();
r1.setStroke(Color.BLACK);
r1.setFill(Color.WHITE);
gridPane.add(r1, 0, 0);
gridPane.add(btn1, 0, 1);
gridPane.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);
Scene scene = new Scene(gridPane,200,200);
primaryStage.setTitle("RotateRectangleFX");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
At the moment you are doing this:
gridPane.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);
You should be setting the alignment of r1 instead like follows:
r1.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);

Padding between pagination control and table

I have a JavaFX table with pagination control on which I applied padding.
pagination.setPadding(new Insets(5, 5, 0, 5));
I added padding, but in this case the pagination control and table are very close to each other. How I can add some space between them?
I just tried and it's odd that this doesn't work. A quick solution / workaround is to put both the TableView and the Pagination into a VBox and add spacing.
Like this:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
TableView tv = new TableView();
Pagination pg = new Pagination();
VBox root = new VBox();
root.setSpacing(10);
root.getChildren().addAll( tv, pg);
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

Categories