Set caret position in javafx.scene.control.TextArea and javafx.scene.control.TextField - javafx-2

I need to set the caret position manually in my code.
There is a getCaretPosition() under javafx.scene.control.TextInputControl but there is no setter method.
How can I set the caret position?

TextArea ta = new TextArea();
ta.setText("1234567890");
ta.positionCaret(4);

You can use the positionCaret function as mentioned before. But make sure to wrap it in Platform.runLater. Otherwise it might not work at all.
Platform.runLater( new Runnable() {
#Override
public void run() {
textArea.positionCaret( 0 );
}
});

There are two method in TextInputControl that allow manipulation of caret position.
These are :-
selectPositionCaret(int pos) - Selects the text between the last caret position up to the current caret position that you entered.
positionCaret(int pos) - Sets the current caret position clearing the previous selection as well.
So i think in your case you want to use the positionCaret method to set the position without any selections.

if you want to add it at the end of your TextField you can do the next
TextFieldName.positionCaret(TextFieldName.getText().length());
this will add the Curser at The end .

Related

insert GlyphProperty.null in shouldGenerateGlyphs causes cursor misplacement problem

I am hiding the character * by inserting GlyphProperty.null then calling setGlyphs(_:properties:characterIndexes:font:forGlyphRange:) inside layoutManager(_:shouldGenerateGlyphs:properties:characterIndexes:font:forGlyphRange:) as described in the answer in https://stackoverflow.com/a/57697139/9568961, it works but after a certain sequence of text editing, the cursor starts to misplace, as shown in the gif https://github.com/dzAtZJU/Demos/blob/master/cursor_misplace.GIF?raw=true
GIF Description: When the second line becomes qq, I try to move the cursor to the end of first line, but it move directly to the beginning. I then try to delete characters, then the cursor move to the end correctly.
The sample project is here https://github.com/dzAtZJU/HideText.
Is it a bug? Have anyone run into this? How can I work around this?
This is a year-old question, but for anyone struggling with the same problem, here's a solution.
My Markdown-like parser knows the ranges for both the line on which the cursor is now, and where it was previously. I'm calling hideAndShowMarkup whenever text view selection changes.
You need to invalidate current glyphs for both of the ranges, and then redraw them synchronously before updating insertion point. The most crucial step is invalidating layout for the range where you want to show Markdown symbols. Without that, your caret gets drawn into the wrong position.
Code is in Objective C, but should be easy to implement in Swift, too.
-(void)hideAndShowMarkup {
// Just for reference
Line* line = currentLine;
Line* prevLine = previouslySelectedLine;
[self.layoutManager invalidateGlyphsForCharacterRange:line.range changeInLength:0 actualCharacterRange:nil];
[self.layoutManager invalidateGlyphsForCharacterRange:prevLine.range changeInLength:0 actualCharacterRange:nil];
[self.layoutManager invalidateLayoutForCharacterRange:line.range actualCharacterRange:nil];
if (NSGraphicsContext.currentContext) {
[self.layoutManager drawGlyphsForGlyphRange:line.range atPoint:self.frame.origin];
[self.layoutManager drawGlyphsForGlyphRange:prevLine.range atPoint:self.frame.origin];
}
[self updateInsertionPointStateAndRestartTimer:NO];
}

How to set the caret to a specific position in etherpad?

I'm authoring a plugin and on some operations of the plugin, the caret moves back to 0:0. I can get the caret position with
exports.aceKeyEvent = function(hook_name, args, cb) {
line = args.editorInfo.ace_caretLine();
char = args.editorInfo.ace_caretColumn();
}
.. but so far I didn't find anything to set the caret. There suppose to be a Set selection method, but the documentation is not giving me any hints.
Thanks to John Mc Lear, the solution came up. example
Only after a call to ace_replaceRange will a call to ace_performSelectionChange set the caret position in an acekeyEvent hook, where we get the ace object with ace_callWithAce.
However, this works in a postAceInit hook.
context.ace.callWithAce(function(ace) {
ace.ace_performSelectionChange([line - 1, char], [line - 1, char], false);
}, 'padsearch_callstack', true);

JavaFX SplitPane Divider Position Inconsistent Behaviour

I have two StackPanes (pane1 & pane2) being displayed in a SplitPane (splitPane). The SplitPane's divider position is set to .3 in the builder. The seond StackPane (pane2) contains a Button which also sets the SplitPane's divider position to .3.
Ceteris paribus, the expected behaviour would be that both actions (setting the divider position in the builder & setting the divider position through an action) either work or not.
Yet, only the latter actually works.
What changes between the construction of the SplitPane and the onAction of the Button. What hinders the placement of the divider in the builder?
StackPane pane1 = new StackPane();
StackPane pane2 = new StackPane();
final SplitPane splitPane = SplitPaneBuilder.create()
.items(pane1, pane2)
// Environment A
.dividerPositions(new double[] {.3}) // splitPane.setDividerPosition(s)(...), etc. yield same result
.orientation(Orientation.HORIZONTAL)
.build();
// The following line influences environment A outcome, though it does not fix the issue
SplitPane.setResizableWithParent(pane1, false);
pane2.getChildren().add(ButtonBuilder.create()
.text("Divider Position")
.onAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// Environment B
splitPane.setDividerPositions(.3);
}
})
.build());
Scene primaryScene = SceneBuilder.create()
.root(splitPane)
.build();
primaryStage.setScene(primaryScene);
primaryStage.setTitle("Name");
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
I had a problem regarding the position of the split pane and it seems similar with yours. Here it is: I have a tabpane, and whenever I add a new tab, I load the content of the tab from an fxml, in which there exists a split pane, with divider position fixed to 0.8.
When the first tab is loaded, it is ok, the divider is positioned at 0.8. When I add the second tab, exactly in the same way with the first tab via fxml, the position of the split pane in the second tab is 0.5, i.e. the default value.
I tried to programatically set the divider position with setDividerPositions method after loading the content of the tab, but it did not work. Then as in your case, if I place the setDividerPositions call in an onAction method, it successfully sets the divider position.
As I stated, my problem is not exactly same as yours, but seems similar. So I am not sure but hope my work around will help you: In my situation, placing the setDividerPositions call in Platform.runLater did the trick.
So, thanks to James D in the Oracle support forums, here is the full answer:
"The issue appears to be that the scene's size is smaller than that of its window. This causes an additional layout pass when the window is first displayed; the second layout pass counts as a "window resize" and consequently the divider position of the split pane is not respected on this pass."
(https://forums.oracle.com/forums/thread.jspa?threadID=2503701)
Workaround 1
Set the Scene's size instead of the Stage's size:
Scene primaryScene = SceneBuilder.create()
.root(splitPane)
.width(500)
.height(500)
.build();
primaryStage.setScene(primaryScene);
primaryStage.setTitle("Name");
primaryStage.show();
Workaround 2
As Ramazan has correctly pointed out, another solution would be to set the divider position in Platform.runLater(...).
Followups
I have filed a bug at jira (http://javafx-jira.kenai.com/browse/RT-28607), referencing the original bug report (http://javafx-jira.kenai.com/browse/RT-17229) which had been marked "unresolveable". I guess the final solution would be to provide methods for the absolute positioning of the divider.

Is there a way to get the cursor position in a mfc application?

I'm using the OnKeyDown handler to detect for an Enter key press:
void CShortestPathFinderView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( nChar == VK_RETURN) //Enter key is pressed
{
CClientDC aDC(this);
rubberbanding = 0;
m_pTempElement->vertices[i++]= /*cursor position??*/;
mSecondPoint=m_pTempElement->vertices[0];
m_pTempElement->Draw(&aDC);
}
In the 3rd line of the if statement I need to store the cursor position in an array, but how do I acquire that point? Does the handler provide me with it? or is there a separate function to do so?
To get the current cursor position, you can call GetCursorPos. I don't believe MFC provides a wrapper for this, so it'll just be the Win32 ::GetCursorPos. It returns the point in screen coordinates, so you'll (almost certainly) want to use ScreenToClient to convert that to client area coordinates before storing it.
Note, however, that GetCursorPos will return the position of the cursor when you call it. That may or may not be quite the same as the position the cursor was at when the key was pressed (though it will normally be at least quite close).
Usually, to add a cursor position like this, you'd want to react to the user clicking the mouse button (e.g., WM_LBUTTONDOWN). That message will tell you the position of the mouse when the button was clicked.

LWUIT - show TextField blinking Cursor, even if the field is empty

I have a TextField in a Form. This TextField should have focus by default,
which works fine. Now I'd like the user to be aware of that and show him,
that he's inside the TextField - so the TextField cursor should be shown
and blink.
I only found drawTextFieldCursor in DefaultLookAndFeel. but I have
absolutely no idea how to apply this to my TextField.
Any help - and code would be appreciated!
Here's a sample. I still don't have it working.
public void search2() {
searchForm = new Form();
TextField searchArea = new TextField();
searchForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
searchForm.addComponent(searchArea);
searchArea.requestFocus();
int i = Display.getInstance().getKeyCode(Display.GAME_FIRE);
searchArea.keyPressed(i);
searchArea.keyReleased(i);
searchForm.show();
}
What happens is: the TextField is focused, it can be directly edited, the "Abc" mode is shown, but what I really want is to show the user the cursor, so he KNOWS he's inside the TextField. This is not happening... if someone could show me some working code for that...
You want the text field to be in editing mode not for the text field cursor to show (which happens when editing). Use requestFocus() to make sure the text field has focus then use something like:
int i = Display.getInstance().getKeyCode(Display.GAME_FIRE);
tf.keyPressed(i);
tf.keyReleased(i);
The drawTextFieldCursor method has a Graphics parameter , so the way you can draw your cursor is :
derive TextField
in its public void paint(Graphics g) you call drawTextFieldCursor after setting the drawing methods of the TextField's paint Graphic.

Resources