Replace EditText selection - java

How can I replace the selected text with something in android's edittext?
For example:
Default Text
Assume "Default" is selected now.
I want to replace it with !boldDefault.
How can I do it? I tried using get selection method but I couldn't replace it.

If the EditText currently has text selected, you can access the start and end points of the selection like so:
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
You can access the EditText's Editable text like this:
Editable edit = et.getText();
Now you can replace anything inside that Editable using the replace() method:
String newText = "this will replace the current selection";
edit.replace(start, end, newText);
Once you've done that, you probably want to change the selection so that you don't have part of your new text still selected:
editText.setSelection(start + newText.length());

Related

Why does my JTextArea not add in the line breaks from my String?

I want a JTextArea to display the returned text when by user clicks a button, but although the actual string contains line breaks, the text area doesn't display them? Why?
//when user clicks this button//
txtPizzaOrder.setText(newPizza.getInfo());
public String getInfo(){
String sizeString=String.valueOf(this.size);
String rtnStment="Size: "+sizeString+"\nCrust:"+this.crust+"\nSauce: "
+this.sauce+"\nFirst topping: "+this.topping1+"\nSecond topping: "+
this.topping2+"\nCost: "+this.getCost()+"\n------------------------";
return rtnStment;
}
Based on your picture, I see the text in the middle of the component, not the top.
This tells me you are probably using a JTextField, not a JTextArea.
If you need more help post a proper "MCVE". You have already been asked for this before.

How to change assigned string with method

I am trying to write a program where when I use a button a "\" shows, then if I hit that same button a "X" shows, and finally if I hit that same button a third time "(X)" shows. Right now I have this method ......
public void display20closePlayer1(String close) {
TextView playerOneTwentyClose = (TextView) findViewById(R.id.player_one_20_close);
playerOneTwentyClose.setText(String.valueOf(close));
}
that finds the button and sets the text with this....
public void twentyCloseOutPlayer1(View v) {
playerOneClose20 = playerOneClose20 + "\\";
display20closePlayer1 (playerOneClose20);
}
I am storing the string in a public object in my main activity...
String playerOneClose20 = "";
When I run my code and press the button I get a "\" to show up, if I hit it again I get "\" and so on.
Can someone please help to explain a method that would replace the already called "\", with an "X" and then replace the "X" with "(X)" Thanks for the help!
You’re close! The way I would do this is with a global int variable that denotes the stage you’re on (i.e 1 => “/“, so on and so forth)
Then in your on button click listener get the text view and set it depending on the stage number.
Remember to set and reset that int variable otherwise it will mess up your code

Java Moving the Cursor in a JTextField

I have seen questions about moving a cursor using the Robot class by an x and y coordinate, but I am trying to figure out how to reposition a cursor among text in a JTextField.
I have an open parenthesis button that when clicked will take whatever text might be in the JTextField already, concat "(" to it and set this to the JTextField.
I was wondering how I might add the closing parenthesis as well, BUT put the cursor in between the 2 so the user can keep typing uninterrupted. Any suggestions?
If you want to move the Caret in a JTextField to a specific location from a button then one way to do this would be to set focus upon it first using the JTextField.requestFocus() method then you would need to use the JTextField.setCaretPosition() method to actually relocate the Caret.
If you have a JTextField named jTextField1 and you want to move the Caret to the end of the text contained within then you can use:
jTextField1.requestFocus(); //
jTextField1.setCaretPosition(jTextField1.getText().length());
You need to be careful not to exceed the length of text within the JTextField otherwise an IllegalArgumentException will occur which you can catch by surrounding the above code within a try/catch block. You will also need to consider those times when there might not be any text within the JTextField.
try {
jTextField1.requestFocus();
jTextField1.setCaretPosition(jTextField1.getText().length());
}
catch (IllegalArgumentException ex) {
///Do Something Here...
}
To Append Brackets to the end of a JTextField then place the Caret between them would be something like this:
String txt = jTextField1.getText(); // Get the text contained in Textfield (if any)
if (txt.equals("")) { txt+= "()"; } // Nothing for text so just add Parenthases
else { txt+= " ()"; } // Some text there so add a space and Parenthases
jTextField1.setText(txt);
try {
// Set focus to JTextField
jTextField1.requestFocus();
// Move the caret between the Parenthases
jTextField1.setCaretPosition(jTextField1.getText().length()-1);
}
catch (IllegalArgumentException ex) {
// Do something here...
}
I have an open parenthesis button that when clicked will take whatever text might be in the JTextField already, concat "(" to it and set this to the JTextField.
Don't use getText()/setText() to do this.
Instead you just want to "append" the new text to the text field.
So the logic in your ActonListener might be something like:
int end = textField.getDocument.getLength();
textField.setCaretPosition(end);
textfield.replaceSelection("()");
textField.setCaretPosition(end + 1);
Appending text is more efficient because you only generate a DocumentEvent for the added text.
If you use the getText()/setText() approach then you generate a DocumentEvent for the remove text and then a second event for the text added, which does not reflect what actually happened.
Also, using the length from the Document instead of getting the text is also more efficient since you don't need to actually create a String object.

TableViewer how to set caret position for the cell with Editing Support and remove FULL_SELECTION

I have a TableViewer with last column which has EditingSupport to edit the cell's value. The viewer has style set to FULL_SELECTION.
Once I click on the cell, the text value is fully selected:
I can start editing, it will clear off the current value "max value" and start new value.
However, If i want to move the mouse to a specific position to edit, I need to give a delay of a sec or so. If I click too fast right after the first click to select, the cell will be deselected.
Is there any way to avoid that? Can I make the cell not fully selected ? or always have the caret to the end of the text ? Thanks!
I assume you are using TextCellEditor as the cell editor.
Create your own class extending TextCellEditor and override the doSetFocus class to be something like:
#Override
protected void doSetFocus()
{
super.doSetFocus();
if (text != null) {
text.setSelection(0, 0);
}
}
to remove full selection and set caret to end of text, override doSetFocus and set text.setSelection( text.getText().length() );
to ignore double click event, override getDoubleClickTimeout to return 0

Enable a Button when String in TextField is more than 5 characters

I want to enable my b_changepw when I enter more than 5 characters in my textfield1. How can I do that?
Here's my code:
String test = textfield1.getText();
if () {
b_changepw.setEnabled(true);
}
I don't know what to put inside if ()
Instead of a TextField, use a JTextField. Then add a DocumentListener that enables or disables the button depending on the text length.

Categories

Resources