Results 1 to 3 of 3
  1. #1
    AWJustAGuy is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default JFrame is blank until an unrelated while loop in another class finishes...

    I am completely stumped on how my computer is doing this. Basically I'm declaring a new frame, like so

    Java Code:
    view2.agentWindow(type);
    view2.stopActionListener(this);
    model2.addAgent(model, 214, 2, 5, "deposit");
    Here are the related functions being called:
    Java Code:
    public void agentWindow(String name)
    	{
    		
    		JPanel panel = new JPanel();
    		
    		frame = new JFrame(name);
    		frame.pack();
    		//set up frame size and location
    		Toolkit tk = Toolkit.getDefaultToolkit();
    	    Dimension screenSize = tk.getScreenSize();
    	    int screenHeight = screenSize.height;
    	    int screenWidth = screenSize.width;
    	    frame.setSize(screenWidth / 7, screenHeight / 7);
    	    frame.setLocation(screenWidth / 4, screenHeight / 4);
    	    
    		//Creates the text fields
    		display1 = new JTextField("", 20);
    		display1.setEditable(false);
    		panel.add(display1);
    		
    		display2 = new JTextField("", 20);
    		display2.setEditable(false);
    		panel.add(display2);
    		
    		state = new JTextField("Running...", 20);
    		state.setEditable(false);
    		panel.add(state);
    		
    		// Creates the buttons
    		stagent = new JButton("Stop Agent");
    		panel.add(stagent);
    		
    		dismiss = new JButton("Dismiss");
    		panel.add(dismiss);
    		
    		frame.add(panel);
    		frame.setVisible(true);
    	}
    Java Code:
    public void addAgent(Account model, int aid, long ops, double amount, String type)
    	{
    		run = true;
    		try
    		{
    			
    			while (run==true)
    			{
    				run = false;
    				if(type.equals("withdraw"))
    				{
    					model.setValue(model.getValue()-amount);
    				}
    				else if (type.equals("deposit"))
    				{
    					model.setValue(model.getValue()+amount);
    				}
    								
    				Thread.sleep(1000 * ops);
    				
    			} 
    		}
    		catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    Now what happens is that until the while loop in the model is finished, the new frame just displays as "deposit" with no text fields or buttons. Once the loop is finished, the things appear. It's quite irritating because my loop breaker is supposed to be one of the buttons that aren't appearing.

    Any ideas as to why this is happening?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: JFrame is blank until an unrelated while loop in another class finishes...

    If the loop is running on the GUI's thread, then the GUI can not be updated until you return the thread for it to execute on.
    You need to give the GUI code back its thread every time you want something to change and be visible, otherwise nothing is done until it gets control again.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: JFrame is blank until an unrelated while loop in another class finishes...

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2011, 11:05 PM
  2. use while loop in jframe
    By vinci in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 03:28 PM
  3. Getting unrelated input from several text fields
    By starexecutive in forum New To Java
    Replies: 1
    Last Post: 01-16-2011, 07:04 PM
  4. how to get value from unrelated class struts-hibernate
    By arsmith in forum Web Frameworks
    Replies: 0
    Last Post: 12-02-2009, 05:29 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 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
  •