So I have probably an easy question, but I couldn't find anyone asking this question on Google, so now I'm here.
The problem is simple - I must copy a line of text that has white spaces and tabulators in it, but once I copy it inside my text (input) field, it removes all the tabulators for some reason, so it leave the text all in one big mess that I cannot filter anything out of it.
Any ideas what could be done so those input fields would allow tabulators?
P.S. By pressing tab while I'm inside the input field, it moves between the buttons, instead of inputting a tabulator.
The content of a TextField is represented by a private static class TextFieldContent. TextFieldContent implements insert(int index, String text, boolean notifyListeners) method to filter the input text. The method uses a static method from TextInputControl class to remove "illegal" characters, here is the implementation:
#Override
public void insert(int index, String text, boolean notifyListeners) {
text = TextInputControl.filterInput(text, true, true);
if (!text.isEmpty()) {
characters.insert(index, text);
if (notifyListeners) {
ExpressionHelper.fireValueChangedEvent(helper);
}
}
}
The last parameter in TextInputControl.filterInput(text, true, true) defines whether tab characters are "illegal" or not. It's set to true and as I mentioned before, that class is a private static final class and you can't extend it and override insert method.
The solution is to extend TextInputControl and create a custom Content class that doesn't remove tab characters.
As an alternative, you can use TextArea, text areas accept tab characters.
Related
I am making my own text editor in JavaFX and I want to have bracket completion. Like in Netbuns. I have tried using a ChangeListener on the TextArea and checking if the last character is a bracket and append the char like this:
textArea.textProperty().addListener(new ChangeListener<String>()
{
#Override
public void changed(final ObservableValue<? extends String> observable, final String oldValue, final String newValue)
{
if (textArea.getText().charAt(textArea.getText().length()-1) == '{')
{
textArea.appendText("}");
}
}
});
But since it's only checking the last character in the textArea, this doesn't work for code where there are brackets inside brackets. Does anyone know a way to fix this? It may also be helpful to note that I am using JDK 1.7.0_55 and my school refuses to update to JDK 8. Any help will be appreciated.
You should use anchor property to receive position of caret instead of using text length value.
Suppose I have a String "CAA". What I am doing now is randomly adding some Character to it for e.g. lets say I am adding Character 'B' and 'D' and shuffle that whole Character ArrayList and placing them in some Buttons as below
Here my character ArrayList contains {'A','C','B','A','D'}
What I am trying to do is when I click on a button I want something like below
I mean only keep the Buttons that contain letters of Original string and disable all other Buttons.
Create a Custom Class that wraps a Character and a boolean flag to maintain that its original character.
class MyCharacter {
private Character c;
private boolean isOriginal;
public MyCharacter(Character c, boolean isOriginal) {
this.c = c;
this.isOriginal = isOriginal;
}
public Character getChar() {
return c;
}
public boolean isOriginal() {
return isOriginal;
}
}
Instead of adding Character into list. Add MyChracter object.
For original chracters pass boolean true in constructor and for later random ones pass false in it.
Do shuffling or whatever you want. At any moment iterate over the list. Get the MyChracter object and check whether its original or not.
This should be simple to do.
Hope this helps
Cheers
You could keep several different ArrayLists. One holds the characters that you display, one holds the original, and then when a button is clicked test to see if a button is in the original ArrayList before graying it out.
I want to add a special character to a textfield .
For example I want to add / automatically between a date that user typed.
Or adding some space between some digits in a number .like this: "2020 2020 2020 2020"
I used this code but it doesn't work correctly .
textfield.textProperty().addListener(new ChangeListener<String>(){
#Override
public void changed(ObservableValue<? extends String> ov, String t, String t1) {
if(t1.length()==4 || t1.length()==9 || t1.length()==14){
textfield.setText(t1+" ");
System.out.println("space added");
}
}
}
It's adding the space just fine. I think the issue is that you want to move the carat position after adding the extra text. You can use textfield.getCaratPosition() to find the current position and textfield.positionCarat(...) to change it.
The logic is going to be quite complex though and depends greatly on what the user is doing and precisely how you want the text field to behave. E.g. what if the text is changing because the user deletes something? What about copy and paste?
I have field in which only Alphanumeric values are allowed. I need to check a scenario where user could enter capital letter using "shift" key (OKAY)
SHIFT+a =A
Also a possibility junk characters can be entered using "shift" key (NOT OKAY), like
SHIFT+1 = !
How do I put a validation in a way that only characters are allowed if "SHIFT" key is pressed?
if(NativeEvent.getShiftKey()){
...........
}
If you want to detect pressed keys, you can use a KeyUpHandler or KeyDownHandler.
TextBox t = new TextBox();
t.addKeyUpHandler(new KeyUpHandler() {
#Override
public void onKeyUp(KeyUpEvent event) {
int pressedKey = event.getNativeKeyCode();
}
});
You can use the "pressedKey" integer to see what key code user pressed. Also interesting is KeyCodes, which has the codes for special keys.
If you want to validate user input instead, the easiest way is with HTML5 form validation. To implement that method, you can set up your field like this:
TextBox t = new TextBox();
t.getElement().setAttribute("required", "required");
t.getElement().setAttribute("pattern", "[a-zA-Z0-9]+");
Here's another resource for HTML5 form validation. And another.
I am currently creating this java GUI that will ask the user to input 10 entries, then use the values to execte the next action.
I want only numbers or decimal point to be inputted inside such that it can only be a float value.
If it is not number or decimal point, it should prompt the user to input that specific entry again before the next action is executed.
How should I do it?
Wong,
not sure whether you are using Swing or not...
Ages ago I had the same problem and I solved it with creating a class RestrictedTextField extending JTextField. In the constructor I added a key listener (addKeyListener(new RestrictedKeyAdapter());)
private class RestrictedKeyAdapter extends KeyAdapter {
#Override
public void keyReleased(KeyEvent e) {
if (getText().equals("")) {
oldString = "";
return;
} else {
// if you cannot parse the string as an int, or float,
// then change the text to the text before (means: ignore
// the user input)
try {
if (type.equals("int")) {
int i = Integer.parseInt(getText());
oldString = getText();
} else if (type.equals("float")) {
float f = Float.parseFloat(getText());
oldString = getText();
} else {
// do nothing
}
} catch (NumberFormatException el) {
setText(oldString);
}
// if the text is identical to the initial text of this
// textfield paint it yellow. If the text was changed
// paint it red.
if (initialString.equals(getText())) {
setForeground(Color.YELLOW);
} else {
setForeground(Color.RED);
}
}
}
}
The idea is, that every time the user presses a key in the textfield (and releases it then), the text in the textfield is parsed. If the component should accept only floats for example then the component tries to parse it as an float (Float.parseFloat(..)). If this parsing is successful everything is fine. If the parsing fails (an NumberFormatException is thrown) then the old text is written back into the textfield (literally ignoring the user input).
I think you can add the KeyAdapter directly to the JTextField without creating a dedicated class for that, but with this solution you can remember the initial string and the old string.
you can play around with the code.. you can change the colour of the textfield if the input is valid or not (or like in my code snippet if the text is identical to the initial string).
one additional comment: I set the 'type' of the textfield in a variable with the name 'type', which is simply a String with the values "int", "float", etc.... a better solution would be here for example an enum of course...
I hope this is helpful...
timo
There are various options for what you would like to do. You can check here for one example of doing so. Another example could be to use Formatted TextFields, as shown here.
On the other hand, upon submission, you can try to parse the value to a float or double. If you get any exceptions, then, the value is not a number.
Lastly, you can use Regular Expressions. An expression such as ^\\d+(\\.\\d+)?$ should match any integer or floating point number.