Results 1 to 4 of 4
  1. #1
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Exclamation [Solved] Tiny Problem with JFrame

    Hi
    I am writing a simple program in java which is simply:
    an panel with one button on it, when you drag the mouse over the panel, the button should follow the mouse ( it is very simple ), the program is working fine, but when I minimize the window or maximize it, the button jumps back to its original location ?
    may I know why this is happening?
    and how to solve it?

    Best Regards.

    this is the code:

    Java Code:
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import javax.swing.*;
    
    public class MoveButtons extends JFrame{
        private JButton move=new JButton("move");
        JPanel panel=new JPanel();
    // a listener is added to the panel, when the mouse is dragged over the panel the button "move" is set to to location of the mouse the problem is that when I minimize the window and restore it, the button "move" jump to its original location  this problem occurs also when I maximize the window
    
        public MoveButtons(){
            panel.add(move);
            panel.addMouseMotionListener(new MouseMotionListener() {
                public void mouseDragged(MouseEvent e) {
                    move.setLocation(e.getX(),e.getY());
                }
                public void mouseMoved(MouseEvent e) {
    
                }
            });
            add(panel);
            setSize(500,500);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setAlwaysOnTop(true);
            setVisible(true);
        }
        public static void main(String [] args){
            new MoveButtons();
        }
    
    }
    Moderator Edit: Code tags added
    Last edited by mr_anderson; 05-30-2010 at 02:12 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck, and again, welcome!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is that your JPanel, panel is being created with a default layout of FlowLayout (as all JPanels do), and whenever you resize this JPanel, its layout manager is being told to re-layout all components held by it. To fix this, set the JPanel to have a null layout via:

    Java Code:
    panel.setLayout(null);
    If you do this, please note that you are fully responsible for setting the size and initial position of all components held by this container, namely you will need to set the JButton's size and location. To set the JButton to the size that Swing thinks is preferred for it, you can simply do this:
    Java Code:
    move.setSize(move.getPreferredSize());
    I'll leave it to you to figure out how to set move's initial position.

    Much luck!

  4. #4
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    Thank you very very much, I am so grateful and I will use the code tag from now on,
    and again thank you very much

Similar Threads

  1. Client Server program, a tiny problem.
    By skarosg3 in forum Networking
    Replies: 12
    Last Post: 05-27-2010, 02:03 PM
  2. What is wrong with my tiny code?
    By Znote in forum New To Java
    Replies: 5
    Last Post: 11-07-2009, 05:42 PM
  3. 'Tiny' 3D Problem
    By Supamagier in forum Advanced Java
    Replies: 2
    Last Post: 09-03-2009, 10:34 AM
  4. The Tiny Multi Agent Platform 2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-21-2007, 08:56 PM
  5. Tiny Eclipse 3.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-09-2007, 05:34 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
  •