Selecting nested subexpressions in a Swing GUI - java

As a part of a GUI design that I'm implementing in Java swing, I need the user to be able to select parts or subparts of a tree like structure represented as a string.
For example, if I were to display the following expression, ((a|b)|(c|d))
The user would need to be able to select any of the following
(a|b), (c|d) or the entire thing ((a|b)|(c|d)).
Ideally I'd like them to be able to navigate via the keyboard arrows, moving up and down though the nested subexpressions, and hit enter when they come to the subexpression they want. However if its only possible to do this on mouse click, thats also acceptable.
The main issue that I'm having with this is the nesting component. I could easily make the entire expression selectable, but I don't know how to allow subexpressions to be selectable using SWING components. Based on my research Swing doesn't allow nesting of labels of text areas in the manner that I need so I'm looking for any alternatives.
Any help or suggestions would be much appreciated.

You could use a Highlighter and a DocumentListener on the read-only JTextField suggested by Joop in the comment above, following http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html in the section about JTextFieldDemo. That way you can highlight your (partial) string and listen to what (sub)string is selected, and compare it to the original string to check for matches.

Related

Replacing specific pieces of text with visual representations in a readable+writable JTextPane

I have a text editor in which the user can define a pattern for data. A pattern can contain a reference to another pattern. A simple example would be { name: $pattern2 }, where $pattern2 refers to a different pattern which will later be substituted into this one. However, patterns are identified by a UUID, not by a name, which makes it unwieldy to use. To compensate, users have a bunch of buttons that insert the UUID into the editor for them. However, UUIDs are not nice to look at.
A visually appealing way might be possible using a JTextPane (or JEditorPane) to replaces the UUID with a small square containing the referenced pattern's name. The first image below shows what the actual contents of the Document are, while the second image shows how I imagine it is displayed to the user, given two other patterns with the names GoodName and Date.
I have tried using the javax.swing.text.Style object obtained from jTextPane.addStyle("", null), I tried with a javax.swing.text.StyleContext, and I looked at extending javax.swing.text.DefaultStyledDocument, but those mostly seem to concern themselves with changing the way the font is rendered. I don't really see a way to render specific text as a non-text shape. Furthermore, I don't see a way of making the reference "atomic", in the sense that the user cannot select and remove half of the UUID, which would reveal the underlying data which I'm trying to keep hidden.
Is anyone aware of a component that supports behaviour like this? Am I overlooking a Swing feature here? Swing has loads of documentation but it's hard to find what you're looking for if you don't know what it's called.
Perhaps this is a case where a JLayer can be successfully applied.
Instead of replacing the UUIDs with small rectangles, an equally large rectangle containing the name could be drawn over each UUID.

Wicket: AutoCompleteTextField with "favourites"

I need to build an AutoCompleteTextField-like component where the user can mark some options as favourites (when he starts to write, some options show up and each option has a checkbox to mark it as favourite). The user can then check a checkbox outside the AutoCompleteTextField to choose whether only the favourites will be shown or on the contrary all the values no matter if they are favourites or not, will be all shown.
I have read Using panels instead of String in Autocompletetextfield and i think it could be done using IAutoCompleteRenderer...
Any ideas?
I would go with the solution provided by Robert in https://stackoverflow.com/a/15484348/461499.
Why?
If you have complete control over how the choices are rendered (by using plain Wicket Components instead of javscript), you can built a very rich component. Although I think it will take some extra javascript effort to make the choices-panel look and feel correct, it should be worth the investment.

Uneditable ParagraphViews in Java

I would like to have a document editor where certain document lines are uneditable, unclickable, etc.
I currently am using a JTextPane for my editor and extensions of DefaultStyledDocument and LeafElement, StyledEditorKit, and ParagraphView, to parse the document text into the appropriate elements and assign them to the appropriate extensions of ParagraphView. Everything is peachy up to this point.
So how to take the next step? How to designate and enforce certain extended ParagraphViews are "read-only"?
My current approach is to override getNextVisualPositionFrom in the Leaf and Section ParagraphViews to prevent arrow keys from moving the cursor into a restricted ParagraphView, but preventing the mouse from clicking inside a restricted ParagraphView has to be handled separately.
Is there a simpler, more comprehensive approach to this?
Thanks!
After a bit more research, a different approach that seems to fit more elegantly is to use a ComponentView for the uneditable views, instead of ParagraphView.
I can then use something as simple as a JTextArea in the ComponentView in its createComponent() method and setEditable to false.
It automatically ignores mouse clicks in that view. I still have to include my code for nicely skipping the caret over the view when the user presses up, down, left, right. Otherwise, the caret will invisibly move through underlying model text. I will also have to prevent backspacing through the protected view. Even though the view is uneditable, the model data underneath can still be affected by the user.

Homework assignment using Java/Swing

I've got an assignment in a class about user interfaces and usability testing. I have to do something that I'm sure I can figure out how to do programatically, but I have no experience with swing so I have no idea what components to use to do the job. My background is in C# so I'm fumbling at times trying to find the right component to use in NetBeans.
Based on the description below, can anybody recommend what kind of text field will do the job? I was thinking jFormattedTextField (based on the name) but I can't seem to figure it out.
Thanks
Submit a Java program that will run on the Linux installation in the general lab (EN-2036).
Provide the following functionality:
load the output of the last command (from a file) into a text viewing area that
allows the user to browse the data
bold all login names only (not the whole line)
bold all occurrences of a user-specified login name
bold all occurrences of a user-specified set of login names
Use JEditorPane or JTextpane, these allow you to add style to text inside them
Tutorial Link

Best way to implement a selection box from a large number of entries

I have a large set of data from which the user has to select one. I'm thinking of a way to implement it (of course, in a GUI). I have a few ideas. But just thought of posting here as there may be better alternatives..
Say, user has to select a name from a large set of user base. If I simply put a text field for user to enter the name, then there can be issues like entering same name in different formats, misspelling etc...
I see two options here
Using a combo box
Using a list (Actually i'm thinking of something like a tool tip. As I cant show the whole list always due to space issues)
But combo box won't be much user friendly i guess. As the user will have to scroll around the whole list to select an entry. If the number of entries are too large, this will be
Which means, now I'm left only one option. A popping up list, which will change the content according the text user is entering in the text field. So he can type first few letters and the list will show all the entries starting from the entered text. Got my point, right?
Are there any other better to achieve this kind of need?
If I'm going to implement above, what will be the best way to follow. I'm thinking of extending the JTextField to add required functionality. Well, I'll put some method to set the popup list entries. And I'll add some actionListner to watch the text field, and control the popup list accordingly...
Autocomplete is what you are probably looking for. Google for "java swing jcombobox autocomplete" and limit results for the last couple of years to get relevant results. There will be a lot of examples and ideas on how to implement this with custom code.
I believe there is also some custom libraries like "swingx" that provide at least partial or full implementations to save time.
http://swingx.java.net/
They have released code as recently as the beginning of this years so it appears active and might have what you need.
You could take a look at SwingLab's autocomplete feature, it allows you to attach it to a JCombBox, JList or JTextComponent
use AutoComplete JComboBox/JTextField
based on Standard Java Classes
no issue with larger sets of data
no issue with Focus, BackSpace Key, Caret
for better performance to required sort the array before use
simple workaround for setStrict(true/false), restrict input to array

Categories