<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Bain &#187; Java</title>
	<atom:link href="http://www.paulbain.com/articles/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paulbain.com</link>
	<description>Web Specialist</description>
	<lastBuildDate>Thu, 05 Nov 2009 20:54:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HowTO: Draggabe JFrame Without Decoration</title>
		<link>http://www.paulbain.com/articles/howto-draggabe-jframe-without-decoration/</link>
		<comments>http://www.paulbain.com/articles/howto-draggabe-jframe-without-decoration/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 10:34:25 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Java Development]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.paulbain.com/?p=122</guid>
		<description><![CDATA[I was playing with writing a quick time tracking application called Efficiency which needed to be always ontop of other windows and therefore be a subtle as possible. I therefore decided to undecorate the window, however the user is then unable to reposition the window, so I wrote a some code to quickly allow them [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing with writing a quick time tracking application called Efficiency which needed to be always ontop of other windows and therefore be a subtle as possible. I therefore decided to undecorate the window, however the user is then unable to reposition the window, so I wrote a some code to quickly allow them to do this by just dragging the JFrame.</p>
<p><img class="aligncenter size-full wp-image-129" title="coffee" src="http://www.paulbain.com/wp-content/uploads/2009/10/coffee.jpg" alt="coffee" width="620" height="302" /></p>
<p>My class extends the javax.swing.JFrame class, so I&#8217;ve set a few properties in the initComponents() function and more importantly added listeners for MousePressed and MouseDragged.</p>
<pre>    private void initComponents() {
        ...
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(0, 0, 0));
        setResizable(false);
        setUndecorated(true);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });
        ...
    }</pre>
<p>Firing an action on mousePressed is required to get the original location of the window. This fires the following function, setting the two variables which are of type java.awt.Point:</p>
<pre>    private void formMousePressed(java.awt.event.MouseEvent evt) {
        this.start_drag = this.getScreenLocation(evt);
        this.start_loc = this.getLocation();
    }</pre>
<p>The mouse dragged function is as follows:</p>
<pre>    private void formMouseDragged(java.awt.event.MouseEvent evt) {
            Point current = this.getScreenLocation(evt);
    Point offset = new Point((int) current.getX() - (int) start_drag.getX(),
        (int) current.getY() - (int) start_drag.getY());

    Point new_location = new Point(
        (int) (this.start_loc.getX() + offset.getX()), (int) (this.start_loc
            .getY() + offset.getY()));
        this.setLocation(new_location);
    }</pre>
<p>You&#8217;ll also need the utilty funciton to convert the points to a screen location:</p>
<pre>      Point getScreenLocation(MouseEvent e) {
        Point cursor = e.getPoint();
        Point target_location = this.getLocationOnScreen();
        return new Point((int) (target_location.getX() + cursor.getX()),
            (int) (target_location.getY() + cursor.getY()));
      }</pre>
<h3>Reference</h3>
<ul>
<li><a href="http://www.java2s.com/Code/Java/Swing-JFC/Dragandmoveaframefromitscontentarea.htm">Drag and Move</a> &#8211; material from java2s.com used as a basis but using an external MoveMouseListner class</li>
</ul>
<p><em>Photo Credit: <a title="Link to dongga BS' photostream" rel="dc:creator cc:attributionURL" href="http://www.flickr.com/photos/dongga/">dongga BS</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbain.com/articles/howto-draggabe-jframe-without-decoration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
