0

I am trying to figure this out and googled a lot. But I couldn't find anything.

This is the first time I am asking a question on StackOverflow.

Please bear with me if I couldn't explain my question properly.

ANY HELP WOULD BE APPRECIATED.

Here is my code:

package Main_Game;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import Models.character;
import Models.gameObject;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class leaderBoard
{
    private static int id = 0;

    leaderBoard()
    {
        //Stage created
        Stage primaryStage = new Stage();

        //root pane
        BorderPane root = new BorderPane();

        GridPane gridPane = new GridPane();
        gridPane.setHgap(5);

        //grid pane for top header
        GridPane gridHeader = new GridPane();
        gridHeader.setHgap(140);

        root.setMargin(gridHeader, new Insets(30,0,0,0));

        //scene created with borderpane as root
        Scene scene = new Scene(root,1200,700);

        //setting background of whole scene
        final String BACKGROUND = "-fx-background-color: #2A2927;";

        //setting style of header
        final String HEADER_STYLE = "-fx-font-family: 'Bungee Shade';"
                + "-fx-font-size: 40;";

        final String TEXT_STYLE = "-fx-font-size: 25;"
                + "-fx-font-family:  'Calibri';";

        root.setStyle(BACKGROUND);

        //Header for rank text
        Text rankHeader = new Text();
        rankHeader.setText("Rank#");
        rankHeader.setFill(Color.YELLOW);
        rankHeader.setStyle(HEADER_STYLE);

        //Header for name text
        Text nameHeader = new Text();
        nameHeader.setText("Name");
        nameHeader.setFill(Color.YELLOW);
        nameHeader.setStyle(HEADER_STYLE);

        //header for score text
        Text scoreHeader = new Text();
        scoreHeader.setText("Score");
        scoreHeader.setFill(Color.YELLOW);
        scoreHeader.setStyle(HEADER_STYLE);

        //
        Text rank = new Text();
        rank.setStyle(TEXT_STYLE);
        rank.setFill(Color.WHITE);

        Text name = new Text();
        name.setStyle(TEXT_STYLE);
        name.setFill(Color.WHITE);

        Text score = new Text();
        score.setStyle(TEXT_STYLE);
        score.setFill(Color.WHITE);

        //totalScore variable gets total score from another java file
        String totalScore = Integer.toString(gameObject.getTotalScore());

        //playerName variable gets character's name from another java file
        String playerName = character.getName();

        //Using BufferedWriter to write playerName and totalScore in a text file
        BufferedWriter bw = null;

        try
        {
            File file = new File("score.txt");

            //if file does not exit it will create a new file
            if(!file.exists())
            {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file,true);
            bw = new BufferedWriter(fw);


            /*Want to increment ID everytime file gets written but that's not main concern for now
             * 
             * file gets written with playerName and totalScore 
             * 
             * I have used \t to create space between ID, name and score
             */

            bw.write(id++ + "\t\t\t\t\t\t\t\t" + playerName + "\t\t\t\t\t\t\t" + totalScore);


            //After every line....another line starts with new line
            bw.newLine();

            bw.flush();
            bw.close();

        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

        /*
         * Using BufferedReader to read from file and outputting it in my scene
         */

        BufferedReader br = null;

        try
        {
            FileReader fr = new FileReader("score.txt");
            br = new BufferedReader(fr);


            String line;
            while((line = br.readLine()) != null)
            {
                //Splitting words so that I can print out ID, Name and Score
                //If i don't use split it just prints out score...because score gets written at last in file
                String[] words = line.split(",");
                for(int i =0; i<words.length; i++)
                {
                    name.setText(words[i]);

                    /*
                     * HERE IS THE PROBLEM
                     * Every time it reads line from text file
                     * I add it to gridPane below Header
                     * but i want to add rows dynamically but i get illegalArgumentException
                     */
                    gridPane.add(name,2,1);
                }
            }


            br.close();

        }
        catch(IOException e)
        {
            e.printStackTrace();
        }


        /*
         * IGNORE THIS PART
         * I tried using Scanner but it won't work
         */

//      Scanner sc = null;
//      try 
//      {
//          File file = new File("score.txt");
//          sc = new Scanner(file);
//      }
//      catch(IOException e)
//      {
//          e.printStackTrace();
//      }
//      
//      while(sc.hasNextLine())
//      {
//          Scanner sc2 = new Scanner(sc.nextLine());
//          while(sc2.hasNext())
//          {
//              String s = sc2.next();
//              score.setText(s);
//          }
//      }

        gridHeader.add(rankHeader, 0, 0);
        gridHeader.add(nameHeader, 1, 0);
        gridHeader.add(scoreHeader, 2, 0);

        root.setTop(gridHeader);
        root.setCenter(gridPane);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Basket Bud");
        primaryStage.show();
    }
}
Utsav Dave
  • 85
  • 1
  • 3
  • 9

1 Answers1

1

You are using the adding the same object multiple times. i.e. You are adding the same "name" variable to the pane a big no no in Javafx a better way to do this is

ArrayList<Text> myText = new ArrayList<Text>();  
try
        {
            FileReader fr = new FileReader("score.txt");
            br = new BufferedReader(fr);


            String line;

            while((line = br.readLine()) != null)
            {
                //Splitting words so that I can print out ID, Name and Score
                //If i don't use split it just prints out score...because score gets written at last in file
                String[] words = line.split(",");

                for(int i =0; i<words.length; i++)
                {
Text temp = new Text();
                    temp.setText(words[i]);
temp.setStyle(TEXT_STYLE);
        temp.setFill(Color.WHITE);
myText.add(temp) //allows you to edit these later 
                    /*
                     * HERE IS THE PROBLEM
                     * Every time it reads line from text file
                     * I add it to gridPane below Header
                     * but i want to add rows dynamically but i get illegalArgumentException
                     */
                    gridPane.add(temp,2,i+1);
                }
            }


            br.close();

        }

Sorry for bad indention impossible to do on a phone

If this doesn't work tell me

Austin
  • 726
  • 4
  • 22
  • Oh, you mean, the OP is adding the same instance of `Text` to the container, not the "same name variable" ... because that didn't make sense at first :P – MadProgrammer Apr 15 '18 at 22:58
  • @MadProgrammer yeah I didn't know how to better express that – Austin Apr 15 '18 at 22:59
  • Thank you so much Sir Much Appreciated!!! The only thing is it displays every text....but on the same row....and on top of each other....i want to display it on new row.... maybe because of gridPane(temp,2,1); – Utsav Dave Apr 15 '18 at 23:08
  • Sorry it took so long to respond had to leave for a while, but I have updated my answer to increment the rows hope this helps – Austin Apr 16 '18 at 00:42