How to set value to JTextField which is uneditable and and should not be considered in validation? - jtextfield

Hi i want to set the JTextField like this ( ) - . example (999) 999-9999 like this format and when i validate this format should not be considered and when i press backspace or delete it should not be deleted. Please help me regarding this.

To make it uneditable you should do:
(object of JTextField).setEditable(false);
and I am not clear about other two Questions, can you explain it please.

I Hope this will help you:
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Format extends JFrame {
public JTextArea ja;
public static void main(String args[]) {
Format ft = new Format();
}
Format() {
ja = new JTextArea("Enter Phone Number:");
ja.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == ke.VK_ENTER) {
String st = ja.getText();
ja.setText("");
char[] ch = st.toCharArray();
if(ch.length==10){
for (int i = 0; i < ch.length; i++) {
if (i == 0) {
ja.append("(" + ch[i]);
} else if (i == 3) {
ja.append(")" + " " + ch[i]);
} else if (i == 6) {
ja.append("-" + ch[i]);
} else {
ja.append("" + ch[i]);
}
}
ja.setEditable(false);
}
}
}
});
add(ja, BorderLayout.NORTH);
setSize(new Dimension(500, 500));
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}

Related

How to change textfield inputs to only numbers in javafx? [duplicate]

This question already has answers here:
What is the recommended way to make a numeric TextField in JavaFX?
(24 answers)
Restricting a TextField input to hexadecimal values in Java FX
(3 answers)
Closed 3 years ago.
I have a credit card page in my java fx program. I am trying to make it so that the inputs only allow numbers. At the moment it only gives an error if the fields are empty. But no error occurs if text is included?
I have tried changing it from String to integer, but that doesn't work.
public void thankyoupage(ActionEvent actionEvent) throws IOException {
String cardno = cardtf.getText();
String expdate1 = expirytf1.getText();
String expdate2 = expirytf2.getText();
String cvvnum = cvvtf.getText();
if (cardno.equals("") || expdate1.equals("") ||
expdate2.equals("") || cvvnum.equals("")) {
Alert alert = new Alert(Alert.AlertType.WARNING, "Enter Full Details", ButtonType.OK);
alert.showAndWait();
} else{
Window mainWindow = confirmbut.getScene().getWindow();
Parent newRoot = FXMLLoader.load(getClass().getResource("Thankyou.fxml"));
mainWindow.getScene().setRoot(newRoot);
}
}
Any links or changes would be nice.
You should attach a TextFormatter to your TextField. I have attached a sample on using Decimals - since you are using money, this might make the most sense.
On your text field you simply add the TextFormatter - this will prevent entry of anything other than what you allow.
//For Example
moneyTextField.setTextFormatter(new DecimalTextFormatter(0, 2));
--Below is the control code.
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.function.UnaryOperator;
import javafx.scene.control.TextFormatter;
import javafx.util.StringConverter;
public class DecimalTextFormatter extends TextFormatter<Number> {
private static DecimalFormat format = new DecimalFormat("#.0;-#.0");
public DecimalTextFormatter(int minDecimals, int maxDecimals) {
super(getStringConverter(minDecimals, maxDecimals), 0, getUnaryOperator(maxDecimals, true,-1));
}
public DecimalTextFormatter(int minDecimals, int maxDecimals, boolean allowsNegative) {
super(getStringConverter(minDecimals, maxDecimals), 0, getUnaryOperator(maxDecimals, allowsNegative,-1));
}
public DecimalTextFormatter(int minDecimals, int maxDecimals, boolean allowsNegative , int maxNoOfDigitsBeforeDecimal) {
super(getStringConverter(minDecimals, maxDecimals), 0, getUnaryOperator(maxDecimals, allowsNegative, maxNoOfDigitsBeforeDecimal));
}
private static StringConverter<Number> getStringConverter(int minDecimals, int maxDecimals) {
return new StringConverter<Number>() {
#Override
public String toString(Number object) {
if (object == null) {
return "";
}
String format = "0.";
for (int i = 0; i < maxDecimals; i++) {
if (i < minDecimals) {
format = format + "0";
} else {
format = format + "#";
}
}
format = format + ";-" + format;
DecimalFormat df = new DecimalFormat(format);
String formatted = df.format(object);
return formatted;
}
#Override
public Number fromString(String string) {
try {
if (string == null) {
return null;
}
return format.parse(string);
} catch (ParseException e) {
return null;
}
}
};
}
private static UnaryOperator<javafx.scene.control.TextFormatter.Change> getUnaryOperator(int maxDecimals,
boolean allowsNegative, int noOfDigitsBeforeDecimal) {
return new UnaryOperator<TextFormatter.Change>() {
#Override
public TextFormatter.Change apply(TextFormatter.Change change) {
if (!allowsNegative && change.getControlNewText().startsWith("-")) {
return null;
}
if (change.getControlNewText().isEmpty()) {
return change;
}
ParsePosition parsePosition = new ParsePosition(0);
Object object = format.parse(change.getControlNewText(), parsePosition);
if (change.getCaretPosition() == 1) {
if (change.getControlNewText().equals(".")) {
return change;
}
}
if (object == null || parsePosition.getIndex() < change.getControlNewText().length()) {
return null;
} else {
if(noOfDigitsBeforeDecimal != -1)
{
int signum = new BigDecimal(change.getControlNewText()).signum();
int precision = new BigDecimal(change.getControlNewText()).precision();
int scale = new BigDecimal(change.getControlNewText()).scale();
int val = signum == 0 ? 1 : precision - scale;
if (val > noOfDigitsBeforeDecimal) {
return null;
}
}
int decPos = change.getControlNewText().indexOf(".");
if (decPos > 0) {
int numberOfDecimals = change.getControlNewText().substring(decPos + 1).length();
if (numberOfDecimals > maxDecimals) {
return null;
}
}
return change;
}
}
};
}
}
You have to use regular expressions to validate fields. You can learn more about regular expression here https://regexr.com/
String cardno = cardtf.getText();
if (cardno.equals("") || expdate1.equals("") || expdate2.equals("") || cvvnum.equals("")) {
Alert alert = new Alert(Alert.AlertType.WARNING, "Enter Full Details", ButtonType.OK);
alert.showAndWait();
}else if (cardno.matches("/^[A-Za-z ]+$/")){
Alert alert = new Alert(Alert.AlertType.WARNING, "It Can not contain letters", ButtonType.OK);
alert.showAndWait();
}else{
//Else Part
}
Here is a piece of code that should help you doing the trick by checking at every input if the text contains only numbers an a maximum of one "," as the decimal separator.
There is already a post showing how to do this.
Post
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;
public class NumberField extends TextField {
public NumberField () {
initSpellListener();
}
public final void initSpellListener() {
this.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
if (!newValue.matches("\\d*")) {
this.setText(newValue.replaceAll("[^\\d,]", ""));/*The comma here "[^\\d,]" can be changed with the dot*/
StringBuilder aus = new StringBuilder();
aus.append(this.getText());
boolean firstPointFound = false;
for (int i = 0; i < aus.length(); i++) {
if (aus.charAt(i) == ',') {/*Change the , with . if you want the . to be the decimal separator*/
if (!firstPointFound) {
firstPointFound = true;
} else {
aus.deleteCharAt(i);
}
}
}
newValue = aus.toString();
this.setText(newValue);
} else {
this.setText(newValue);
}
});
}}
[As soon as I find the post I will credit this code.]
if (!newValue.matches("\\d*"))
this part of the code checks with a regex expression if the new string value doesn't contain only numbers, and then with this code
this.setText(newValue.replaceAll("[^\\d,]", ""));
it replaces all the non-digit or comma chars.
Finally the for-loop checks if only exists one comma ad if other are found they are deleted.
To help you with regex writing here is a very useful site : Online regex
Then you can use this object as a normal TextField:
#FMXL
private NumberField nf;

How to prevent closing of AutoCompleteCombobox popupmenu on whitespace key press in JavaFX?

I have created a AutoCompleteCombobox in JavaFX, but issue is that combobox popup closes when user presses SPACE key. I want to continue filtering with space character and prevent popup from closing.
I just want to know how can I override the event handler which handles SPACE key press.
Please help...I've been banging my head against the wall all day on this...
package org.fxapps;
import java.text.Normalizer;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Window;
/**
*
* Uses a combobox tooltip as the suggestion for auto complete and updates the
* combo box itens accordingly <br />
* It does not work with space, space and escape cause the combobox to hide and
* clean the filter ... Send me a PR if you want it to work with all characters
* -> It should be a custom controller - I KNOW!
*
* #author wsiqueir
*
* #param <T>
*/
public class ComboBoxAutoComplete<T> {
private ComboBox<T> cmb;
String filter = "";
private ObservableList<T> originalItems;
public ComboBoxAutoComplete(ComboBox<T> cmb) {
this.cmb = cmb;
originalItems = FXCollections.observableArrayList(cmb.getItems());
cmb.setTooltip(new Tooltip());
cmb.setOnKeyPressed(this::handleOnKeyPressed);
cmb.setOnHidden(this::handleOnHiding);
}
public void handleOnKeyPressed(KeyEvent e) {
ObservableList<T> filteredList = FXCollections.observableArrayList();
KeyCode code = e.getCode();
if (code.isLetterKey()) {
filter += e.getText();
}
if (code == KeyCode.BACK_SPACE && filter.length() > 0) {
filter = filter.substring(0, filter.length() - 1);
cmb.getItems().setAll(originalItems);
}
if (code == KeyCode.ESCAPE) {
filter = "";
}
if (filter.length() == 0) {
filteredList = originalItems;
cmb.getTooltip().hide();
} else {
Stream<T> itens = cmb.getItems().stream();
String txtUsr = unaccent(filter.toString().toLowerCase());
itens.filter(el -> unaccent(el.toString().toLowerCase()).contains(txtUsr)).forEach(filteredList::add);
cmb.getTooltip().setText(txtUsr);
Window stage = cmb.getScene().getWindow();
double posX = stage.getX() + cmb.getBoundsInParent().getMinX();
double posY = stage.getY() + cmb.getBoundsInParent().getMinY();
cmb.getTooltip().show(stage, posX, posY);
cmb.show();
}
cmb.getItems().setAll(filteredList);
}
public void handleOnHiding(Event e) {
filter = "";
cmb.getTooltip().hide();
T s = cmb.getSelectionModel().getSelectedItem();
cmb.getItems().setAll(originalItems);
cmb.getSelectionModel().select(s);
}
private String unaccent(String s) {
String temp = Normalizer.normalize(s, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
return pattern.matcher(temp).replaceAll("");
}
}
And this is the test:
package org.fxapps;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ComboBoxAutoCompleteTest extends Application {
private static final String[] LISTA = { "Abacate", "Abacaxi", "Ameixa", "Amora", "Araticum", "Atemoia", "Avocado",
"Banana prata", "Caju", "Cana descascada", "Caqui", "Caqui Fuyu", "Carambola", "Cereja", "Coco verde",
"Figo", "Figo da Índia", "Framboesa", "Goiaba", "Graviola", "Jabuticaba", "Jambo", "Jambo rosa", "Jambolão",
"Kino (Kiwano)", "Kiwi", "Laranja Bahia", "Laranja para suco", "Laranja seleta", "Laranja serra d’água",
"Laranjinha kinkan", "Lichia", "Lima da pérsia", "Limão galego", "Limão Taiti", "Maçã argentina",
"Maçã Fuji", "Maçã gala", "Maçã verde", "Mamão formosa", "Mamão Havaí", "Manga espada", "Manga Haden",
"Manga Palmer", "Manga Tommy", "Manga Ubá", "Mangostim", "Maracujá doce", "Maracujá para suco", "Melancia",
"Melancia sem semente", "Melão", "Melão Net", "Melão Orange", "Melão pele de sapo", "Melão redinha",
"Mexerica carioca", "Mexerica Murcote", "Mexerica Ponkan", "Mirtilo", "Morango", "Nectarina",
"Nêspera ou ameixa amarela", "Noni", "Pera asiática", "Pera portuguesa", "Pêssego", "Physalis", "Pinha",
"Pitaia", "Romã", "Tamarilo", "Tamarindo", "Uva red globe", "Uva rosada", "Uva Rubi", "Uva sem semente",
"Abobora moranga", "Abobrinha italiana", "Abobrinha menina", "Alho", "Alho descascado",
"Batata baroa ou cenoura amarela", "Batata bolinha", "Batata doce", "Batata inglesa", "Batata yacon",
"Berinjela", "Beterraba", "Cebola bolinha", "Cebola comum", "Cebola roxa", "Cenoura", "Cenoura baby",
"Couve flor", "Ervilha", "Fava", "Gengibre", "Inhame", "Jiló", "Massa de alho", "Maxixe", "Milho",
"Pimenta biquinho fresca", "Pimenta de bode fresca", "Pimentão amarelo", "Pimentão verde",
"Pimentão vermelho", "Quiabo", "Repolho", "Repolho roxo", "Tomate cereja", "Tomate salada",
"Tomate sem acidez", "Tomate uva", "Vagem", "Agrião", "Alcachofra", "Alface", "Alface americana",
"Almeirão", "Brócolis", "Broto de alfafa", "Broto de bambu", "Broto de feijão", "Cebolinha", "Coentro",
"Couve", "Espinafre", "Hortelã", "Mostarda", "Rúcula", "Salsa", "Ovos brancos", "Ovos de codorna",
"Ovos vermelhos" };
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) throws Exception {
ComboBox<String> cmb = new ComboBox<>();
cmb.setTooltip(new Tooltip());
cmb.getItems().addAll(LISTA);
stage.setScene(new Scene(new StackPane(cmb)));
stage.show();
stage.setTitle("Filtrando um ComboBox");
stage.setWidth(300);
stage.setHeight(300);
new ComboBoxAutoComplete<String>(cmb);
}
}
I used the help of code mentioned on https://github.com/jesuino/javafx-combox-autocomplete/blob/master/src/main/java/org/fxapps/ComboBoxAutoComplete.java
I have handled all three events (key press, key release, key typed) on combobox but no solutions. I think it is being caused by key press event on combobox item list view.
Bug is mentioned on https://bugs.openjdk.java.net/browse/JDK-8087549
This is the same question as posed here (but I am unable to ask a question or make a comment on that page):
How to prevent closing of AutoCompleteCombobox popupmenu on SPACE key press in JavaFX
There are a lot of good posts here, too (but I am not smart enough to figure out how to get any of them to work and fix this space bug!)
AutoComplete ComboBox in JavaFX
I tried the answer/code on How to prevent closing of AutoCompleteCombobox popupmenu on SPACE key press in JavaFX, but it either doesn't change the behavior and/or I am too stupid to make it work. This is the code I used (and the ComboBox still closes when I hit space)
public class ComboBoxAutoComplete<T> {
private ComboBox<T> cmb;
String filter = "";
private ObservableList<T> originalItems;
public ComboBoxAutoComplete(ComboBox<T> cmb) {
this.cmb = cmb;
ComboBoxListViewSkin<T> comboBoxListViewSkin = new ComboBoxListViewSkin<T>(cmb);
comboBoxListViewSkin.getPopupContent().addEventFilter(KeyEvent.ANY, (event) -> {
if( event.getCode() == KeyCode.SPACE ) {
event.consume();
}
});
cmb.setSkin(comboBoxListViewSkin);
originalItems = FXCollections.observableArrayList(cmb.getItems());
cmb.setTooltip(new Tooltip());
cmb.setOnKeyPressed(this::handleOnKeyPressed);
cmb.setOnHidden(this::handleOnHiding);
}
public void handleOnKeyPressed(KeyEvent e) {
ObservableList<T> filteredList = FXCollections.observableArrayList();
KeyCode code = e.getCode();
ComboBoxListViewSkin<T> comboBoxListViewSkin = new ComboBoxListViewSkin<T>(cmb);
comboBoxListViewSkin.getPopupContent().addEventFilter(KeyEvent.ANY, (event) -> {
if( event.getCode() == KeyCode.SPACE ) {
event.consume();
}
});
cmb.setSkin(comboBoxListViewSkin);
if (code.isLetterKey()||code.isDigitKey()) {
filter += e.getText();
}
if (code == KeyCode.BACK_SPACE && filter.length() > 0) {
filter = filter.substring(0, filter.length() - 1);
cmb.getItems().setAll(originalItems);
}
if (code == KeyCode.ESCAPE) {
filter = "";
}
if (filter.length() == 0) {
filteredList = originalItems;
cmb.getTooltip().hide();
} else {
Stream<T> itens = cmb.getItems().stream();
String txtUsr = unaccent(filter.toString().toLowerCase());
itens.filter(el -> unaccent(el.toString().toLowerCase()).contains(txtUsr)).forEach(filteredList::add);
cmb.getTooltip().setText(txtUsr);
Window stage = cmb.getScene().getWindow();
double posX = stage.getX() + cmb.getBoundsInParent().getMinX();
double posY = stage.getY() + cmb.getBoundsInParent().getMinY();
cmb.getTooltip().show(stage, posX, posY);
cmb.show();
}
cmb.getItems().setAll(filteredList);
}
public void handleOnHiding(Event e) {
ComboBoxListViewSkin<T> comboBoxListViewSkin = new ComboBoxListViewSkin<T>(cmb);
comboBoxListViewSkin.getPopupContent().addEventFilter(KeyEvent.ANY, (event) -> {
if( event.getCode() == KeyCode.SPACE ) {
event.consume();
}
});
cmb.setSkin(comboBoxListViewSkin);
filter = "";
cmb.getTooltip().hide();
T s = cmb.getSelectionModel().getSelectedItem();
cmb.getItems().setAll(originalItems);
cmb.getSelectionModel().select(s);
}
private String unaccent(String s) {
ComboBoxListViewSkin<T> comboBoxListViewSkin = new ComboBoxListViewSkin<T>(cmb);
comboBoxListViewSkin.getPopupContent().addEventFilter(KeyEvent.ANY, (event) -> {
if( event.getCode() == KeyCode.SPACE ) {
event.consume();
}
});
cmb.setSkin(comboBoxListViewSkin);
String temp = Normalizer.normalize(s, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
return pattern.matcher(temp).replaceAll("");
}
}

Formatting a string in a textarea javafx using escape sequences

Super simple question, but I can't figure it out for the life of me. I'm connecting to a database and selecting a table and outputting the tables contents into a text area. It works, but the output gets all bunched up if one of the table's contents is longer/shorter.
How can I make my current output more formatted preferably using escape sequences?
Here's my current code:
ta.appendText(rsMetaData.getColumnName(i)+ " \t"); //outputs the table column names
while (rSet.next()) { // this outputs the tables contents
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
ta.appendText(rSet.getObject(i) + " \t");
}
ta.appendText("\n");
}
The current output looks like this
deptName chairID collegeID deptID
Biology 111221118 SC BIOL
Chemistry 111221119 SC CHEM
Computer Science 111221115 SC CS
Mathematics 111221116 SC MATH
It might not look bad on here, but in table form it looks kinda crap. I imagine it's because of the tabs. I tried to use a few escape sequences, but it doesn't work with text areas or something.
I suggest you use a TableView. If you don't want to use a TableView, then it is a matter of simple math and making sure you use a Monospace font. In this example app I find the longest word for each column. Then I determine how many spaces needed to be added to words that are shorter than the longest word. Then I add four more spaces to create a complete column.
Used to set the TextArea font to a monospaced font
textArea.setStyle("-fx-font-family: monospace");
Used to find the longest String in each column.
List<Integer> longestDataLengths = new ArrayList();//This variable is global
void findLongestDataLengthsForColumns(List<List<String>> fakeData)
{
for (int i = 0; i < fakeData.size(); i++) {
for (int ii = 0; ii < fakeData.get(i).size(); ii++) {
if (i == 0) {
longestDataLengths.add(fakeData.get(i).get(ii).length());
//System.out.println("added: " + fakeData.get(i).get(ii));
}
else {
//System.out.println("adding: " + i);;//+ fakeData.get(i).get(ii));
if (fakeData.get(i).get(ii).length() > longestDataLengths.get(ii)) {
longestDataLengths.set(ii, fakeData.get(i).get(ii).length());
}
}
}
}
}
Used to find the number of spaces needed to complete a column's length
int numberOfSpacesNeeded(int longestLength, String entry)
{
int numberOfSpaceAfterLongestLength = 4;
System.out.println("space needed: " + (longestLength - entry.length() + numberOfSpaceAfterLongestLength));
return longestLength - entry.length() + numberOfSpaceAfterLongestLength;
}
Used to create the extra spaces/spaces needed
String createSpace(int numberOfSpaces)
{
StringBuilder spaces = new StringBuilder();
for (int i = 0; i < numberOfSpaces; i++) {
spaces.append(" ");
}
return spaces.toString();
}
Complete Example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication261 extends Application
{
List<Integer> longestDataLengths = new ArrayList();
#Override
public void start(Stage primaryStage)
{
// for (List<String> line : getFakeDBData()) {
// System.out.println(line);
// }
List<List<String>> fakeData = getFakeDBData();
findLongestDataLengthsForColumns(fakeData);
// for (Integer entry : longestDataLengths) {
// System.out.println(entry);
// }
TextArea textArea = new TextArea();
textArea.setStyle("-fx-font-family: monospace");
for (List<String> line : fakeData) {
for (int i = 0; i < line.size(); i++) {
textArea.appendText(line.get(i) + createSpace(numberOfSpacesNeeded(longestDataLengths.get(i), line.get(i))));
}
textArea.appendText("\n");
}
StackPane root = new StackPane(textArea);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
List<List<String>> getFakeDBData()
{
List<List<String>> fakeData = new ArrayList();
String data = "deptName chairID collegeID deptID\n"
+ "Biology 111221118 SC BIOL\n"
+ "Chemistry 111221119 SC CHEM\n"
+ "Computer_Science 111221115 SC CS\n"
+ "Mathematics 111221116 SC MATH";
for (String line : Arrays.asList(data.split("\n"))) {
fakeData.add(Arrays.asList(line.split(" ")));
}
return fakeData;
}
//
void findLongestDataLengthsForColumns(List<List<String>> fakeData)
{
for (int i = 0; i < fakeData.size(); i++) {
for (int ii = 0; ii < fakeData.get(i).size(); ii++) {
if (i == 0) {
longestDataLengths.add(fakeData.get(i).get(ii).length());
//System.out.println("added: " + fakeData.get(i).get(ii));
}
else {
//System.out.println("adding: " + i);;//+ fakeData.get(i).get(ii));
if (fakeData.get(i).get(ii).length() > longestDataLengths.get(ii)) {
longestDataLengths.set(ii, fakeData.get(i).get(ii).length());
}
}
}
}
}
String createSpace(int numberOfSpaces)
{
StringBuilder spaces = new StringBuilder();
for (int i = 0; i < numberOfSpaces; i++) {
spaces.append(" ");
}
return spaces.toString();
}
int numberOfSpacesNeeded(int longestLength, String entry)
{
int numberOfSpaceAfterLongestLength = 4;
System.out.println("space needed: " + (longestLength - entry.length() + numberOfSpaceAfterLongestLength));
return longestLength - entry.length() + numberOfSpaceAfterLongestLength;
}
}
Results

want to use updateMessage("...") in a Class that is not an Task

I want to use updateMessage("...") in a Class that is not an Task.
How can I solve this Problem?
Is it possible to give a Task Handle which can perform a updateMessage("...") to a Function?
import javafx.concurrent.Task;
public class TestTask extends Task<Void> {
private final static int COUNT = 1000;
#Override
protected Void call() {
doTaskStuff("Found ");
return null;
}
public void doTaskStuff(String s) {
for (int i = 0; i < COUNT; i++) {
System.out.println(i);
this.updateProgress(i, COUNT);
this.updateMessage(s + (i + 1) + "!");
Dummy.dummy(this, i);
try {
Thread.sleep(1);
} catch (InterruptedException interruptedException) {
}
}
}
}
class Dummy {
public static void dummy(TestTask testTask, int i) {
String s = "";
if (i == 0) {
s = "i == 0";
} else {
s = "i != 0";
}
testTask.updateMessage(s);// does not work
}
}
The issue here is the method being protected. Of course you could override the method and increase the visibility, but this would imho be a bad approach:
#Override
public void updateMessage(String message) {
super.updateMessage(message);
}
Note that the method sets a single message, therefore you could just return the value:
updateMessage(Dummy.dummy(i));
public static String dummy(int i) {
return (i == 0) ? "i == 0" : "i != 0";
}
For multiple updates you could also provide access to this kind of functionality using an interface:
Dummy.dummy(this::updateMessage, i);
public static void dummy(Consumer<String> updater, int i) {
updater.accept((i == 0) ? "i == 0" : "i != 0");
}

JavaFX TimeTextField

Base Article:
[https://community.oracle.com/thread/2552039?start=0&tstart=0][1]
I extend the solution from the oracle community with the event filters in the constructor for a better user ergonomics.
The JavaFX TimeTextField provides all functions needed for time handling.
Unfortunately it's missing in the JavaFX Standard in Java8, where a time handling field is a must in my point of view as in other technologies it is possible.
The thread in the oracle community is not as frequent as I expect it, please test it and try to improve it! We need it...
Features:
- only numeric keys are accepted.
- validation for a given time format
- (my improvement) when the last number of a block like hours or minutes was reached, the cursor jumps to the next block.
package test;
import java.util.regex.Pattern;
import javafx.beans.binding.Bindings;
import javafx.application.Application;
import javafx.beans.binding.IntegerBinding;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.IndexRange;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TimeTextFieldTest extends Application {
#Override
public void start(Stage primaryStage) {
VBox root = new VBox(5);
root.setPadding(new javafx.geometry.Insets(5));
Label hrLabel = new Label();
Label minLabel = new Label();
Label secLabel = new Label();
TimeTextField timeTextField = new TimeTextField();
hrLabel.textProperty().bind(Bindings.format("Hours: %d", timeTextField.hoursProperty()));
minLabel.textProperty().bind(Bindings.format("Minutes: %d", timeTextField.minutesProperty()));
secLabel.textProperty().bind(Bindings.format("Seconds: %d", timeTextField.secondsProperty()));
root.getChildren().addAll(timeTextField, hrLabel, minLabel, secLabel);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class TimeTextField extends TextField {
enum Unit {
HOURS, MINUTES, SECONDS
};
private final Pattern timePattern;
private final ReadOnlyIntegerWrapper hours;
private final ReadOnlyIntegerWrapper minutes;
private final ReadOnlyIntegerWrapper seconds;
public TimeTextField() {
this("00:00:00");
this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent inputevent) {
int c = TimeTextField.this.getCaretPosition();
if (c <= 7) {
if (!"1234567890:".contains(inputevent.getCharacter().toLowerCase())) {
inputevent.consume();
}
} else {
inputevent.consume();
}
}
});
this.addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent inputevent) {
boolean withMinutes = false;
if (TimeTextField.this.getText() != null && TimeTextField.this.getText().length() >= 5
&& TimeTextField.this.getText().indexOf(":") == 2) {
withMinutes = true;
}
boolean withSeconds = false;
if (TimeTextField.this.getText() != null && TimeTextField.this.getText().length() == 8
&& TimeTextField.this.getText().lastIndexOf(":") == 5) {
withSeconds = true;
}
int c = TimeTextField.this.getCaretPosition();
if (((c == 2 && withMinutes) || (c == 5 && withSeconds))
&& (inputevent.getCode() != KeyCode.LEFT && inputevent.getCode() != KeyCode.BACK_SPACE)) {
TimeTextField.this.forward();
inputevent.consume();
}
}
});
}
public TimeTextField(String time) {
super(time);
// timePattern = Pattern.compile("\\d\\d:\\d\\d:\\d\\d");
timePattern = Pattern.compile("([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]");
if (!validate(time)) {
throw new IllegalArgumentException("Invalid time: " + time);
}
hours = new ReadOnlyIntegerWrapper(this, "hours");
minutes = new ReadOnlyIntegerWrapper(this, "minutes");
seconds = new ReadOnlyIntegerWrapper(this, "seconds");
hours.bind(new TimeTextField.TimeUnitBinding(Unit.HOURS));
minutes.bind(new TimeTextField.TimeUnitBinding(Unit.MINUTES));
seconds.bind(new TimeTextField.TimeUnitBinding(Unit.SECONDS));
}
public ReadOnlyIntegerProperty hoursProperty() {
return hours.getReadOnlyProperty();
}
public int getHours() {
return hours.get();
}
public ReadOnlyIntegerProperty minutesProperty() {
return minutes.getReadOnlyProperty();
}
public int getMinutes() {
return minutes.get();
}
public ReadOnlyIntegerProperty secondsProperty() {
return seconds.getReadOnlyProperty();
}
public int getSeconds() {
return seconds.get();
}
#Override
public void appendText(String text) {
// Ignore this. Our text is always 8 characters long, we cannot
// append anything
}
#Override
public boolean deleteNextChar() {
boolean success = false;
// If there's a selection, delete it:
final IndexRange selection = getSelection();
if (selection.getLength() > 0) {
int selectionEnd = selection.getEnd();
this.deleteText(selection);
this.positionCaret(selectionEnd);
success = true;
} else {
// If the caret preceeds a digit, replace that digit with a zero
// and move the caret forward. Else just move the caret forward.
int caret = this.getCaretPosition();
if (caret % 3 != 2) { // not preceeding a colon
String currentText = this.getText();
setText(currentText.substring(0, caret) + "0" + currentText.substring(caret + 1));
success = true;
}
this.positionCaret(Math.min(caret + 1, this.getText().length()));
}
return success;
}
#Override
public boolean deletePreviousChar() {
boolean success = false;
// If there's a selection, delete it:
final IndexRange selection = getSelection();
if (selection.getLength() > 0) {
int selectionStart = selection.getStart();
this.deleteText(selection);
this.positionCaret(selectionStart);
success = true;
} else {
// If the caret is after a digit, replace that digit with a zero
// and move the caret backward. Else just move the caret back.
int caret = this.getCaretPosition();
if (caret % 3 != 0) { // not following a colon
String currentText = this.getText();
setText(currentText.substring(0, caret - 1) + "0" + currentText.substring(caret));
success = true;
}
this.positionCaret(Math.max(caret - 1, 0));
}
return success;
}
#Override
public void deleteText(IndexRange range) {
this.deleteText(range.getStart(), range.getEnd());
}
#Override
public void deleteText(int begin, int end) {
// Replace all digits in the given range with zero:
StringBuilder builder = new StringBuilder(this.getText());
for (int c = begin; c < end; c++) {
if (c % 3 != 2) { // Not at a colon:
builder.replace(c, c + 1, "0");
}
}
this.setText(builder.toString());
}
#Override
public void insertText(int index, String text) {
// Handle an insert by replacing the range from index to
// index+text.length() with text, if that results in a valid string:
StringBuilder builder = new StringBuilder(this.getText());
builder.replace(index, index + text.length(), text);
final String testText = builder.toString();
if (validate(testText)) {
this.setText(testText);
}
this.positionCaret(index + text.length());
}
#Override
public void replaceSelection(String replacement) {
final IndexRange selection = this.getSelection();
if (selection.getLength() == 0) {
this.insertText(selection.getStart(), replacement);
} else {
this.replaceText(selection.getStart(), selection.getEnd(), replacement);
}
}
#Override
public void replaceText(IndexRange range, String text) {
this.replaceText(range.getStart(), range.getEnd(), text);
}
#Override
public void replaceText(int begin, int end, String text) {
if (begin == end) {
this.insertText(begin, text);
} else {
// only handle this if text.length() is equal to the number of
// characters being replaced, and if the replacement results in
// a valid string:
if (text.length() == end - begin) {
StringBuilder builder = new StringBuilder(this.getText());
builder.replace(begin, end, text);
String testText = builder.toString();
if (validate(testText)) {
this.setText(testText);
}
this.positionCaret(end);
}
}
}
private boolean validate(String time) {
if (!timePattern.matcher(time).matches()) {
return false;
}
String[] tokens = time.split(":");
assert tokens.length == 3;
try {
int hours = Integer.parseInt(tokens[0]);
int mins = Integer.parseInt(tokens[1]);
int secs = Integer.parseInt(tokens[2]);
if (hours < 0 || hours > 23) {
return false;
}
if (mins < 0 || mins > 59) {
return false;
}
if (secs < 0 || secs > 59) {
return false;
}
return true;
} catch (NumberFormatException nfe) {
// regex matching should assure we never reach this catch block
assert false;
return false;
}
}
private final class TimeUnitBinding extends IntegerBinding {
final Unit unit;
TimeUnitBinding(Unit unit) {
this.bind(textProperty());
this.unit = unit;
}
#Override
protected int computeValue() {
// Crazy enum magic
String token = getText().split(":")[unit.ordinal()];
return Integer.parseInt(token);
}
}
}
}

Resources