java - Matrix of Buttons (How to Create and Arrange) in JavaFX -


This is my first program written in JavaFX (as opposed to swing). I'm just starting small by displaying a data set in a matrix (I do not want to use any table yet).

  Package MatrixGivex; 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.StackPane; Import javafx.stage.Stage; Import java.util.Random; // Random Number Required for Generation ** ** * Author / Matt * / Public Category Matrix increases the FX application {button [] [] matrix; // Grid of names @ Override Public Zero Start (Stage Primary Stage) {int SIZE = 10; Int length = size; Int width = SIZE; Stackpan root = new stackpan (); Matrix = new button [width] [length]; // allocates the size of matrix // and one is embedded for the loop to fill the matrix size and these buttons are added to the matrix (int y = 0; y & lt; length; Y + +) {For (int x = 0; x & lt; width; x ++) {random rand = new random (); // creates a new random object to generate random numbers / / the following variables are generated using random object rand. // rand.nextInt (2) comes from 0.000 to 0.999 with a double. // Setting the target variable to be an integer decreases the range from 0 - 1. int rand1 = rand.nextInt (2); // Announce the random integer and start (0 - 1) + 1 matrix [x] [y] = new button (/ * "(" + rand1 + ")" * /); // new random binary button matrix [x] [y] .setText ("(" + + rand1 + ")"); // matrix matrix sets the text inside [x] [y] .setOnAction (new event handler & lt; action event & gt; () {@Override public void handle (ActionEvent event) {System.out.println ("Random Binary Matrix (JavaFX) ");}}); Root.getChildren () add (matrix [x] [y]). }} Visual View = New View (Route, 500, 500); PrimaryStage.setTitle ("Random Binary Matrix (JavaFX)"); PrimaryStage.setScene (view); PrimaryStage.show (); } / ** * @complete command line argument * / public static zero main (string [] args) {greeting (); // congratulates the user and tells the purpose of program launch (args); // opens a window that prepares and displays the matrix / new matrixasing uiI (width, length); // Creates new MatrixSwing with 2 metrics (thanksgiving); // thank you user and indicates that the program is about to close) Public Static Zero Greetings () {// Congratulates the user and the purpose of the program is System.out.println ("Welcome to the Random Matrix Generator \ n" ); System.out.println ("The purpose of this program is to generate and display" + 10 \ 10 10 matrix of random 0, and 1 ".);} Public static void thanks () // Thank the user and indicates Thank you for closing the program {System.out.println ("\ nThank you for using Goodbye and Random Matrix Generators \ n \ n");} /} and / and Thanksgiving Method}  

The heart of the problem seems to be the following line, because by default all buttons are in the center of the pane Stacking on top of each other.

StackPan Root = New StackPan ();

He said, for all lengths and width I would like a button to align with the side, which will result in an N-x matrix. What should be done to accomplish that? Does the additional javafx.scene. * Files need to be imported?

< P>

The basic problem is that you choose the layout project You can get a deeper knowledge about layouts, support for biophysics

StackPan stack all nodes which you pass by I will give you the gridpan I suggest using the encod of stackpan

  GridPay Root = New GridPain ();  

To add nodes, use

  root.add (matrix [x] [y], y, x);  PS   PS  I also recommend you to rename your button, advise you to give more clarity on the line of matrix and button representing the column. You can use it as something, though its completely optional! :)  
  matrix [x] [y] .set text (x + "," + y);  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -