How to re-position cursor in TextArea - java

i have set some text in JTextArea. The cursor is in 5th line. Now I want to set some text in the first line.
So is it possible to re position the cursor to desired line?

Use JTextComponent.setCaretPosition(int) which:
Sets the position of the text insertion caret for the TextComponent. Note that the caret tracks change, so this may move if the underlying text of the component is changed. If the document is null, does nothing. The position must be between 0 and the length of the component's text or else an exception is thrown.

If you want to go from one actual text line to another text line then you still need to use the JTextComponent.setCaretPosition() method but what you will also need is a means to get the desired line starting index to pass to the JTextComponent.setCaretPosition() method. Here is how you can get the starting index of any supplied line number providing the line number supplied exists within the document:
public int getLineStartIndex(JTextComponent textComp, int lineNumber) {
if (lineNumber == 0) { return 0; }
// Gets the current line number start index value for
// the supplied text line.
try {
JTextArea jta = (JTextArea) textComp;
return jta.getLineStartOffset(lineNumber-1);
} catch (BadLocationException ex) { return -1; }
}
How you might use the above method (let's say from the ActionPerformed event of a JButton):
int index = getLineStartIndex(jTextArea1, 3);
if (index != -1) {
jTextArea1.setCaretPosition(index);
}
jTextArea1.requestFocus();
The example usage code above will move the caret (from whatever location it happens to be in within the document) to the beginning of line 3 within the same document.
EDIT: Based on question in comments...
To move the caret to the end of a line you can make yet another method very similar to the getLineStartIndex() method above except now we'll name it getLineEndIndex() and we'll make a single code line change:
public int getLineEndIndex(JTextComponent textComp, int lineNumber) {
if (lineNumber == 0) { return 0; }
// Gets the current line number end index value for
// the supplied text line.
try {
JTextArea jta = (JTextArea) textComp;
return jta.getLineEndOffset(lineNumber-1) - System.lineSeparator().length();
} catch (BadLocationException ex) { return -1; }
}
Use this method the same way as the getLineStartIndex() method shown above.

Related

Reading a text file into an array and performing a sort in Java

I have a homework question I need help with
We have been given a text file containing one word per line, of a story.
We need to read this file into an array, perform a sort on the array and then perform a binary search.
The task also says I'll need to use an overload method, but I'm unsure where
I have a bubble sort, that I've tested on a small array of characters which works
public static void bubbleV1String(String[]numbers)
{
for(int i = 0; i < numbers.length-1; i++)
{
for(int j = 0; j < numbers.length-1; j++)
{
if(numbers[j] .compareTo(numbers[j+1])>0)
{
String temp = numbers[j+1];
numbers[j+1] = numbers[j];
numbers[j] = temp;
}
}
}
}`
And my binary search which I've tested on the same small array
public static String binarySearch(int[] numbers, int wanted)
{
ArrayUtilities.bucketSort(numbers);
int left = 0;
int right = numbers.length-1;
while(left <= right)
{
int middle = (left+right)/2;
if (numbers[middle] == wanted)
{
return (wanted + " was found at position " + middle);
}
else if(numbers[middle] > wanted)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return wanted + " was not found";
}
Here is my code in an app class to read in a file and sort it
String[] myArray = new String[100000];
int index = 0;
File text = new File("threebears.txt");
try {
Scanner scan = new Scanner(text);
while(scan.hasNextLine() && index < 100000)
{
myArray[index] = scan.nextLine();
index++;
}
scan.close();
} catch (IOException e) {
System.out.println("Problem with file");
e.printStackTrace();
}
ArrayUtilities.bubbleV1String(myArray);
try {
FileWriter outFile = new FileWriter("sorted1.txt");
PrintWriter out = new PrintWriter(outFile);
for(String item : myArray)
{
out.println(item);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
When I go to run the code, I get a null pointer exception and the following message
Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.String.compareTo(Unknown Source)
at parrayutilities.ArrayUtilities.bubbleV1String(ArrayUtilities.java:129)
at parrayutilities.binarySearchApp.main(binarySearchApp.java:32)
Line 129 refers to this line of code of my bubblesort
if(numbers[j] .compareTo(numbers[j+1])>0)
And line 32 refers to the piece of code where I call the bubblesort
ArrayUtilities.bubbleV1String(myArray);
Does anyone know why I'm getting a null pointer exception when I've tested the bubblesort on a small string array? I'm thinking possibly something to do with the overloaded method mentioned earlier but I'm not sure
Thanks
You are creating an array of length 100000 and fill the lines as they are read. Initially all elements will be null and after reading the file quite a number of them is likely to still be null. Thus when you sort the array numbers[j] will eventually be a null element and thus calling compareTo(...) on that will throw a NullPointerException.
To fix that you need to know where in the array the non-null part ends. You are already tracking the number of read lines in index so after reading the file that would be the index of the first null element.
Now you basically have 2 options:
Pass index to bubbleV1String() and do for(int i = 0; i < index-1; i++) etc.
Make a copy of the array after reading the lines and before sorting it:
String[] copy = new String[index];
StringSystem.arrayCopy(myArray,0,copy,0,index);
//optional but it can make the rest of the code easier to handle: replace myArray with copy
myArray = copy;
Finally you could also use a List<String> which would be better than using arrays but I assume that's covered by a future lesson.
It seems that you have some null values in your numbers array. Try to debug your code (or just print array's content) and verify what you have there. Hard to tell anything not knowing what is in your input file.
Method overloading is when multiple functions have the same name but different parameters.
e.g. (taken from wikipedia - function overloading)
// volume of a cube
int volume(const int s)
{
return s*s*s;
}
// volume of a cylinder
double volume(const double r, const int h)
{
return 3.1415926*r*r*static_cast<double>(h);
}
Regarding your null pointer exception, you've created an array of size 100000, but it's likely you haven't read in enough information to fill that size. Therefore some of the array is empty when you try to access it. There are multiple ways you can go about this, off the top of my head that includes array lists, dynamic arrays or even moving the contents of the array to another one, once you know the size of the contents (however this is inefficient).

Java code not hitting IF or ELSE in Android Studio

I am running Android Studio and setting breakpoints, but on both of my IF...ELSE conditions, the code is not being executed. It seems to me that such a thing is impossible. Either the IF or the ELSE should be true... right?
The code is this:
if (lastReading.isItTimeYet(Calendar.getInstance()))
{
lastReadingReturn = lastReading.SensorReadingChanges(z_value, chkOrient, inclination, rotation);
if (lastReadingReturn.isEmpty())
{
String EMPTY = "TRUE";
// DO NOTHING
}
else
{
int stopHERE = 0;
}
}
lastReadingReturn is a string. It is getting a value from SensorReadingChanges just fine. I don't see any errors being thrown.
I put break points on both the String EMPTY = "TRUE"; line and the int stopHERE = 0; line, but neither is hit. I can stop on the line before the if. But when I try to step into or step over the next line, the debugger jumps to the first line of code that is OUT of the If clause. In other words, it just skips it.
I have run it with both conditions (i.e. the string being checked is empty and the string being checked has a value) but it doesn't matter. Neither is hit.
Here is a screenshot of my Android Studio running in debugger:
Both your if-block and your else-block contains code that is very likely to be removed by the compiler during optimization since they actually don't do anything.
Try replacing them with something that actually does something like logging a message or move the declaration of EMPTY and stopHERE outside of their respective blocks.
String EMPTY;
int stopHERE;
if (lastReading.isItTimeYet(Calendar.getInstance()))
{
lastReadingReturn = lastReading.SensorReadingChanges(z_value, chkOrient, inclination, rotation);
if (lastReadingReturn.isEmpty())
{
EMPTY = "TRUE";
// DO NOTHING
System.out.println("Doing nothing");
}
else
{
stopHERE = 0;
System.out.println("stopHERE set to zero");
}
}
Edit: Since I'm not being believed I simulated OPs issue in Android Studio:
final Random random = new Random();
if(random.nextInt(1) != 0) {
String EMPTY = "EMPTY";
} else {
int stopHERE = 0;
}
So, Android Studio is actually warning us that there is no executable code inside our else-block where the breakpoint is.
When run, this will only pause execution once, on row 18 (since random.nextInt(1) will always be 0).

Android/Java EditableTextView Line Count

I am trying to teach myself Android Java programming and I have started by attempting to create a simple text editor.
I wanted to have a line count down the left hand side like standard IDEs, and I couldn't really find anywhere on StackExchange or the internet on the definitive "best practice" way to do something like this.
So I created my own logic based on what I read, but I wanted to just check that this was the best and most efficient way to do it -- and also if this happens to help anyone out looking to do the same thing.
// START onCreate
// #mEditText = Main AutoCompleteTextView
// #mLineCount = Line Count TextView
mEditText.addTextChangedListener(new TextWatcher() {
// Set current line variable
private int currentLine;
// Text Watcher
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Before new text is inserted, get the current line count
currentLine = mEditText.getLineCount();
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
#Override
public void afterTextChanged(Editable e) {
// Update Line Count
// #mEditText: AutoCompleteTextView Input
// #mLineCount: TextView Output
// #currentLine: Integer
updateLineCount(mEditText, mLineCount, currentLine);
}
});
// END onCreate
public void updateLineCount(AutoCompleteTextView editText, TextView lineText, int currentLine){
// Get updated Line Count
int lineCount = editText.getLineCount();
// If that Line Count exists and IS NOT the "before" Line Count (to stop repeating)
if(lineCount > 0 && lineCount != currentLine){
// If "before" Line Count is smaller, push Line Count up
if(currentLine < lineCount){
lineText.append(Integer.toString(lineCount) + "\n");
}
// Else if "before" Line Count is greater (ie. you have deleted a line), push Line Count down
else {
// Get Text of current lineText TextView, replace with a substring of
// the current lineText TextView - the length of the deleted line
// (ie. Line 9 = 1 Character + 1 for the line break; Line 10 = 2 Characters + 1 etc)
lineText.setText(lineText.getText().toString().substring(0, lineText.getText().toString().length() - (Integer.toString(lineCount+1).length() + 1)));
}
}
return;
}
So yeah, this is working fine -- but I am especially not sure about that last line -- Seems abit ... resource wasteful .. to be replacing the entire lineText TextView content each time a line is deleted.
Is there an anti-append that might work better in this situation?
Thanks,
Jamie
if(lineCount > 0 && lineCount != currentLine){
What happend if I select all text and delete all.
That will not meet the condition. So, mabe text line still display "5 lines" rather than "0 line"

Why does this code have a "one off error"?

I am building a GWT component to behave much like the comments box here on stackoverflow, and other sites. I am trying to register listeners for KeyPress, Change and ONPASTE events that will update my status line with number of characters remaining, etc.
It works except it is always one character behind the actual number of characters in the text area. I set the max number of characters to 10. When I type the first character it still says, "10 characters remaining". It doesn't update the status line until I type the second character and then it is one off, it says 9 characters remaining when the second character is typed.
When I BACKSPACE or DELETE, it is also one off, when there are no characters it still says "9 characters remaining" until I press the BACKSPACE or DELETE a second time.
I am getting this behavior in both Firefox, Chrome and Internet Explorer on Windows. So I think I am not registering something correctly.
I know this has something to do with when the events are getting fired, but I have spend hours on trying to diagnose this behavior and have run out of ideas.
Here is where I am registering the event handlers, the complete code is BoundedTextAreaWithFeedback.
private void registerHandlers()
{
final BoundedTextAreaWithFeedback outer = this;
this.textArea.addChangeHandler(new ChangeHandler()
{
public void onChange(final ChangeEvent changeEvent)
{
outer.validate();
}
});
this.textArea.addKeyPressHandler(new KeyPressHandler()
{
public void onKeyPress(final KeyPressEvent keyPressEvent)
{
outer.validate();
}
});
this.panel.addFocusHandler(new FocusHandler()
{
public void onFocus(final FocusEvent focusEvent)
{
outer.textArea.setFocus(true);
}
});
// capture paste events
this.textArea.sinkEvents(Event.ONPASTE);
}
Here is the validate() method.
private boolean validate()
{
final boolean isValid;
final int len = this.textArea.getText().length();
if (len < this.minLength)
{
this.status.setText("Enter at least " + this.minLength + " characters.");
this.status.setStyleName("input-status-underflow");
isValid = false;
}
else if (len > this.maxLength)
{
this.status.setText(this.maxLength - len + " characters remaining");
this.status.setStyleName("input-status-overflow");
isValid = false;
}
else
{
this.status.setText(this.maxLength - len + " characters remaining");
this.status.setStyleName("input-status-ok");
isValid = true;
}
return isValid;
}
I just started adding every addXXXHandler() until one worked.
this.textArea.addKeyUpHandler(new KeyUpHandler()
{
public void onKeyUp(final KeyUpEvent event)
{
outer.validate();
}
});
Seems to have done the trick.
Here is the working code, CTRL-V and paste from context menu also work now.
Try using a DeferredCommand to execute the validation code. I believe the problem is that when the event is firing, they character is not yet added to the text area. The DeferredCommand will not execute until any pending event handlers have finished, allowing the length of the text to be calculated correctly.
See this question for an example of using a DeferredCommand.

Ignoring focusLost(), SWT.Verify, or other SWT listeners in Java code

Outside of the actual SWT listener, is there any way to ignore a listener via code?
For example, I have a java program that implements SWT Text Widgets, and the widgets have:
SWT.Verify listeners to filter out unwanted text input.
ModifyListeners to wait for the correct number of valid input characters and automatically set focus (using setFocus())to the next valid field, skipping the other text widgets in the tab order.
focusLost(FocusEvent) FocusListeners that wait for the loss of focus from the text widget to perform additional input verification and execute an SQL query based on the user input.
The issue I run into is clearing the text widgets. One of the widgets has the format "####-##" (Four Numbers, a hyphen, then two numbers) and I have implemented this listener, which is a modified version of SWT Snippet Snippet179. The initial text for this text widget is " - " to provide visual feedback to the user as to the expected format. Only numbers are acceptable input, and the program automatically skips past the hyphen at the appropriate point.
/*
* This listener was adapted from the "verify input in a template (YYYY/MM/DD)" SWT Code
* Snippet (also known as Snippet179), from the Snippets page of the SWT Project.
* SWT Code Snippets can be found at:
* http://www.eclipse.org/swt/snippets/
*/
textBox.addListener(SWT.Verify, new Listener()
{
boolean ignore;
public void handleEvent(Event e)
{
if (ignore) return;
e.doit = false;
StringBuffer buffer = new StringBuffer(e.text);
char[] chars = new char[buffer.length()];
buffer.getChars(0, chars.length, chars, 0);
if (e.character == '\b')
{
for (int i = e.start; i < e.end; i++)
{
switch (i)
{
case 0: /* [x]xxx-xx */
case 1: /* x[x]xx-xx */
case 2: /* xx[x]x-xx */
case 3: /* xxx[x]-xx */
case 5: /* xxxx-[x]x */
case 6: /* xxxx-x[x] */
{
buffer.append(' ');
break;
}
case 4: /* xxxx[-]xx */
{
buffer.append('-');
break;
}
default:
return;
}
}
textBox.setSelection(e.start, e.start + buffer.length());
ignore = true;
textBox.insert(buffer.toString());
ignore = false;
textBox.setSelection(e.start, e.start);
return;
}
int start = e.start;
if (start > 6) return;
int index = 0;
for (int i = 0; i < chars.length; i++)
{
if (start + index == 4)
{
if (chars[i] == '-')
{
index++;
continue;
}
buffer.insert(index++, '-');
}
if (chars[i] < '0' || '9' < chars[i]) return;
index++;
}
String newText = buffer.toString();
int length = newText.length();
textBox.setSelection(e.start, e.start + length);
ignore = true;
textBox.insert(newText);
ignore = false;
/*
* After a valid key press, verifying if the input is completed
* and passing the cursor to the next text box.
*/
if (7 == textBox.getCaretPosition())
{
/*
* Attempting to change the text after receiving a known valid input that has no results (0000-00).
*/
if ("0000-00".equals(textBox.getText()))
{
// "0000-00" is the special "Erase Me" code for these text boxes.
ignore = true;
textBox.setText(" - ");
ignore = false;
}
// Changing focus to a different textBox by using "setFocus()" method.
differentTextBox.setFocus();
}
}
}
);
As you can see, the only method I've figured out to clear this text widget from a different point in the code is by assigning "0000-00"
textBox.setText("000000")
and checking for that input in the listener. When that input is received, the listener changes the text back to " - " (four spaces, a hyphen, then two spaces).
There is also a focusLost Listener that parses this text widget for spaces, then in order to avoid unnecessary SQL queries, it clears/resets all fields if the input is invalid (i.e contains spaces).
// Adding focus listener to textBox to wait for loss of focus to perform SQL statement.
textBox.addFocusListener(new FocusAdapter()
{
#Override
public void focusLost(FocusEvent evt)
{
// Get the contents of otherTextBox and textBox. (otherTextBox must be <= textBox)
String boxFour = otherTextBox.getText();
String boxFive = textBox.getText();
// If either text box has spaces in it, don't perform the search.
if (boxFour.contains(" ") || boxFive.contains(" "))
{
// Don't perform SQL statements. Debug statement.
System.out.println("Tray Position input contains spaces. Ignoring.");
//Make all previous results invisible, if any.
labels.setVisible(false);
differentTextBox.setText("");
labelResults.setVisible(false);
}
else
{
//... Perform SQL statement ...
}
}
}
);
OK. Often, I use SWT MessageBox widgets in this code to communicate to the user, or wish to change the text widgets back to an empty state after verifying the input. The problem is that messageboxes seem to create a focusLost event, and using the .setText(string) method is subject to SWT.Verify listeners that are present on the text widget.
Any suggestions as to selectively ignoring these listeners in code, but keeping them present for all other user input?
Thank you in advance for your assistance.
If you name the listener instead of using an anonymous one, you can add and remove it whenever you like.
Example:
// Adding focus listener to textBox to wait for loss of focus to perform SQL statement.
FocusAdapter focusTextBox = new FocusAdapter()
{
#Override
public void focusLost(FocusEvent evt)
{
// Get the contents of otherTextBox and textBox. (otherTextBox must be <= textBox)
String boxFour = otherTextBox.getText();
String boxFive = textBox.getText();
// If either text box has spaces in it, don't perform the search.
if (boxFour.contains(" ") || boxFive.contains(" "))
{
// Don't perform SQL statements. Debug statement.
System.out.println("Tray Position input contains spaces. Ignoring.");
//Make all previous results invisible, if any.
labels.setVisible(false);
differentTextBox.setText("");
labelResults.setVisible(false);
}
else
{
//... Perform SQL statement ...
}
}
};
You can then add the listener by doing this:
textBox.addFocusListener(focusTextBox);
Removal is as easy as this:
textBox.removeFocusListener(focusTextBox);
Just make sure to re-enable the listener after you've done what you've wanted to programmatically achieve, or your code will not act like you'd expect.
I didn't tested your code, but try method isFocusControl() in the listener to see if the user enters the text or you set the text with setText() when the focus is not in textBox.

Categories