How can I create an AutoComplete popup in a JTextPane in Java? - java

I am creating a SQL editor. I am using JTextPane for the editor. I want to implement AutoCompletion for table name etc. like Eclipse.

I think the appropriate class for displaying info on top of another component is JPopupMenu, which already handles layering correctly to display itself. JPopupMenu has a show() method that takes its 'parent' component as an argument, and it will show itself in that component's coordinate space. Since you want to display a selection of terms for the user to choose from, a menu seems appropriate.
To check for text changes, you'd add a DocumentListener to the document that's wrapped by the JTextPane; you can access it using getDocument().
To find out where the cursor (actually, the caret) is, you can use getCaretPosition(). That returns the caret's position within the text stream as an int. You can use modelToView() to translate that position to actual (x,y) coordinates. That in turn will tell you where to show your menu.
You can use addKeyListener() to catch keyboard events on your JTextPane, like hitting Ctrl-Space.
The combination of all that should allow you to do what you're looking to do.

You can also use http://fifesoft.com/autocomplete/. You can install it on any JTextComponent.

For things like this you probably should consider layered panes so your auto-complete suggestions appear in the correct place and z-order.
Furthermore you will have to look for changes in the JTextPane to know when the user is typing and you will need a parser that understands what is typed so you can offer the feature only at appropriate points.
It's not quite clear what exactly your problem is and what you got so far.

I achieved this by adding a key listener to the JTextPane and checking for CTRL + Space keystrokes. When the appropriate key combo was detected the listener went off and looked up the list of possible matches based on the characters directly to the left of the cursor at the time of the key press and found the best matches and displayed them to the user in a JPopup. If there was an exact match then it simply replaced the partial text with the match. If no matches were found an option was given to the user to add the text that they had already typed, edit it and record it into the list of acceptable data.

We use jide. They have a lot of components that help you do this kind of thing really easily

Related

how can i programatically select a particular text from JTable when i do search in it?

here are the screenshots of the application
Rows will be displayed in the Table according to the text which is written in the search textfield.
Now i want to mark that particular text as per the shown in the second image with the yellow color
I know how to select a row or a particular cell.
but I don't know how to select a particular text inside the cell of any row in the table.
I am guessing you know how to search in JTable, so I am not pasting code of it here.
You can look over the SwingX library. It has this kind of function as you said predefined it it. You just need to add it to your table. This is where you can find it. Give it a try you will surely like it.
The basic premise would be to use a custom TableCellRenderer that provided the functionality that you require.
The problem is how to implement it.
I would create a TableCellRenderer based on a JTextField, remove it's border and make it transparent. This will allow you to use the text highlighting functionality provided by JTextCompoent to highlight portions of the text, as demonstrated here.
The next problem is then knowing what to highlight. There are a number of possibilities.
You could provide a method in your table model that could return the current text that should be highlighted.
I'd, personally, probably use the JTable#putClientProperty and JTable#getClientProperty methods to seed the search text.
Or, you could actually provide a simple model that directly to the renderer which had a method that returned the current search text. This might actually be more useful as you could link it to field, the method building the filter and the renderers and allow them to simply seed each other

What's the equivalent of JTextArea.viewToModel for JavaFX's text controls?

In JavaFX, in some text control like TextArea or similar, I want to be able to convert from mouse event coordinates to know exactly which character location is under the mouse. Is there some straightforward way to do that?
In Swing, you can use JTextArea.viewToModel, but I can't find the equivalent in JavaFX yet.
Specifically, I'm trying to find the location in the text (row/column) where mouse events occur. Knowing which side of the char it occurs on would be nice, too, though I don't know if that's supported.
Put another way, I want to decorate the character on mouse over.
I found the answer. It's:
TextArea text = ...
HitInfo hit = ((TextAreaSkin) text.getSkin()).getIndex(me);
Yes, it's in a com.sun.** package but I don't have a choice about that. I can hope that Oracle will someday make the equivalent public.
isLeading on HitInfo doesn't seem to work: it always returns true. And the insertion/char index are always the same even when they shouldn't be... But, at least I can get the insertion/char index. I suppose I should report bugs for those things but since they're not public APIs they probably won't care.
Perhaps coordinates of the mouse events are not necessary. You know that certain mouse events will move the carat (or selection). When you hear one of them inside your textarea, just ask the textarea where the carat wound up.
If that's acceptable, then getAnchor() and getCaretPosition() seem to be what you want.
From the javadoc for getCaretPosition()
The current position of the caret within the text. The anchor and
caretPosition make up the selection range.

Send accelerator with mouse to perform selection of text

I need to make selection of text using mouse instead of ctrl+A
I tried:
sendAcceleratorKey(MouseEvent.BUTTON1, "");
but I don't know which argument could I set to say make a click with mouse and let the mouse enforced to select the text.
If the text is in a JTextComponent, selectAll() may be a suitable choice in your MouseListener.
Addendum: You may also be able to leverage the select-all Action, which is bound to control-A or meta-A by default on various platforms.
Your sscce may be helpful in deciding. There's a related example here.

Code/text folding in SWING

I'm looking for a way to provide 'text folding' capabilities to a swing JTextArea or JTextPane
More specifically, I want to add a block of data in a text component and I want the component to display only some header line. Then the user can unfold the block by clicking some icon. This is just like the code folding feature in most IDE.
I've found ->some sample code<- after some thorough search, but the mechanisms used here are quite obscure to me and it stops working when I try to remove text from the document.
Maybe using XML as input could be a lead ?
This one how to add collapsible area
http://java-sl.com/collapse_area.html
This one how to represent XML
http://java-sl.com/xml_editor_kit.html
I would start by looking at the NetBeans API: http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-editor-fold/overview-summary.html
If you were to do it yourself, you'd need to provide a Document implementation that makes the JTextComponent think that pieces are being added or removed, then attach click events that tell the document to update itself. A lot of work.
Visually, it may also be better to use JEditorPane, but that's probably more work.

Java StyledDocument: How can I ensure that what the user types never appears already styled?

The user types some text. When they press a button, what they have typed is split up and colour coded:
colors.setCharacterAttributes(characters, tokens[x].length(), formatBlue, true);
Using a set of rules.
When they make an edit between the position as defined by characters and the position characters + tokens[x].length() it comes up in my formatBlue style.
However, I would like it to be in black until the user next presses the 'colour code' button I have.
In short: the desired effect is that everything that is typed should always in black, until it has been phrased and coloured differently by the program.
So far, the best solution I have is to detect when the caret changes position, and then do:
setLogicalStyle(textArea.getCaretPosition(), formatBlack)
Any better suggestions would be appreciated.
the desired effect is that everything that is typed should always in black
You could try using a DocumentFilter. If the text about to be inserted does not contain an attribute then you assign the default black attribute.
Read the section from the Swing tutorial on Implementing a Document Filter for more information.
You can get EditorKit from your JEditorPane. It's StyledEditorKit instance. So you can get InputAtributes from the kit and remove all the attributes. Thus all the typing will use the empty AttributeSet.

Categories

Resources