Resize java swing component from UI - java

I need to make resizable java swing component (JPanel), so when I click on the border of that component and drag with cursor to somewhere else, the component will change size by mouse position. Has this any easy solution?

Attach a MouseListener to the component, record mouse position at start of dragging in mouse pressed method after checking whether the mouse is pressed on what you have defined as border, then resize the component in mouse released method calculating the size difference by subtracting mouse positions at start and end of dragging.
If you need / want continuous resizing while you drag, use a MouseInputListener instead, record the size of the component too in the mouse pressed method and do the resizing in the mouse moved method.
If you also want to have a visible border, you can maybe add that border to the component and add the mouse listener to the border, so that you know you are on the border when you get the pressed event.

Related

How to detect mouse moving outside Java Popup?

I'm creating the following UI in Java.
When a user hovers their mouse over a particular area on the screen, a popup appears that contains a bunch of buttons and controls.
PopupFactory factory = PopupFactory.getSharedInstance();
_hoverPanel = factory.getPopup( parent, panel, x, y );
_hoverPanel.show();
I want this panel to remain visible on the screen while the user interacts with the panel's components, but as soon as the users mouse exists the popup bounds I want the popup to hide.
I tried adding a mouselistener to the panel inside the popup but I noticed that mouseEntered only fire when I enter the panel (and not when I enter components found inside the panel) and mouseExited only fires when I exit the panel.
The problem here is that I can make the popup appear, but as soon as I move my mouse inside the panel, and then over top of a component inside the panel, mouseExited fires for the panel, and the popup hides :(
I can also move my mouse quickly inside the panel over top of a component, and mouseEntered never fires for the panel :(
How can I detect when the mouse goes inside and outside my popup panel?
I had a similar problem (catching all mouse events while the mouse hovers over my dialog) and I solved it by wrapping the root pane of the dialog in a JLayer (javax.swing.JLayer) as shown in http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html#events

How to properly change pane content while dragging a jscrollbar handle

I have a pane that contains image content which changes during scrolling. The content is properly updated via a scrollwheel event because I implemented a wheel listener which repaints the image before setting the new scroll value.
However, when the user drags the scrollbar handle with the mouse, the image content was not being updated during the manual drag-scroll. So I implemented a timer which grabs the current scroll value and repaints the content given the new scroll position.
This solution however (despite 10 millisecond adjustments) results in a jumpy scroll experience. The image moves (without the necessary image adjustments) and then gets corrected after-the-fact every 10 milliseconds.
I had originally tried an adjustmentlistener, but it only gets the event after the handle is released. How can I live-update the pane content during a jscrollbar handle drag BEFORE the scrollbar machinery starts to simply move my content as if it was a static image? Can I somehow give the scrollbar machinery a clue that content has changed or something every time it tries to redraw the content? Or can I disable the scrollbar's ability to move the image and just rely on my timer to do it?
I would recommend that you add a ChangeListener to the JScrollBar's model, a BounderedRangeModel, and then based on the value of the model as well as its maximum and minimum, change your image. If you're swapping images, the easiest way to do this is by swapping a JLabel's ImageIcon.

How to "Unclick" a JButton without releasing the left mouse button?

I have set up a mouse dragged listener. I trying to set it up where you can click one button then drag your mouse over others to click the other ones. The problem I am having is when you click the first button it turns grey like its waiting for you to release the mouse button. When you move your mouse off the button (still holding the left mouse button) it returns back to its normal color but you cant highlight anything until you let go. Is there anyway to simulated letting the mouse go and "unclicking" the button so you can highlight other things?
What you observe is the typical behavior of the ButtonModel used by Swing buttons. A complete example is examined here, but note how the effect depends on the chosen Look & Feel's ButtonUI delegate.
To get the effect you want, you would have to create buttons using your own variation of BasicButtonUI and a custom ButtonModel that uses isRollover() to add buttons to your program's notion of a selection model.
As an alternative, consider JList, which contains a ListSelectionModel that allows MULTIPLE_INTERVAL_SELECTION. A compete example is shown here.

How do I lock the mouse inside a JFrame

I would like to lock the mouse inside a JFrame. That is, the mouse can not leave the contents of the JFrame (unless the user hits escape, alt-tab, or the window otherwise looses focus). Ideas?
Thanks!
I'm not sure if there's a more automatic way of doing that, but you could use the Robot class to set the mouse position. So in the event handler for when the JFrame gains focus you can start watching the mouse move event, and when the mouse moves just make sure it stays within the JFrame. If it leaves the JFrame you can use the Robot class to set the mouse's position to go back.
Then when the window loses focus, you can unregister from the mouse move event.
The Robot class is ideal for this type of thing, but I would suggest another approach.
Perhaps making the game full screen (maximizing the window pane) would achieve what you want instead. The mouse would be unable to exit the window and no ugly Robot-esque hack needs to be used to force the user to stay within the borders.
Another workaround I just thought of - lock the cursor to the centre of the Frame, and make it invisible.Then render a software cursor where the real cursor should be.
You can then lock the cursor to whatever area you want.
Here's a sneaky one could work if you don't use mouse button 2 in your game.
Use a Robot to press down BUTTON2.
The idea is so the mouse gets dragged, not moved. Whenever you get a mouse moved event, it's because the user has released button2, so press it down again.
Whenever you get a mouse dragged event, if the mouse is outside the window, put it back in.

Detect Mouse Move Event On JPanel

I have a JPanel, which I would like to detect the following events
(1) When the mouse move in
(2) When the mouse move out
The (1) is quick easy. (2) is a bit tricky. Currently, I have to register event at all the components around JPanel. If the neighbor around JPanel detected a mouse move in event, this also means that JPanel is having (2) situation. However, this is a rather dirty away, as I add in new components in the future, this dirty workaround will break.
Another method is to have a timer to monitor the JPanel. If the mouse position is not within JPanel within x seconds, I can consider JPanel is having mouse move out event.
However, this seem a dirty way to me too, as having a separate timer to perform such common task is overkill.
Is there any better way, which Java platform may provide?
Have your class implement MouseListener and add it as a mouse listener on the outermost panel. You should get a mouse-entered event when the mouse moves over the panel, and mouse-exited when it leaves; regardless of whatever components the panel contains.
From the JavaDoc:
void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.
void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.

Categories