Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a GUI-based program that simulates the selection of ball team. You will re

ID: 3812427 • Letter: W

Question

Write a GUI-based program that simulates the selection of ball team. You will retrieve the data from a text file containing 10 lines. On each line will be the name of a player. Your program needs to read the file and display 10 checkboxes representing the 10 players. A text area will display the team, made up of the C being selected. A basketball team has five players. Your program should not allow the user to change his or her selection after the team has five players. Every time the user checks or unchecks a checkbox the team in the text area should be updated accordingly. Provide buttons for the following: One button, when clicked, displays how many players are currently on the team. Another button, when clicked, displays how many players remain unselected. For this, you should design and code a separate (non-GUI) class encapsulating the player selection process, then instantiate an object of that class inside your GUI class and call the various methods as needed.

Explanation / Answer

package chegg.swinggame;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class SwingBasketBallTeamSelection extends Application {

   Button submit = null;
   BorderPane root = null;
   VBox vbox = null;
   TableView<Player> table = null;

   static int count = 0;

   public static void main(String[] args) {
       Application.launch(args);
   }

   @SuppressWarnings({ "unchecked", "rawtypes" })
   @Override
   public void start(Stage stage) {

       final List<Player> list = SwingBasketBallTeamSelection
               .readFile("player");

       final ObservableList<Player> data = FXCollections.observableArrayList();;
      
       EventHandler eh = new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
               int count = 0;
               if (event.getSource() instanceof CheckBox) {
                   CheckBox chk = (CheckBox) event.getSource();
                   System.out.println("Action performed on checkbox "
                           + chk.getText());
                   int ch = Integer.parseInt(chk.getId());
                   System.out.println(ch);
                   switch (ch) {
                   case 0:
                       System.out.println("hello");
                       if (chk.isSelected()) {
                           count++;
                           list.add(new Player(chk.getText()));
                       } else {
                           list.remove(new Player(chk.getText()));
                           count--;
                       }
                       break;

                   }
               }
           }
       };

       GridPane pane = new GridPane();
       pane.setAlignment(Pos.TOP_LEFT);
       pane.setHgap(5);
       pane.setVgap(5);

       CheckBox cb = null;
       for (int i = 0; i < list.size(); i++) {
           cb = new CheckBox(list.get(i).getName());
           cb.setStyle("-fx-font-size: 14;" + "-fx-border-insets: -5; ");
           cb.setId("" + i);
           cb.setOnAction(eh);
           cb.setSelected(false);
           pane.add(cb, 0, i);
       }

       TableView<Player> table = new TableView<Player>();

       TableColumn firstNameCol = new TableColumn("Team Player's Name");
       firstNameCol
               .setCellValueFactory(new PropertyValueFactory<Player, String>(
                       "name"));

       table.setEditable(true);
       table.setItems(data);
       table.getColumns().addAll(firstNameCol);

       vbox = new VBox();
       vbox.setSpacing(5);
       vbox.setPadding(new Insets(10, 0, 0, 10));
       vbox.getChildren().addAll(table);

       BorderPane.setAlignment(pane, Pos.CENTER_LEFT);
       // Set the alignment of the Top Text to Center
       BorderPane.setAlignment(vbox, Pos.BOTTOM_CENTER);

       // Create a BorderPane with a Text node in each of the five regions
       BorderPane root = new BorderPane(vbox, pane, null, null, null);
       // Set the Size of the VBox
       root.setPrefSize(600, 600);
       // Set the Style-properties of the BorderPane
       root.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;"
               + "-fx-border-width: 2;" + "-fx-border-insets: 5;"
               + "-fx-border-radius: 5;" + "-fx-border-color: blue;");

       // Create the Scene
       Scene scene = new Scene(root);
       // Add the scene to the Stage
       stage.setScene(scene);
       // Set the title of the Stage
       stage.setTitle("A simple BorderPane Example");
       // Display the Stage
       stage.show();
      
   }

   /**
   *
   * @param fileNameWithPath
   * @return
   */
   public static List<Player> readFile(String fileNameWithPath) {
       List<Player> list = new ArrayList<Player>();
       try (BufferedReader br = new BufferedReader(new FileReader("d:\"
               + fileNameWithPath + ".txt"))) {
           String sCurrentLine;
           while ((sCurrentLine = br.readLine()) != null) {
               list.add(new Player(sCurrentLine));
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
       return list;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote