Results 1 to 3 of 3
  1. #1
    ace_quorthon is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Component Location Lag

    I'm trying to create a program in which panels/components start small and (visually) expand to their actual size once their parent has done the same thing. Therefore I need the actual location & dimensions of components.

    The thing I run into is that with any component you add, it takes a little while to get the correct location of the component according to the layout it is in. That is, if you add the component, it is not directly in the correct (x,y) location.

    JFrame
    I seem to have found out how to fix this for a JFrame: Adding a call to the JFrame.pack() method right after the addition of a component will make sure that the component is at the correct location. This is perhaps not ideal, so an alternative would be nice (although not that important).

    JApplet
    Now, I'm trying to get the same result for a JApplet.

    I would like to know what method makes sure that every component in the JApplet gets assigned it's correct location according to the layout it is in, so I can call that method directly and make sure my components are in their correct locations when I start resizing them.

    Code example that prints debuglines showing the issue:

    Java Code:
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    
    /**
     * This class shows that it takes time for the components 
     * of a JPanel to get their actual location.
     *
     */
    public class TestApplet extends JApplet implements ActionListener
    {
    	private JButton b1, b2;
    	private Timer printTimer;
    	private int numberOfPrints;
    	
    	/**
    	 * This initializes the applet.
    	 */
    	@Override
    	public void init()
    	{
    		this.setMinimumSize(new Dimension(300,200));
    		this.setPreferredSize(new Dimension(800,800));
    		this.setVisible(true);
    		
    		// We create the two test components that will act as an
    		// example.
    		b1 = new JButton("1");
    		b2 = new JButton("2");
    		
    		// The actual panel that will hold the test components.
    		JPanel holder = new JPanel();
    		holder.setLayout(new FlowLayout());
    		holder.add(b1);
    		holder.add(b2);
    		this.add(holder);
    		
    		// The state of the buttons right after they have been added:
    		System.out.println("Button state directly after addition:");
    		printBasicComponentInfo(b1);
    		printBasicComponentInfo(b2);
    		System.out.println("---");
    		
    		//These are the steps I tried to ensure that the buttons
    		//had their correct locations set:
    		//holder.invalidate();
    		holder.revalidate(); //Does not provide the wanted effect.
    		
    		// This is where I would like the buttons to have their correct
    		// location.
    		System.out.println("The buttons should now be in their correct location:");
    		printBasicComponentInfo(b1);
    		printBasicComponentInfo(b2);
    		System.out.println("---");
    		
    		//We will also print the info of the buttons after a few ms.
    		//Therefore we construct a timer that will ring soon.
    		printTimer = new Timer(10, this);
    		printTimer.restart();
    		numberOfPrints = 5; 
    	}
    	
    	/**
    	 * This method prints some basic info about the given component.
    	 * @param c
    	 */
    	private void printBasicComponentInfo(Component c)
    	{
    		if(c == null) return;
    		
    		//System.out.println("(w: " + c.getWidth() + ",h: " + c.getHeight() + ")");
    		System.out.println("(x: " + c.getX() + ",y: " + c.getY() + ")");
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent ae) 
    	{
    		if(ae.getSource().equals(printTimer))
    		{
    			System.out.println("Button state when the timer rings:");
    			printBasicComponentInfo(b1);
    			printBasicComponentInfo(b2);
    			System.out.println("---");
    			
    			numberOfPrints--;
    			
    			if(numberOfPrints <= 0)
    				printTimer.stop();
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Java Code:
    revalidate();
    repaint();
    db

  3. #3
    ace_quorthon is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you for your reply mr Burke. I have now tried calling the repaint method and it does not solve the issue.

    It is not the visual aspect of the buttons being in the wrong place, it is the actual variables x,y of the JButtons that are not yet correct. The output of my program:

    Button state directly after addition:
    (x: 0,y: 0)
    (x: 0,y: 0)
    ---
    The buttons should now be in their correct location: //But they are not, since they are still at (0,0) and should be at (56,5) and (102,5).
    (x: 0,y: 0)
    (x: 0,y: 0)
    ---
    Button state when the timer rings:
    (x: 56,y: 5)
    (x: 102,y: 5)
    ---


    Perhaps calling the repaint() method does not give us the correct result because we are still in the init() of the JApplet?
    Last edited by ace_quorthon; 02-01-2011 at 08:46 AM.

Similar Threads

  1. JList Location
    By BeeGee in forum Advanced Java
    Replies: 8
    Last Post: 04-07-2010, 03:09 PM
  2. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  3. location of click
    By saima in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2009, 12:06 PM
  4. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM
  5. how to get the location of some button
    By mary in forum Java 2D
    Replies: 2
    Last Post: 08-05-2007, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •