Results 1 to 5 of 5
  1. #1
    dklett is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default Detecting user movement of a JFrame

    The JavaDoc for a ComponentEvent states that the COMPONENT_MOVED event is triggered only if the component's position has changed. I interpret this to mean the location of the component (coordinates of upper left hand corner). So if the component is resized, but the location does not change, then the COMPONENT_MOVED event should not occur. But if I create a JFrame, add all the required components, pack the frame and make it visible, and then do an addComponentListener() to the frame, I will still get a componentMoved() call if I add another component to the frame (which resizes the frame), but does not change it's position. So I cannot detect when the user grabs the frame and moves it, and when a resize happens by adding more components. Hope this description is clear. Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    What is the question?
    Have you written a program to test what happens?

  3. #3
    dklett is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default More information

    Yes, I have written a test program.

    The question is: why are two events dispatched (MOVED and RESIZED) when a component is added to an existing JFrame, but a setLocation() is not issued. The position of the JFrame has not changed, only the dimensions. Here are examples of output from the test program:

    Before pack() (initial pack)
    Moved event
    Next event = java.awt.event.ComponentEvent[COMPONENT_MOVED (0,22 128x67)] on frame0
    After pack()
    Resize event
    Next event = java.awt.event.ComponentEvent[COMPONENT_RESIZED (0,22 128x67)] on frame0

    I have no problem with the above two events.

    Now I grab the frame and move it (notice the first two numbers are the x,y values for the position):
    Moved event
    Next event = java.awt.event.ComponentEvent[COMPONENT_MOVED (645,314 128x67)] on frame0
    No problem here either.

    Now I click a button in the frame that adds another button to the frame.
    Moved event
    Next event = java.awt.event.ComponentEvent[COMPONENT_MOVED (645,314 128x96)] on frame0
    Resize event
    Next event = java.awt.event.ComponentEvent[COMPONENT_RESIZED (645,314 128x96)] on frame0

    I receive two events. Note that the position of the frame has not changed since the grab and move operation. The dimensions have changed.

    Why is there a MOVED event in the last case?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    why are two events dispatched
    You'll have to ask whoever wrote the code.
    The source code comes with the JDK, take a look at it.

    You'll have to keep the state the the window yourself to see if its position has changed.

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

    Default

    I'm unable to replicate what you reported. I tried the way shown below and also tried doing this without the JScrollPane, ie, returning "panel" from the getCenter method, and with calling
    Java Code:
    ((JFrame)panel.getTopLevelAncestor()).pack()
    at the end of the actionPerformed method.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Movement extends ComponentAdapter {
        JPanel panel;
        GridBagConstraints gbc;
    
        public void componentMoved(ComponentEvent e) {
            System.out.println(e.paramString());
        }
    
        private JPanel getFirst() {
            JButton button = new JButton("add button");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int n = panel.getComponentCount();
                    JButton button = new JButton(String.valueOf(n+1));
                    panel.add(button, gbc);
                    panel.revalidate();
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        private JScrollPane getCenter() {
            panel = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.insets = new Insets(20,0,20,0);
            gbc.weighty = 1.0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            return new JScrollPane(panel);
        }
    
        public static void main(String[] args) {
            Movement test = new Movement();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getFirst(), "First");
            f.add(test.getCenter());
            f.setSize(360,300);
            f.setLocation(200,200);
            f.setVisible(true);
            f.addComponentListener(test);
        }
    }

Similar Threads

  1. Detecting Mouse Down In SWT Tree Item
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:51 PM
  2. [SOLVED] File Movement/Manipulation
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 04-23-2008, 12:39 AM
  3. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM
  4. Detecting browser with JSP scriptlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:05 AM
  5. Detecting Browser Settings
    By arupranjans in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-31-2007, 02:31 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
  •