Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By sehudson

Thread: Update my window every second?

  1. #1
    Jib
    Jib is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Question Update my window every second?

    Hello everyone!
    I've made a piece of code which opens up a window and makes a button. Then I added a action listener and it does what I tell it. Here is my code:

    Java Code:
    package tutorial_fromthestart;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class tutorial_fromthestart{
    	
    	public static void main(String[] args){
    		tutorial_fromthestart a = new tutorial_fromthestart();
    	}
    	
    	public tutorial_fromthestart(){
    		JFrame frame = new JFrame("Fir");
    		frame.setLayout(null);
    		JButton button = new JButton("Button1");
    		button.addActionListener( new ActionListener()
    		{
    		    public void actionPerformed(ActionEvent e)
    		    {
    		        System.exit(0);
    		    }
    		});
    		button.setSize(100, 30);
    		frame.add(button);
    		frame.setVisible(true);
    		frame.pack();
    		frame.setSize(500, 500);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    So I've got a question which is answered lot of times but I can't understand it.. I mean can someone explain me please how to update my window every second? I mean for example if I add a JLabel and then tell the action listener to change the JLabel text it won' t change it because it doesn't update every second I suppose. Anyways, can someone help me? It will be really helpful and appriciated.

    Thaks :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,971
    Rep Power
    8

    Default Re: Update my window every second?

    Sounds like a job for a Swing Timer.
    Jib likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Update my window every second?

    From what I see, your ActionListener exits the program. What are you seeing when you update your ActionListener to change the text of the JLabel? I'm not sure why you would need to "update" every second just because you are changing the text of a JLabel when a button is clicked.
    Jib likes this.

  4. #4
    Jib
    Jib is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Update my window every second?

    Quote Originally Posted by sehudson View Post
    From what I see, your ActionListener exits the program. What are you seeing when you update your ActionListener to change the text of the JLabel? I'm not sure why you would need to "update" every second just because you are changing the text of a JLabel when a button is clicked.
    I added the System.exit for my reasons.. just to check if the action listener works correctly. I want to make something like user login system.. that's why I need to update my JLabel, for reasons to output the user messages and stuff.
    Last edited by Jib; 12-05-2013 at 06:06 PM.

Similar Threads

  1. Closing secondary window from button in primary window
    By KoncentriK in forum New To Java
    Replies: 4
    Last Post: 02-26-2012, 01:24 AM
  2. Replies: 1
    Last Post: 07-25-2011, 10:24 AM
  3. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  4. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 12:17 PM
  5. Replies: 18
    Last Post: 11-22-2008, 05:25 PM

Tags for this Thread

Posting Permissions

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