Listener on text within JLabel - java

I'm building a program in Java, using Swing, that will act as an interactive presentation.
I have paragraphs I need to display (presumably in JLabels) , and within each paragraph are certain words and phrases that need to be formatted differently (have a different color), and I need them to call a method that will display something else when clicked or hovered over.
I know there must be a way to accomplish this...

If you want to apply some style, you can use HTML in your JLabel's content.
If you need some custom behavior and you want to handle it in a different way for some parts of your JLabel, you need your component to be split in a more detailed way.
Create a container(JPanel) and arrange you components within it. From now, your smaller components will be able to listen for events like mouseEntered and mouseClicked and handle them separately, not confusing the whole JLabel component.
In this way, every smart part of text will be a standalone component.

Related

Hyperlink in swing component with character wrap (combination of JEditorPane and JTextArea)

I want to put a hyperlink and some other text in a swing component. This component should be able to fit all of the text in its horizontal space (i.e. no horizontal scrollbars) by wrapping by words, and only by characters when words are too long to fit across the entire component. Parts of this can be accomplished using certain components:
JEditorPanes support hyperlinks. However, they don't break on characters and do weird things when placed in JScrollPanes.
JTextAreas can wrap by words or characters, but do not support hyperlinking.
Is there some combination of these components, or some way I can get one to act like the other?
Some of the other SO questions that I've looked at (for reference):
Wrap long words in JTextPane (Java 7)
JEditorPane inside JScrollPane not resizing as needed
How can I add a clickable URL in a JTextArea?
Note: I am using java 8, and would prefer not to have to download anything, if possible.
Thanks in Advance!
Can you get a JEditorPane to break on characters if you add this to its stylesheet?
word-wrap: break-word;
If you want to elaborate on the "weird things" that might be holding you back from using it, we can try to address those too.

Java gui that allows user to move fields

I'm trying to create a java gui, currently I'm playing around in the netbeans IDE using their gui creator, but I've also been reading a book about Swing and trying to learn it that way as well. Im hoping someone can help me with a problem I'm having. I'm attempting to allow users to have a "pool" of fields they can choose from (for instance a title, a paragraph, a text fiend, buttons, etc) and be able to move items from the pool into another potion of the window which would let them create their own layout. At this point I don't need these fields to DO anything, but I do want the user to be able to move them around and create their own layout. Is there any way to do this?
I think that to do this, you'd need to use a null layout on the container that would hold the movable components, and you'd have to give the components MouseListeners and MouseMotionListeners (conveniently combined into MouseAdapters) that are active when the program is in the set-up state, but then inactive when the program's components have been all set.
You could use the DragLayout from the tips4java website.

java swing- Possible to add JLabel next to application icon/title?

Is the above question possible? The effect I'm trying to achieve is similar to how MS Word displays "Document- Microsoft Word (Technical Preview)" in this picture link: http://img.blogsolute.com/ms-word-2010.png, but with a colored background.
You can set the title of any frame you create by passing the title string to the constructor of the JFrame. You can't, however, add any controls to the 'decoration' portion of the frame - i.e., the title bar.
What you probably can do, however, is create an undecorated frame, and manually add the decoration using customised Border objects. This effectively allows you to put any controls you like around the outside, and the root pane will happily work inside it.
Why do you need JLabel for that? You can use setTitle("") for this purpose

A Swing component to display separate, selectable strings on different lines?

I'm looking for a Swing component that displays several separate strings (probably from a string array or list) on separate lines within a pane or field (scrollable if there's too many to show at once). The user needs to be able to select one of these lines (double-clicking would be ideal) and thereby trigger a listener that does some magic with that line's string. Can anyone point me in the right direction?
Try a JList: http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JList.html. Can be added to a ScrollPane if you want to include scrolling ability.
I suggest you to use a JList inside a vertical JScrollPane.
The user needs to be able to select one of these lines (double-clicking would be ideal) and thereby trigger a listener that does some magic with that line's string
You can use the List Action for this.

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