I have a JComponent that's painting various shapes on itself. I'm detecting whenever the mouse enters one of these shapes and changing the tooltip accordingly.
The problems I'm having are:
The tooltip doesn't follow the mouse as the user tracks the mouse across the shape. It
stays where it was first set and then only jumps whenever another shape changes the tooltip.
It takes about a second for the tooltip to appear, but I'd like it to appear immediately.
Can someone suggest a way of getting these behaviours without writing a custom tooltip mechanism?
Take a look at the ToolTipManager.
You can register your component with that manager and then adjust a number of settings. Its pretty straight forward to use.
That at least can solve your initialdelay problem.
For your first problem you can overide the createTooltip command from your component to get a hold of the JTooltip instance. and then its easy make the position change whenever you move your mouse(aka follow your mouse) as its a subclass of the JComponent class.
To solve your first issue of where the tooltip doesn't follow the mouse, if you override the getToolTipLocation(MouseEvent e) in JComponent, you can return the point for where you want to the display the tooltip. The MouseEvent will allow you to retrieve the x and y.
Related
I create buttons using jlabels, so I can make a image into sort of a button. The only problem is, is that jlabels are square, therfore if i click somewhere within the square where the picture is not contained, it still runs the jlabel.MouseClickEvent. Is there any fix for this, or another component that i could use?
Ex. If i click this on the corner where the circle is not showing, but the square is still there, then the event fires.
Any fixes/different components to use? Thanks!
If you are just using simple Shapes for the images then you might be able to use the Shape Component found in Playing With Shapes.
The ShapeComponent will only respond to mouse events within the bounds of the Shape.
Otherwise the solution is to override the contains(...) method of your JLabel to check if the mouse point is in the bounds of your image, or in your case if the pixel at that location is not transparent.
So I wrote a small program for a class. I designed it as a JApplet inside an undecorated JFrame, not in a browser. Other than that, it's a simple drawing program of sorts. You click two points to draw the selected shapes, then it calls a repaint. The problem I'm having is that when you draw while the program has been moved to my secondary monitor, the entire JApplet seems to disappear, only displaying the drawn shape. It only disappears after the 2nd point is selected, so I presume it does this on repaint().
My secondary monitor is using the exact same brand and resolution, even color profile.
Any other technical details, I'm using Java 1.7 (Can't recall which update off the top of my head), Windows 8 Enterprise 64x, using Eclipse's Run button to test.
Thanks in advance for any help!
I am indeed calling getGraphics(); in the init() method of the JApplet..
That is the problem. The Graphics object is a transient thing that will be repainted the very next time the JVM thinks there is any need to do so. That might be triggered by:
Changing the size or location of the window.
Covering it with another program and then removing the covering app.
Adding new components or changing values that are displayed.
See Performing Custom Painting for more details on how to do what you are attempting to achieve. OTOH Swing has a JLabel that can show a BufferedImage. You can use the BufferedImage in the way you want. When it is updated, call repaint() on the label to see the effect.
In Java Swing, I want a Window to show up right next to a component--so it will float on top of the GUI. But I need it to always stay right next to that specific component wherever it moves, whether the user moves the window, scrolls the scroll pane that the component is in, resizes, etc. Is there a straightforward way to do this?
I already know how to get it to show up in the right place to begin with. So, for example, if there was some event that fires anytime a component has changed location on the screen, that would work.
Take a look at Componet#getLocationOnScreen
You're going to have to take into account the size of the component and the possibility that the window could be opened outside of the current screen bounds, but lets start with small steps
I think I found it. The java.awt.Component.addHierarchyBoundsListener fires events when scrollbars move, windows move or resize. I think that will do what I need.
In .NET a UserControl may be composed of Controls and it may be part of a Form or part of a bigger UserControl. One can design UserControls with a Visual Designer using the ToolBox Palette.
I have found out that a java BeanForm is the analouge for a .NET UserControl.
I can design a BeanForm with a visual Designer in NetBeans
In my Library I can extend a BeanForm from any other Library and I can design the extended BeanForm with a visual Designer in NetBeans.
In my main Project I can pick all my BeanForms from the Palette onto my frames.
So "BeanForm" is the java pendant for the "UserControl".
In .Net I can easily shape my UserControl to a Fish or to a Star or to whatever I like
by simply setting this.Region = new Region(graphicsPath).
I know that Java is not as easy as C# and I have found Java Samples that partly describe the necessary steps BUT
I did not find a Shaped non-rectangular BeanForm Sample.
Of course extending the MouseAdapter with a new special mouseClicked(MouseEvent e) -- as many Samples do suggest -- isnt enough - this is only a first step.
Did they all forget about the Dragged-Event and the Focus Events and the Mouse-Entered Event and Mouse-Up Event and Mouse-Leave Event and all the other typical JComponent Events ??? - Such Component will SURELY NOT BEHAVE like a real Shaped JComponent should behave.
Please give me a Sample for a OvalComponent-BeanForm that can be taken from the Palette onto my frame and that will behave like a REAL Shaped JComponent
It should cover All the necessary Mouse Events and all the necessary MouseMotion Events and all the Drag-Drop Events and all the Key-Events in case the Oval has the focus or in case the Mouse is over the Oval.
(e.g I should be enabled to use the Oval's Mouse Events and to extend the Oval Component to a MovingOval Component that can be moved around within the frame ...)
If this is too much work for you then, please, outline and describe the steps I would have to implement - like MouseAdapter, KeyAdapter, maybe DragDropAdapter or whatever is necessary.
please, point me what to do!
many thanks in advance.
Every java.awt.Shape has a contains() method that you can use for hit testing in your MouseListener or Mouseadapter.
Okay, no one answered so far.
I want to put it differently.
Let us suppose we have 2 square CirclePanels (extends JPanel), size 100x100 pixels, each shaped to a cicle and these 2 Components show a red cicle and a blue circle.
And let us suppose that the CirclePanels are transparent in the regions outside the colored circles.
And let us furthermore suppose that the hosting frame has Null-Layout - so the red circle partly overlaps the blue circle.
In order to make these circles behave like real Controls we have to fix at least these issues:
1.) a MouseClick outside the circles but very very close to the circle's border must ONLY trigger the frame's MouseListener - it MUST NOT trigger the JPanel'S MouseListeners.
2.) a MouseClick inside the red area must ONLY trigger the red JPanel's MouseListener - it MUST NOT trigger the blue JPanel'S MouseListener and it MUST NOT trigger the frame's MouseListener.
3.) a MouseClick inside the blue area must ONLY trigger the blue JPanel's MouseListener - it MUST NOT trigger the red JPanel'S MouseListener and it MUST NOT trigger the frame's MouseListener.
4.) The necessary Source Code to achieve this all MUST be within the CirclePanel.java file and the frame's java MUST NOT care for these issues. This is because these shaped Controls have to function in ANY frame.
This describes the requirement differently.
Can anybody, please, point me now, how it can be done in java.
yours, Gerald
I have a JPanel that contains a bunch of Swing JComponents, including some JSeparators that may be only one or two pixels wide. I want to let my users drag the items around, but it can be rather difficult to hit a one or two pixel wide line. Is there a way that I can give those JSeparators a wider "target" region for mouse clicks? The only thing I've been able to think of is to have my mouse handler listen for clicks on the JPanel, and if it gets any, run through the list of JSeparators, looking to see if any of them are within a couple of pixels of the mouse click.
Should that work? Is there a better way?
Add a fat EmptyBorder to the component.
If it already has a border, you can set a compound border using the current border then the empty border, or simpler, add the empty border (and listener) to a panel that contains the component. The latter will work better for components such as JButton, which have borders that change according to state and focus.