Swing: Programmatically select a text - java

I have a very simple Swing GUI with just a JTetxtArea. I am trying to programmatically select a part of text using:
textArea.select(startSelection,endSelection);
This work. However as soon as I add some other components to the GUI I do not see selection anymore
frame.getContentPane().add(button);
frame.getContentPane().add(textArea);
textArea.select(startSelection,endSelection);
I suspect that during layouting the gui, some event causes the text to be deselected. Am I right? And could anybody suggest a solution?
My goal is to have a program which displays a text, and allows the user to input start and end selection position, and a selection appears between these two position. Thank you.

Text selection only shows when the text component has focus.
Text components also support "highlighting" by using the getHighlighter().addHighlight() method. In this case the highlighting remains whether the component has focus or not.
If you need more help post your SSCCE that demonstrates the problem.

If what you really want is just a selection, not highlight (which behaves differently), you can use JTextComponent.getCaret().setSelectionVisible(true).

Related

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.

Dynamically add Textbox to Window when checkbox is checked

I'm just figuring out my way around SWT. I have a little problem that i cant seem to solve. I have a check-box in my window. When the check-box is checked, i would like add a multi-line, read-only, text box below it, lets say 200x200. I want the height of the window to increase to accommodate this text-box. When the check-box is unchecked I'd like the opposite to happen.
Could you help me with this? I can't find an example but maybe I'm not using the right keywords. Cheers.
--EDIT
the.duckman'ss answer was very helpful. I've managed to get it working to some extent. I'm adding a multi-line textbox 480px high. How do I automatically resize the window to accommodate the text box? When the user checks the checkbox, the textbox shows up but the height of the window doesn't increase to accommodate the textbox. My code is a little long so I've put it in PasteBin — http://pastebin.com/01RxKeEr
Thanks.
I recommend looking at the SWT Snippets to every beginner - that's probably the best place to go to with SWT questions.
This snippet does exactly what you want.
Edit
Ooops, I ignored the second half of your question, sorry. Simply add this line to your listener:
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));

Java swing how to do when mouse over pop out a small box to show details

I have a JTable displays the event accordingly, I want to do like when mouse over the table cell will pop out a small box show the event details. Something like tooltip how can i do that? is there any component in swing doing that?
Have a read about How to Use Tables: Specifying Tool Tips for Cells.
Use JToolTip and HTML. More info here:
http://java.sun.com/docs/books/tutorial/uiswing/components/html.html

Java focus question

This may be a silly question I don't know.
Is there a way to remove the highlighter to represent focus in a Java GUI?
For example when you click on a button the text will have a slight rectangle around the text.
Thank you
I believe you want to remove the set focusable attribute from your items
using setFocusable(false)
http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
That feature is there for a reason because it gives user feedback about which component currently has focus. But if you really must turn it off then you can use:
button.setFocusPainted( false );
As far as I knew one of the selling points of swing when it first appeared was that any GUI element could be completely customised. This might help you out: Custom Swing Controls

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