Generate a clickable event from a text area - java

I want to generate a clickable event from text area which contains a list of names of people. When we click on a certain name it must make a call to a method (which in turn is going to display the details of that person from a XML file).
Is this possible with text area? I have heard about JTextPane, but I need some other option.

Yes it's possible. Use viewToModel() method to get offset for the clicked point. Then use Utilities to getWordStart/getWordEnd methods to get the clicked name. Then just pass the name into desired function.

Related

Getting the text of all the Selected Checkbox in Java

So I'm doing a project that requires a lot of checkboxes. And all those selected checkboxes will go to the texture or something trigger by a button. I already browse here and I came up with this.
The code actually is almost what I'm looking for. But the problem is the way it displays.
What I want is to remove those [ , ] because I want to be the one who controls the way the text is display.
Well, passing the checkboxes List to the showMessageDialog method is implicitly calling List#toString(). The implementation of this method in class ArrayList is to print a comma-separated list of the values, just as you see it. If you want to display it in another way, you'd have to iterate over the elements of your list and build a string according to your likings.

Text field validation in JavaFX using marks

I'm using JavaFX for my application's GUI. I want to implement a validation method for all the textfields inside the sign up window. I want to check them all and than, whether they are true or false, I want to use a mark to show the user what field is incorrect. I also want to be able to show a small message box when I hover the mouse pointer over those marks.
Simple way is to create HBox , put TextField,Label in there ,label will be Bound on the textProperty/ or do it with listener
txtField.textProperty().addListener((v, oldValue, newValue) -> {
//code here if valid, set label visible false, else set label visible true(red image crossed or whatever)
});
, when value changes it will check if that is ACCEPTABLE/FAILED state , for instance empty box.States will be changed on property change , use array of your hboxes to check if they are valid or invalid at the time , you can check this based on visibility of Label or internal boolean state value.
For the hover over part , use Tooltip on label.
If you want to go lazyer way , take a look at controlsfx validation it will take care of graphics for you.And its already embedded in its component.Just create validation process
Good beginner reference might be newboston videos so you understand concept.In javafx you gonna use property binding ,listeners etc often , get familiar with them as you cant avoid it.
https://www.youtube.com/watch?v=s8GomyEOA8w
https://www.youtube.com/watch?v=6Zi2L0kHSx4
Since you've given no code I can't give you answer that involves actual coding, because I've got no way of knowing whether or not what I give you will be viable or conflict etc..
In regards to the validation that depends entirely on how your accessing a username/password to compare it to what the user has entered and with out knowing how you're thinking of doing it I cannot give you a good answer.
There are quite a few options to display your red x, you could draw it internally etc..
But the easiest is probably going to be creating an image and importing it to your project, you can set a label next to your JTextField and have the picture set to that lable. Once the user inputs the username/password if either or both are incorrect you could have a method that would set the label to be visible.
The message box is as simple as a tooltip that you could also place on the label which would tell the user that the information they entered is wrong.

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

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.

How can I create an AutoComplete popup in a JTextPane in 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

Categories

Resources