Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For Java FXML how would I be able to limit the number of letters in my textfield. Here is my code below. I have googled

For Java FXML how would I be able to limit the number of letters in my textfield. Here is my code below. I have googled this question before, but they all seem to direct to Jtextfields which is not applicable.

package project4;

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.net.URL; import java.util.ArrayList; import java.util.ResourceBundle; import java.util.Scanner; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.RadioButton; import javafx.scene.control.Slider; import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.text.Text; import javafx.stage.FileChooser; import javafx.stage.Stage; import static jdk.nashorn.tools.ShellFunctions.input;

/** * * @author Parker */ public class FXMLDocumentController implements Initializable { @FXML private Label lblGender;

@FXML private RadioButton rbMale;

@FXML private RadioButton rbFemale;

@FXML private RadioButton rbOther;

@FXML private Label lblClass;

@FXML private TextField txtfieldName;

@FXML private Label lblName;

@FXML private Slider sldJvC;

@FXML private Label lblValues;

@FXML private Slider sldSvW;

@FXML private Slider sldHuVHo;

@FXML private MenuBar mnuBar;

@FXML private Menu mnuFile;

@FXML private MenuItem btnOpen;

@FXML private MenuItem btnSave;

@FXML private Menu mnuOpen;

@FXML private MenuItem btnReset;

@FXML private MenuItem btnExit; private FileChooser fileChooser; private ObservableList charClasses = FXCollections.observableArrayList(); @FXML private ComboBox cmbClass; @FXML private ToggleGroup tgGender; @Override public void initialize(URL url, ResourceBundle rb) { fileChooser = new FileChooser(); txtfieldName.setPrefColumnCount(10); //fill the classes charClasses.add("Warrior"); charClasses.add("Barbarian"); charClasses.add("Mage"); charClasses.add("Monk"); charClasses.add("Thief"); cmbClass.setItems(charClasses); }//end initialize @FXML private void handleButtonAction(ActionEvent event) { Stage stage = (Stage) txtfieldName.getScene().getWindow(); if(event.getSource() == btnOpen) { File file = fileChooser.showOpenDialog(stage); Scanner inFile = null; String input; try { inFile = new Scanner(file); //first line: gender input = inFile.nextLine(); if(input.equals("Male")){ rbMale.setSelected(true); } if(input.equals("Female")){ rbFemale.setSelected(true); } if(input.equals("Other")){ rbOther.setSelected(true); } //second line: Class input = inFile.nextLine(); for(int i = 0; i < charClasses.size(); i++){ if(charClasses.get(i).equals(input)){ cmbClass.getSelectionModel().select(0); break; }//end if }//end for //third line: name input = inFile.nextLine(); txtfieldName.setText(input); //fourth line: Justice vs Compassion input = inFile.nextLine(); sldJvC.setValue(Integer.parseInt(input)); //fifth line: Strength vs Wisdom input = inFile.nextLine(); sldSvW.setValue(Integer.parseInt(input)); //sixth line: Humility vs Honour input = inFile.nextLine(); sldHuVHo.setValue(Integer.parseInt(input)); inFile.close(); } catch(FileNotFoundException ex){ System.out.println("Woops, can't read."); }//end try catch }//end if else if(event.getSource() == btnSave) { File file = fileChooser.showSaveDialog(stage); try { PrintWriter pw = new PrintWriter(file); //first line: Gender if(rbMale.isSelected()){ pw.println("Male"); } else if(rbFemale.isSelected()){ pw.println("Female"); } else if(rbOther.isSelected()){ pw.println("Other"); }//end if else //second line: Character Class pw.println(cmbClass.getValue()); //third line: Name pw.println(txtfieldName.getText()); //fourth line: Justice vs Compassion pw.println((int)sldJvC.getValue()); //fifth line: Strength vs Wisdom pw.println((int)sldSvW.getValue()); //sixth line: Humilit vs Honour pw.println((int)sldHuVHo.getValue()); pw.close(); } catch(FileNotFoundException ex) { System.out.println("Woops, can't write."); } }//end if if(event.getSource() == btnReset) { //first line: gender rbMale.setSelected(true); //second line: Class cmbClass.getSelectionModel().select(-1); //third line: name txtfieldName.setText("Type Here"); //fourth line: Justice vs Compassion sldJvC.setValue(0); //fifth line: Strength vs Wisdom sldSvW.setValue(0); //sixth line: Humilit vs Honour sldHuVHo.setValue(0); }//end if if(event.getSource() == btnExit) { System.exit(0); }//end if }//end handleButtonAction }//end class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Semantics In Databases Second International Workshop Dagstuhl Castle Germany January 2001 Revised Papers Lncs 2582

Authors: Leopoldo Bertossi ,Gyula O.H. Katona ,Klaus-Dieter Schewe ,Bernhard Thalheim

2003rd Edition

3540009574, 978-3540009573

More Books

Students also viewed these Databases questions

Question

Explain the market segmentation.

Answered: 1 week ago

Question

Mention the bases on which consumer market can be segmented.

Answered: 1 week ago