I am using SWT's StyledText and I am wondering how I would detect if the user hovers over a word for more then 3 seconds. An example of what I mean is how Eclipse has a popup that shows, when you hover over a text.
You can use a 'mouse track listener' to listen for mouse hover events. However you can't set the hover time for this:
control.addMouseTrackListener(MouseTrackListener)
MouseTrackListener has three events - mouseEnter, mouseExit and mouseHover.
While you can implementet text hover yourself, with a MouseTrackListener like suggested by Greg, you can also use the JFace TextViewer.
The TextViewer provides a higher-level abstraction for text hover. With its setTextHover() methods you can set a callback, that is informed when a text hover for a certain region should be shown.
This is also the method that is used by Eclipse text editors.
I believe those are tooltips that appear when you hover over a text for e.g. in Java editor. See here for tooltip doc and here for examples on how real production code uses eclipse tooltips.
Related
I want to change text of button in org.eclipse.jface.wizard.Wizard to arabic in button bar?
I mean I wanna change label "Next" button to "بعدی".
The language of the Wizard's labels is handled by JFace and uses your OS's region settings. That means if you're machine is set to "English (UK)" you will see the labels in English, if it's set to "Germany" you'll see them in German, etc.
There are related questions here:
JFace dialog button translation
How to set custom text on buttons in JFace Wizard (Java)
Update:
If you're using a WizardDialog you can override the method createButtonsForButtonBar(Composite parent) like it's done here.
I have an Eclipse RCP application that has a widget that uses the rich text editor from org.eclipse.epf.richtext.RichTextEditor. So all the code is open to me to alter to how I see fit. I have found that the rte uses a browser to display the text.
My problem is that I want the rte to wrap the text and not put a horizontal scroll bar. So I was hoping to see if anyone has used this rte and figured out how to do this.
I think that in every SWT widget's constructor you can pass a style. So for example if you want vertical scroll but no horizontal you can do this:
new MyWidget(parent, SWT.V_SCROLL);
This way the widget won't scroll horizontally. the RichTextEditor itself does not have anything about text wrapping in its API so I guess that you can't alter that directly.
A little more about SWT styles: SWT style bits
Examine the HTML which is displayed in the SWT browser widget (call getText()). That should give you an idea how it's organized. Find the HTML element which wraps the editor, set it's width to 100% and make sure you do the same for the body and html elements to have the widget's width propagate to the editor element.
That should already fix the issue unless the text in the editor contains elements which are wider than the widget.
I am making a UI with Swing, and I want the buttons I am using for my custom dialogs to have the same style as the ones in standard dialogs.
For instance, in the attached image I have a custom dialog and the standard file select dialog. I want the 'OK' and 'Cancel' buttons from the file select dialog to be used for the equivalent buttons in my custom dialog.
I want my application to use the default system look and feel of whatever OS it is running on, so I don't want to try to manually re-create these standard buttons. Using a more rigid Swing class that automatically provides these buttons wouldn't work either, as I'd like to also use them in other, more exotic places in my UI.
Is there an easy solution to this problem?
Sorry to be the bearer of bad news, but it look like there is no standard way to do this cross-platform. The behaviours, mnemonics and icons on default buttons are handled in very specific ways by each look-and-feel.
Here is a SO question that answers the question on how to set the default OK and Cancel buttons on a dialog (the default button is set using getRootPane().setDefaultButton(...) and the Cancel button needs a custom keyboard listener. If you're very lucky, setting the default button might add an icon to it, depending on how the UI is coded.
This forum thread addresses the issue of getting icon resources from the UIManager. Each LaF has a set of UI defaults such as colors, text, borders and icons. There are a number of default icons which are found across all LaFs, but for non-standard icons, such as ones on buttons, there are no guarantees. However, if you tell me which LaF you are using in the screenshot you provided, I can look up the resource keys used by its custom UI classes (or you can find it yourself if you have the source). You could then write a helper method which looks for the icons via these keys, and adds them to the buttons if they are found.
JButton.setUI(ButtonUI) sets the UI for just one JButton. Use that in conjunction with a factory:
public static JButton createStyledButton(String text) {
JButton button = new JButton(text);
button.setUI(STYLE_UI);
return button;
}
I have a JTextPane sitting in a JFrame, with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
I want to give the JTextPane a "Word-like" popup behavior. By that I mean, if you right click outside of your current text selection, the caret will reposition to where you right clicked, with menu options that affect a text selection (such as cut, copy, or bold) disabled. If you right click within your current text selection, the popup will appear with options that effect text selection enabled, the text selection will persist, and the caret will not move.
The problem is I cannot seem to find where I can put the code that handles the selection change. I tried:
Using the "PopupMenuWillBecomeVisible" event which is triggered before a popup becomes visible. The event passed into this method does not contain any mouse event information so there is no way for me to use viewtomodel to find out how to modify the selection. I could use MouseInfo but that seems dubious at best.
Using MousePressed/MouseReleased events in the JTextPane or JFrame. Apparently, neither of these events are invoked when a popup menu is triggered. In fact, I still can't determine what the parent component of my popup menu is. (I did read that in windows "MouseReleased" is the popup trigger, while in other systems "MousePressed" is the trigger. I tried both and neither worked).
So, I guess the problem is that I can't seem to find a place to put code where it would be called before the popup menu becomes visible, but has awareness of the mouseEvent that triggered the popup menu. I must be missing something here.
with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
You can use the older approach of displaying the popup based on your own custom MouseListener.
See the section from the Swing tutorial on Bringing Up a Popup Menu. Now you have access to the MouseEvent so you can convert that point to a point in the Document so you know where the click was made, on selected or unselected text.
I am creating an options dialog using JOptionPane.showOptionDialog(...)
When I click one of the buttons added to this dialog, I need a label to apear underneath on the dialog (and this label should be scrollable if necessary). I have written event handlers for the buttons, but I am not sure how to get this label to appear on the dialog.
Any help would be great.
Update: I realized that it would be ok if I somehow called JOptionPane.showOptionDialog(...) with an initial message, and then when one of the buttons was clicked I would change the message. Is this possible?
JOptionPane static methods are only shortcuts to easily create a dialog with option buttons and a fixed message. If you check the source from it, you will see that all is wrapped in this purpose. It's only a convenience class over a frequent use case of dialogs.
The suggestion from comment is correct, if you want more than this, you will have to create your own JDialog, as it will be easier than trying to change something from this generated dialog.
Edit: You can create your own JDialog yourself, using layout managers. A more simple way, suggested as well in the previous link, is to use a GUI builder, like the one included in Netbeans.