Results 1 to 4 of 4
  1. #1
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default How to mouse-drag a JWindow?

    Anybody could help me?:(
    And btw, whats the difference between the mouseDragged() method and the mouseMoved()?
    How do i use them?
    I have read the sun tutorial, but well, still a bit dull in the practice someho :p
    Been trying to override them for a while to make it work, but it haven't went well so far

    Oh n btw, i did use JWindow intentionally, in case ur wondering why i didn't use JFrame instead

    Thanks guys
    CruxBlack

  2. #2
    levent Guest

    Default

    Hi CruxBlack,

    I did not use mouse events on a JWindow, so i dont know possible problems there but as far as i guess those events should work same in ancestors of JComponent. But you can check what is reffered with a JWindow, if a JWindow does not contain inner part of a window, then catching events in that tiny area will be difficult!

    About the difference of mouseDragged and mouseMoved: mouseDragged event is fired when you move your mouse while the left mouse button is clicked! In mouseMoved, left mouse button does not need to be clicked. So even moving mouse around on top of your component will fire mouseMoved!

    If you write your code and ask your specific problems, i might help better.

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Well, im just trying to make some images appear in a JWindow, just for practice
    If i add some right-clicks inside the window then it will give an option whether to close the window (terminating the application that is) or not, so far that was solved by using a JOptionPane

    I also want if i do left-click inside the window, there would be a choice just like the usual left click menu, containing properties and some other custom choices which i might insert later
    Though, i havent find any way to process whether the MouseEvent input is a left or right click, anybody know how?:confused:

    Bout the mouseDrag problem, i want to make it that if i mousePressed and drag the window, the window would follow according to where i dragged it, just like if u drag a frame, but in JWindow i cant seem to drag it at all
    Actually, none of the MouseMotionListener implementation worked at all, the mouseMoved itself that Mr. levent said to send a MouseEvent simply by hovering above the component doesn't work even when i move my mouse inside the window

    Bit troubled here, anybody experienced with JWindow could help me?:(
    here's the code so far, just a simple one
    Java Code:
    import java.awt.event.*;
    import java.net.*;
    import javax.swing.*;
     
    public class Anima2 implements MouseListener,MouseMotionListener
    {
    	JWindow w;
    	public static void main(String[] args) throws MalformedURLException 
    	{
    		String path = <Path where the image is located, i used a gif btw>;
    		Anima2 a = new Anima2(path);
    	}
    	public Anima2(String path) throws MalformedURLException 
    	{
    		URL url = new URL(path);
    		Icon icon = new ImageIcon(url);
    		JLabel label = new JLabel(icon);
    		
    		w = new JWindow();
    		w.addMouseListener(this);
    		w.getContentPane().add(label);
    		w.pack();
    		w.setLocationRelativeTo(null);
    		w.setVisible(true);
    	}
    	public void mousePressed(MouseEvent e)
    	{
    		System.out.println("Mouse Pressed");
    	}
    	public void mouseReleased(MouseEvent e)
    	{
    		System.out.println("Mouse Released");
    	}
    	public void mouseClicked(MouseEvent e)
    	{
    		System.out.println("Mouse Clicked");
    		JOptionPane j = new JOptionPane();
    		int i = j.showConfirmDialog(null,"Close?");
    		if(i == 0)
    			System.exit(0);
    	}
    	public void mouseEntered(MouseEvent e)
    	{
    		System.out.println("Entering Window");
    	}
    	public void mouseExited(MouseEvent e)
    	{
    		System.out.println("Leaving Window");
    	}
    	//these 2 methods below doesnt work at all
    	public void mouseDragged(MouseEvent e)
    	{
    		System.out.println("Dragged");
    	}
    	public void mouseMoved(MouseEvent e)
    	{
    		System.out.println("Moved");
    	}
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I also want if i do left-click inside the window, there would be a choice just like the usual left click menu, containing properties and some other custom choices
    You can use a JPopupMenu for this. The tutorial has a relevant section (see below). The mouse button that triggers the popup, the popupTrigger, varies by platform.
    i havent find any way to process whether the MouseEvent input is a left or right click
    One of the easier ways is with SwingUtilities. It has three methods, eg, SwingUtilities.isLeftMouseButton(aMouseEvent). The MouseEvent api has more info in the comments section.
    Aside from the focus problem (see below) this might be difficult. You could try working with the MouseInfo and PointerInfo classes (j2se 1.5+). And the other helpful resource is again SwingUtilities which has some convenience methods such as convertPointToScreen.
    none of the MouseMotionListener implementation worked at all
    Focus is an issue beginning with j2se 1.4. Look in the Window class api for the method isFocusableWindow. For the window, its JRootPane or any child components to be able to send Input/MouseEvents it/they must have the focus. This method lists the requirements for this to happen.
    Here are a couple of links into the tutorial that may be helpful.
    How to Use Menus
    esp, Bringing Up a Popup Menu
    How to Use the Focus Subsystem
    esp, Making a Custom Component Focusable

Similar Threads

  1. Image with JWindow -> trails
    By ofir3dvb in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2008, 05:21 PM
  2. JWindow/JFrame with boarders
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:35 AM
  3. The mouse and the cheese
    By Don Quixote in forum Java 2D
    Replies: 4
    Last Post: 08-15-2007, 10:55 PM
  4. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 10:19 PM
  5. Mouse over JButton
    By sandor in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2007, 09:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •