

- TRACK MOUSE COORDINATES OUTSIDE OF WINDOW JAVA HOW TO
- TRACK MOUSE COORDINATES OUTSIDE OF WINDOW JAVA UPDATE
- TRACK MOUSE COORDINATES OUTSIDE OF WINDOW JAVA CODE
tBackground(new Color(255,255,255, 0)) ĬtBorder(BorderFactory.createEtchedBorder()) ītn.setBackground(new Color(0,80,130, 40)) ītn.setFont(new Font("SansSerif", Font.BOLD, 12)) Public Container container public Component comp Private int xPos, yPos, prevX, prevY = 0 Implements ActionListener, MouseMotionListener, MouseListener

TRACK MOUSE COORDINATES OUTSIDE OF WINDOW JAVA CODE
Start a new thread and post the relevant code or a small example that demonstrates the problem. Therefor I didn't consider creating a new thread. This is the only problem I need to resolve in order to go further into my development. This is called every time the panel is resized. Which uses absolute positioning in order to arrange every container and component inside the main panel. If useful to know: I have a method called update()

The container from the other class is still the same size, I have also added a container, from another class to the panel,Īnd when it updates, because this panel is resized, I just experience this flicker, even when the JPanel is double-buffered, YPos = panel.getY() + evt.getYOnScreen() - prevY XPos = panel.getX() + evt.getXOnScreen() - prevX PrevY = void mouseDragged(MouseEvent evt)

TRACK MOUSE COORDINATES OUTSIDE OF WINDOW JAVA UPDATE
You just need to keep track of the previous coordinates and update by the delta values int prevX=0 The version is clearly shorter and more useful tough I am stuck on this small little problem. (Panel jumps to the cursor when mouseDragged event occurs). However, I tried it and an error(No exception, but doesn't function the way it should) occur while dragging the JPanel (it is flickering): probably due to getX() and getY()ĭo you know any way in order to get X and Y coordinates from the JPanel while moving the JPanel in the contentPane, to get the position inside the JPanel where the dragging occurs?, instead of getting the JPanel to the cursor when dragged. Thanks for the simpler version of moving JPanels. You also need to set the content pane's layout to null if you want to set the location manually. You don't need a timer at all and you don't need to keep adding the component to the container. You can simply setLocation() on the panel and repaint() in your mouse listener.
TRACK MOUSE COORDINATES OUTSIDE OF WINDOW JAVA HOW TO
Your example does show how to get those coordinates, so I guess some might find it helpful in that regard, but it's overly complex for just moving a panel. The original question had nothing to do with moving panels. This method gets refreshed constantly by the timer while the JPanel is dragged. The method addComponent() has 6 parameters which will position the JPanel. NOTE: This won't match if panel is resized. 100 is not necessary, it is set to fit the panels current size. Means: xPos will be equal to X on screen(frame) - the x coordinate on the panel - 100 "yPos = evt.getYOnScreen() - getY() - 40 " The statements: "xPos = evt.getXOnScreen() - getX() - 100 " or We need to add MouseMotionListener in order to get the X and Y coordinates of the cursor on the screen. The timer will only start if Panel is dragged. YPos = evt.getYOnScreen() - getY() - void mouseMoved(MouseEvent evt) XPos = evt.getXOnScreen() - getX() - 100 Public void actionPerformed(ActionEvent e)ĪddComponent(contentPane, panel, xPos, yPos, 200, 200) Public void addComponent(Container container, SetTitle("Java Tutorial - Moving JPanels") Implements ActionListener, MouseListener, MouseMotionListenerĬontentPane = (JPanel) this.getContentPane() ĪddComponent(contentPane, panel, 40, 40, 100, 100) Public class MovingPnlDemo extends JFrame Here is an example: // Moving JPanel during runtime. Are you going to create moving JPanels during runtime, I recommend using MouseMotionListener and.
