TextEditor caret visible when not focused


#1

Is there any way to make the TextEditor's caret visible when it's not actually in focus? It might seem like a weird request in most cases, but in my case I think it makes sense to do.

So I'm creating a "search box" that contains a TextEditor (might change to Label) with a ListBox right below it. The TextBox is obviously for inputting text to search (file names, commands, or whatever it may be) and the ListBox is kind of like a drop down menu that shows the results of the search (with fuzzy matching or whatever).

As of now, here's my behavior. When the TextEditor is focused and you press the down arrow, the ListBox grabs focus and the second item of the ListBox gets selected (the first is already selected). Then you can use the arrow keys to scroll through the matches with the arrow keys. Well when the ListBox is selected and you enter a character (alphanumeric or punctuation), the TextEditor grabs focus again, inserts the character typed at the caret, and selects the first item again. This will probably change, but the point is that I want these to be "one", I guess you could say. I want it to appear as if the TextEditor never lost focus, because that's pretty much how it behaves.

Is this possible at all already? If not, would it be possible to add without actually changing the TextEditor code? Sorry if I'm not making sense, by the way. If you want an example of what I mean, try Sublime Text's or Atom's command palette (Ctrl+Shift+p on Windows).


#2

I think you're thinking about it in the wrong way - rather than wanting to show the cursor when it's not focused, a better approach would be to make sure the text editor stays focused, but make sure that the dropdown attaches to it as a KeyListener to get the up/down keys.


#3

Hmm... I think you're right. I have no idea why I didn't just try that in the first place. :) For some reason, I was thinking that the ListBox would have to gain and keep focus at some point, but now that I think about it more, I can't really think of why it would need to. Thanks for the suggestion man. That would seriously make things easier. This should be a simple edit to my existing code, so I'll have to try this out today.


#4

I think it would be really useful to have methods like selectNextRow() and selectPreviousRow() in the ListBox for things like this. Of course, it won't be hard to implement myself though. 


#5

So yes, just keeping the TextEditor focused and listening to events from it and doing selecting the ListBox items based on the TextEditor's keyUp and keyDown events works perfectly. I can't believe I was going that harder route before, that really makes no sense now. Anyways, thanks you again. I'm glad I asked this question on here. I would've seriously hated sticking to my earlier path. By the way, it took like 5 minutes to switch it to this easier, more understandable way.