Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

Moveable Components

Thread view: 
RayL - 06 Jan 2004 22:16 GMT
I need to create a gui and allow the user to rearrange some of the
components on the screen.  Has anyone seen a write-up on anything like this?

Thanks,
Ray
ak - 06 Jan 2004 23:38 GMT
> I need to create a gui and allow the user to rearrange some of the
> components on the screen.

do you mean rearrange by dragging?
--

____________

http://reader.imagero.com the best java image reader.
RayL - 07 Jan 2004 00:34 GMT
Yes

>>I need to create a gui and allow the user to rearrange some of the
>>components on the screen.
[quoted text clipped - 5 lines]
>
> http://reader.imagero.com the best java image reader.
ak - 07 Jan 2004 01:56 GMT
public static void main(String[] args) {
 JFrame frame = new JFrame();
 JDesktopPane desktopPane = new JDesktopPane();
 frame.setContentPane(desktopPane);

 for(int i = 0; i < 8; i++) {
  JPanel panel = new JPanel();
  panel.setBackground(Color.blue);
  panel.setSize(100, 100);
  DragHandler handler = new DragHandler(panel);
  desktopPane.add(panel);
 }
 frame.pack();
 frame.setVisible(true);
}

static class DragHandler extends MouseInputAdapter {

 int x0, y0, x, y;
 JComponent comp;
 Rectangle rw = new Rectangle();

 public DragHandler(JComponent comp) {
  this.comp = comp;
  comp.addMouseListener(this);
  comp.addMouseMotionListener(this);
 }

 public void mousePressed(MouseEvent e) {
  x = e.getX();
  y = e.getY();
  Point p = new Point(x, y);
  SwingUtilities.convertPointToScreen(p, e.getComponent());
  x = p.x;
  y = p.y;

  x0 = comp.getX();
  y0 = comp.getY();
 }

 public void mouseReleased(MouseEvent e) {
 }

 public void mouseDragged(MouseEvent e) {
  Point p = e.getPoint();
  SwingUtilities.convertPointToScreen(p, e.getComponent());
  int dx = x - p.x;
  int dy = y - p.y;
  rw = comp.getBounds(rw);
  rw.x = x0 - dx;
  rw.y = y0 - dy;

  comp.setLocation(rw.x, rw.y);
 }
}

--

____________

http://reader.imagero.com the best java image reader.

> Yes
>
[quoted text clipped - 7 lines]
> >
> > http://reader.imagero.com the best java image reader.
RayL - 07 Jan 2004 11:26 GMT
I'm going to try to play with this today.

Thank you very much.

Ray

> public static void main(String[] args) {
>   JFrame frame = new JFrame();
[quoted text clipped - 69 lines]
>>>
>>>http://reader.imagero.com the best java image reader.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.