How do I get tooltips to activate on a JLayer-covered JComponent? - java

I have a Java class that extends JTextField and covers it with a JLayer (new Java 1.7 feature) in order to display custom graphical effects in certain conditions. Without the JLayer it is easy enough to add a tooltip using setToolTipText(String). But with the JLayer in place, nothing happens when I hold my mouse over the box. Perhaps it is blocking the mouse-over event? I also tried calling setToolTipText() on the JLayer object itself but it didn't make any difference. So how can I get the tooltip to work?

Did you try overriding getToolTipText and return getView().getToolTipText()? Doubt it will help though.

Related

Swing JTextPane in javafx

I'm looking for a rich textarea in javafx. I have tried the RichTextFX component by Tomas Mikula but it is too buggy for my needs.
I was thinking whether it would be possible to plug in a swing JTextPane using the SwingNode class.
So far it actually works as in, I can see the text in the JTextPane and the JScrollPane around it even works. A mouselistener on the JTextPane also seems to be triggered correctly for mouse events but other than that...nothing works.
There is no cursor indicating the current position, no way to "click" with the mouse to change the position, select text,...
Key events simply don't arrive (using a keylistener)
Is it possible to use a JTextPane in javafx and if so, what am I missing?
Currently I only know a workaround for this problem.
node.setOnMouseReleased(event -> node.requestFocus()); (node is your SwingNode).
I used mouseReleased to mimic the same behavior as the standard FX nodes.

Java keyListener with multiple JPanel

I'm having a problem with Java KeyListener when adding adding another JPanel with 5 JLabels, I've searched around this website, and most solutions to this problem involve switching from KeyListener to KeyBindings. This wont work for me because I need to know exactly when a key is pressed, released and held down. To my knowledge, KeyBindings does not provide all those.
I've tried to use
this.requestFocus();
after creating the new JPanel, but it didn't work, however when I use the same line inside the paintComponent(), it works. Which brings me to my questions: How does this reflect on performance? My paintComponent() is called about 60 times/sec. Is there a way to call it once and still have this working? I see that requestDefaultFocus() from the type JComponent is deprecated...
I've also tried adding same KeyListener to the second JPanel, but that didn't help. Im guessing one of the JLabels is the one that gets focus?
This wont work for me because I need to know exactly when a key is pressed, released and held down. To my knowledge, KeyBindings does not provide all those.
Yes it does. You have an Action for "pressed" and "released". There is no such Action as "held down" (even for a KeyListener), you just get multiple events generated.
this.requestFocus();
That is not the proper method to use for requestion focus on a component. Read the API for that method and it will tell you the proper method to use.
however when I use the same line inside the paintComponent(), it works.
This is because you can't request focus on a component until the frame has been realized, which means you've invoked pack() or setVisible() on the frame.
Is there a way to call it once and still have this working?
See the RequestFocusListener class in Dialog Focus.
The proper solution is to use Key Bindings so you don't need to use these work arounds.

How to resize controls at runtime in java

Is there a way to resize a control, a JTextfield for example, at runtime in java? I want my textfield to have the resize cursors (just like when you point your cursor on the corner of a window) and will be able to resize on runtime. Ive read on the internet that vb6 and C# have those capabilities, is there anything for java? A sample code or a link to a good tutorial will be very much appreciated. Thank you.
It sounds like you are trying to implement a component editor, such as the GUI editors available in popular programing IDEs. The essential feature is a handle, a graphical object that can be selected and dragged to change the geometry. GraphPanel is a simple example of an object drawing program that illustrates many of the required techniques.
That depends on the Layout of the JTextField's container. A good tutorial is available at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
For a quick and cheap solution you could use a JSplitPane component, with the JTextField to be resized in the left side, and an empty JPanel in the right side. By default a JSplitPane is decorated with a border and a divider, but you can remove these by setting an empty border.

Line graphical component

I want to draw a line between different components in a JPanel, but the line should be a component, ie, it can be clicked, highlighted, selected and deleted instead of just painting a line in a panel, is there anything like this or I must implement it by myself. And if I must implement it, how?
You could use a JSeparator. But you'll have to implement the click, highlighting, selection and deletion yourself. A JSeparator is just use to... separate sections in a panel.
If you mean that all these operations should be available when designing your GUI in a wysiwyg editor like NetBeans Matisse, then JSeparator is just what you need.
I tried to use prepared things like JSeparator, But I found the best way by myself and I implement it. I used a JLayeredPane for my container. I add my own JPanel behind the all layers and override its paint() method. in paint() method I used Java2D to draw a curve between Components on higher layers in JLayeredPane. You can see the result in below.

Java tool tip not displaying

The program I am writing has a Swing GUI and several of the components have Tooltips. These are all on JButton, JCheckBox and JRadioButton components, they are set using the setToolTipText method and all work perfectly. I have just tried to add one to a custom component that extends JPanel using the same method and no tool tip appears.
The JPanel contains 2 JLabel components and a JSlider. I tried to override the setToolTipText method and then use it to call setToolTipText on the slider. This didn't work either.
public void setToolTipText(String text) {
super.setToolTipText(text);
slider.setToolTipText(text);
}
Am I doing something wrong? or can't you have a tooltip on a JSlider or JPanel. I would have thought that it should work on anything that extends JComponent.
The control is disabled when the program starts although even after it is enabled there is still no tool tip if that is relevant. It doesn't really need to show it until the control is enabled anyway.
Thanks.
try settings the tooltip to the JLabels as well. they may be obstructing the JPanel.
Try making the following call:
ToolTipManager.sharedInstance().registerComponent(customComponent);

Categories