How to disable paste action in SWT Text - java

I have an SWT Text and i dont want any special characters to be entered , but only digits and numbers. So i have used the verify lister for the text :
text.addVerifyListener(new VerifyListener()
{
#Override
public void verifyText(VerifyEvent event)
{
char eachChar = (Character)event.character;
if(Character.isLetterOrDigit(eachChar))
{
event.doit = true;
}
else
{
event.doit = false;
}
}
});
So now i'll not be able to enter special characters.
1) How do i enable paste of a text inside the Text ?
2) And when i copy a text containing special characters from outside and paste it in the Text , it should not get pasted How do i restrict this ?
Please suggest me on this.

1) How do i enable paste of a text inside the Text ?
Paste will be enabled by default for SWT text. You no need to enable explicitly.
2) And when i copy a text containing special characters from outside and paste it in the Text , it should not get pasted How do i restrict this ?
"event.text" will give you the text you enter/paste to the SWT text. Validate this event.text with your regular expression (or with your isLetterOrDigit() by reading the event.text as character)

Related

Java JTextField text length not updating after deleting characters

I am trying to create a JTextField with a key listener that will enable a button only when the length of the text is exactly 4.
It's working but if length of 4 is reached, and I delete a character (using backspace or delete buttons) the text length isn't updated and keeps being 4, until I remove another character, but that will make it actually 2 characters.
Can you help me with the code to make it update the length? Thanks in advance.
I already tried these without success:
Document doc = field.getDocument();
if (doc.getLength() > 0) {
try {
doc.remove(field.getCaretPosition(), 1);
} catch (BadLocationException e1) {
}
}
and
field.setText(field.getText().substring(0, field.getText().length()));
Found the error:
I was calling the method to enable the button in the KeyListener's keyTyped method, instead of doing it in keyReleased method.
That simple change, made the code work as expected.

Copy&Paste to JavaFX TextArea and keep the Line break

I'm currently writing a Tool in JavaFX.
Therefor I need to copy some columns from Microsoft Excel using cmd + c,
I then have the columns separated by tab in the clipboard.
e.g:
Column1 Column2 Column3
Value1 Value2 Value3
Value4 Value5 Value6
When I past the clipboard in an editor like Atom, they look exactly that way, but when I paste them into a TextArea, the line breaks are gone.
I need to do this, because I want to process the data from Excel using the Open CSV library.
Is there a way to keep the line breaks?
EDIT:
I just found out, if I use Clipboard.getSystemClipboard().getString() and than pass the String to my Open CSV handler, it works, but if I set the same string in a TextArea using textArea.setText(string) there are still no line breaks in the displayed Text.
Although it works now, I would like to user to paste the data into a TextArea, because it's a more common way than clicking a "paste&load" Button, and the user then also can check if all of the data was pasted.
I think the issue is that when Excel copies text to the System clipboard, it represents a line break as '\r', (ASCII 0xc) instead of the more standard '\n' (ASCII 0xa).
This is something of a hack, but if you do
TextArea textArea = new TextArea(){
#Override
public void replaceText(IndexRange range, String text) {
super.replaceText(range, text.replaceAll("\r", "\n"));
}
#Override
public void replaceText(int start, int end, String text) {
super.replaceText(start, end, text.replaceAll("\r", "\n"));
}
#Override
public void replaceSelection(String replacement) {
super.replaceSelection(replacement.replaceAll("\r", "\n"));
}
});
it will filter all the '\r' and replace them with '\n'.
Edit: here is a slightly cleaner version using a TextFormatter:
TextArea textArea = new TextArea();
UnaryOperator<Change> filter = c -> {
c.setText(c.getText().replaceAll("\r", "\n"));
return c ;
};
textField.setTextFormatter(new TextFormatter<>(filter));

add a special character to a text field in javafx

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?

How to efficiently edit the text in an EditText in Android

I am new to java and android development and I am working on an application that takes input from the user through an EditText. However, sometimes the input data has some characters with an encoding that is different from what the app expects.
I have created a hashmap with all the possible mapping (about 460 elements) and whenever the user types in (or paste) something I use the afterTextChanged method to go through the text and swap the ones that matches the incorrect encoding with the correct ones that displays correctly.
My code works fine for small sized entry, however the longer it is the slower it gets because afterTextChanged will be called recursively after every swap I do. And when pasting a text that has about 30+ characters with incorrect encoding the application will crash.
here is my code:
public void afterTextChanged(Editable editable) {
substituteText(editable);
}
public CharSequence substituteText(Editable txt) {
if (txt.length() == 0) {
return txt;
}
for (int i = 0; i < txt.length(); i++) {
char ch = txt.charAt(i);
CharSequence ret = myHashMap.get(ch);
if (ret != null) {
txt.replace(i, i + 1, ret);
}
}
return txt;
}
I thought about cloning "editable" to some, change it and then copy it back to the edittext editable variable so it will only see it as one change but I failed to do it since I couldn't find an easy way to clone it.
Is there a more efficient way to do this?
If you use onTextChanged instead of afterTextChanged, you'll get passed the new character(s) the user typed and where they are, and you'd only need to check those.

How to not add a string to a text area if it contains something - Java

I have the following:
InputStream inputFromServer
TextArea t
String display
String validation
String display holds the data from the inputstream, it then adds it to the text area. I want to check that if display contains validation then dont write it to the text area. Here is the code I currently have:
while ((inputFromServer.read())!=-1)
{
display = display + ((char)inputFromServer.read());
t.setText(s);
}
I want something like:
while ((inputFromServer.read())!=-1)
{
display = display + ((char)inputFromServer.read());
if display contains validation {
THEN DONT WRITE IT TO THE TEXT AREA
}
else{
t.setText(s);
}
}
If the method is completely wrong then can someone guide me in the right direction please? Thanks
You could do like this:
while ((inputFromServer.read())!=-1)
{
display = display + ((char)inputFromServer.read());
if (!display.contains(VALIDATION_STRING) {
t.setText(s);
}
}
Ok since the validation String is possibly uninitialized you can check against that
display += ((char)inputFroMServer.read());
if(validationString.equals("") || !display.contains(validationString)) {
t.setText(display);
}
Or like I said in my comment you can do:
!display.toLowerCase().contains(validationString.toLowerCase())
if you don't care about case
Also if you are going to editing the display string a bunch of times you should consider using a StringBuilder

Categories

Resources