Click here to Skip to main content
13,005,991 members (54,320 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 12 Nov 2011

Dragging a borderless frame in Java

, 13 Nov 2011
Rate this:
Please Sign up or sign in to vote.
A borderless JFrame needs an extra bit of coding to make it draggable
In case of giving your application a fancy look, sometimes you may consider it to have some custom border.You will remove the default border from it & wish to have it the draggable capability (as you know, the border is the one that enables your JFrame to move when you drag).So how to achieve this?
It's simple. Let's say your application's main window(the JFrame that will be visible at first launch) is MyWindow such that:
class MyWindow extends JFrame

And you make it borderless by:


Now let's say you have made your custom window using your own GUI coding,images & bla,bla. What you need now is to drag it when you wish. Create fields like the following:
int posX=0,posY=0;

Now we do the following coding:

this.addMouseListener(new MouseAdapter()
   public void mousePressed(MouseEvent e)

At last, the main part of it:

this.addMouseMotionListener(new MouseAdapter()
     public void mouseDragged(MouseEvent evt)
		//sets frame position when mouse dragged			
		setLocation (evt.getXOnScreen()-posX,evt.getYOnScreen()-posY);

You are done! Try with a simple JFrame application and you will see your borderless JFrame is moving smoothly as any other business app!
Here[^] is a handy library(by me ;)) for doing these except using extra codes; simple & strong.You can try it too.
Well, I think you have understood the simple coding & the explanation behind it (why did I do each line of coding). In case you want to know a bit more details, don't forget to give me a knock!
Cheers :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Junior) ManGoes Mobile,Inc.
Bangladesh Bangladesh

ManGoes Mobile, Inc.
Software Developer-Android & iOS platform, present
Udvash Academic & Admission Care
Lecturer,Physics Department, present


Bangladesh University of Engineering & Technology
Computer Science & Engineering, present
Dhaka City College
Major-Science, 2006 - 2008


Software designing,UX designing,Architecture designing,
Graphics designing.

You may also be interested in...

Comments and Discussions

QuestionExcellent Work! Pin
Member 1120193917-Nov-14 10:07
memberMember 1120193917-Nov-14 10:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170628.1 | Last Updated 13 Nov 2011
Article Copyright 2011 by Coderguy194
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid