Copy tooltip to clipboard - java

I have a Java program with icons, tooltips...
What I need is that every time a tooltip is showed, the containing text should be copied to the windows clipboard.
Any solution should be a general single solution for all the tooltips. I cannot change the properties of each one of them (there are thousands...)
Thank you!

The way I see it, if you are indeed using Swing, you might need to subclass your components and override createToolTip():
class MyJButton extends JButton {
#Override
public JToolTip createToolTip() {
return new MyJTooltip();
}
}
And:
class MyJTooltip extends JToolTip {
#Override
public void setVisible(boolean aFlag) {
super.setVisible(aFlag);
//copy your text to clipboard here
}
}
Not very practical, not very pretty, but I can't think of another way right now: AFAIK, there is no way to subclass TooltipManager and make it be used as the default.
If you are using JavaFX, things get much easier:
new Button().setTooltip(new MyJavaFxTooltip());

Related

Can two exported classes in different packages interact without making their members public?

I stumbled across this issue while working on a library and have been trying to find a solution for hours now. I'm not sure if this is even possible or not. I have a module, com.gui, which contains a package called com.gui.components, com.gui.constraints and com.gui.animation. I want to implement text-based components and set up a package called com.gui.text.
I have a Font class inside of that package (com.gui.text) which should be public so the user can pass it into one of the text components. However, I'm struggling with how I transfer data like the texture id over to the text component without making the variable public (or implement a public getter). I messed around with not exporting the text package and extending the Font class inside of the text component class but this seemed like a suboptimal solution and I don't really like the feel of it.
Here is the hierarchy of my project visually:
src/com.gui
--component -> exported
----UITextComponent
--text
----mesh
------Texture
----font -> exported
------Font // contains a Texture object which should stay invisible to the user
--XXX // other packages
Am I missing anything obvious here or is this impossible to do currently?
If you want to go crazy you can do the following (this is not recommended)
in your Font class have a method like this:
public class Font {
private String textureId;
...
public void setTextureId(Texture texture) throws Exception {
if (texture == null) return;
Field field = texture.getClass().getDeclaredField("textureId");
if (field == null) return;
field.setAccessible(true);
textureId = String.valueof(texture.get(font));
}
}
Again this is not the recommended way but it is a way to get what you want with the layout you have. Also you'd have to add some more validation checks.

Is it possible to add a message bar/ toolbar at the bottom of a titleareadialog?

With my TitleAreaDialog is it possible to add a area or a bar across the bottom, below the buttons. That a message can be displayed to the users, when a operation is taking place.
Here is a example of what I am referring to
AFAIK, this is not possible for JFace Dialogs. Depending on what exactly you are doing, you might want to have a look at JFace ApplicationWindow. This class has a method addStatusLine(). You would have to override the following method:
#Override
protected StatusLineManager createStatusLineManager() {
StatusLineManager statusLineManager = new StatusLineManager();
statusLineManager.setMessage(null, "YOUR_MESSAGE");
return statusLineManager;
}
You can change the text with:
getStatusLineManager().setMessage("YOUR_NEW_MESSAGE");
Here is an excellent overview of the ApplicationWindow class.

Add a custom widget in a CoolBar?

Is it possible in a standalone SWT/JFace application to add a custom widget in a CoolBarManager (a text box for exemple) ?
I look for IContributionItem but I didn't find useful examples.
HelpSearchContributionItem seems to work only with a RCP application (it uses objects from RCP : IWorkbenchWindow for example).
Thanks in advance
You should subclass ControlContribution and implement createControl() method.
Then add a ToolBarManager/ToolBarManagerContributionItem to the coolbar, and your subclass of ControlContribution to the ToolBarManager.
Although it is not recommended, you can subclass the ToolItem class and provide your own implementation. To do this you also have to override the checkSubclass method.
public class MyToolItem extends ToolItem {
#Override
protected void checkSubClass(){
// leave it empty
}
}

Why doesn't my Wicket Panel rerender after changing the default model?

When the page with the MessagePanel first renders, the message and the approve link render perfectly. When I click the approve link, all the business logic works as desired, the getNextMessage() method returns the appropriate object, but the message panel does not update on the page in the browser. That is, the message body Label does not update.
JPAEntityModel extends LoadableDetachableModel.
What am I missing? And how do I fix it?
public class MessagePanel(String id, IModel<Message> messageModel) extends Panel {
super(id, messageModel);
add(new Label("messageText", new PropertyModel<Message>(getModelObject(), Message.BODY_FIELD)));
add(new IndicatingAjaxFallbackLink<User>("approveLink", new JPAEntityModel<User> (getActiveUser())) {
#Override
public void onClick(AjaxRequestTarget target) {
Message nextMessage = getNextMessage();
MessagePanel.this.setDefaultModel(new JPAEntityModel<Message>(nextMessage));
target.add(MessagePanel.this);
}
});
setOutputMarkupId(true);
}
It is because you're not using the model properly.
This line takes the value of the panel's model object, as it is set during construction, and uses it to create the component model.
add(new Label("messageText", new PropertyModel<Message>(getModelObject(), Message.BODY_FIELD)));
To make matters worse, when you click the link, the panel is given a new model:
MessagePanel.this.setDefaultModel(new JPAEntityModel<Message>(nextMessage));
But this obviously doesn't affect the model of the label, as it is already set to refer to the original value.
So there are two things you need to change to make it work. First off, your label model should use your panel model directly:
new Model<Message>() {
#Override
public Message getObject() {
return MessagePanel.this.getModelObject().getMessage(); //or something similar
}
}
(Note: the code above isn't necessarily the best solution, but it is a working solution that demonstrates how models can be used dynamically.)
And ideally you shouldn't replace the model when you click the link, just change the model object. If you need a custom model class (JPAEntityModel), you shouldn't be accepting a pre-constructed model in the panel constructor anyway, just the first message object. The reason being the current implementation doesn't enforce the use of JPAEntityModel from the start, only after the first click of the link.
Can you try calling MessagePanel.this.modelChanged() before adding it to the target?
You must use call setOutputMarkupId(true) within you MessagePanel. The panel needs to have a markup identifier to be able to update the markup DOM in the browser.

Java JFrame keeps loading

Hi this is a stange one for me so I hope you can help :)
I have a method..
public void WMPEGUI(String info)
it loads a JFrame...
final JFrame mainFrame = new JFrame("JFrame");
the method is set up to recieve strings into it to later be wrote out to a text area also created within the method. When I run the program everytime the methods recieves a string it opens a new JFrame I have tried to solve it using...
mainFrame.setAlwaysOnTop(true);
mainFrame.setLocationByPlatform(true);
But this hasn't solved it, hence asking you kind people. If anyone knows why as I can't find anything on it :/
Many thansk in advance
Well I think your problem is that according to you, your method loads a JFrame. You need to create the JFrame outside that method, somewhere else within the same class, and then refer to it (or to one of it's containers where you will display that string).
Something like this:
public class YourClass {
//Class variables
...
JFrame mainFrame = new JFrame("JFrame");
...
public void WMPEGUI(String info) {
...
mainFrame.someMethod(...) //or a get for one of its containers
...
}
}

Categories

Resources