Results 1 to 14 of 14
  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default START/STOP Button

    Hello everyone,
    I've created a java application that deletes all the files in a specific folder 24/7 (while loop).
    I want the software to be stoped when I click again on the start button:



    I'm very confused and dont know where to change the buttom text.
    btw the software is running with a while(true) which is suck, the while above is somthing that I've tried.
    And the while condition must be more complicated to make it work.
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    public class GUI extends JFrame
    {
    // Variables declaration

    JFrame window;
    JPanel p;
    GridBagConstraints c;
    Font f1,f2;
    JLabel label1,label2;
    JButton b;

    public void setVars()
    {
    window = new JFrame();
    p = new JPanel(new GridBagLayout());
    c = new GridBagConstraints();
    f1 = new Font("Arial",Font.BOLD,16);
    f2 = new Font("Arial",Font.BOLD,10);
    label1 = new JLabel("Put me on your server files folder and click start.");
    label2 = new JLabel("Then, I will delete for you all the files in there :) 24/7");
    b = new JButton("Start");
    }

    public void go()
    {
    // Window configuration
    window.setSize(340, 100);
    window.setTitle("BlackUser deleter Made By Imri");
    window.setVisible(true);
    window.setLocation(650,200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);

    // Add Objects
    window.add(p);

    // label1
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(1,1,1,1);
    label1.setFont(f2);
    p.add(label1,c);

    // label2
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(1,1,1,1);
    label2.setFont(f2);
    p.add(label2,c);

    // b
    c.gridx = 0;
    c.gridy = 2;
    p.add(b,c);
    b.addActionListener(new Action());

    }

    static class Action implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    while(true)
    {
    File directory = new File("blackuser");
    String[] fileArr = directory.list();
    int length = fileArr.length;

    File fileName;
    for(int i=0;i<length;i++)
    {
    fileName = new File("blackuser/"+fileArr[i]);
    fileName.delete();
    }
    }
    }
    }
    }
    Kinda hard to find a solution after 10 hours infront the pc
    Thanks in advanced.
    Last edited by tnrh1; 08-17-2011 at 02:58 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Which part of this are you having trouble with?

    When do you want the text to change? Change it then.

    For the loop, instead of doing while(true), do something like while(someBoolean), and set someBoolean whenever you want.

    Also, don't do your deletions on the EDT, as that's just going to freeze up the GUI.
    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
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    What you should be doing is putting the while loop inside a separate thread, also you should add a longish sleep inside the while loop as I doubt you are going to be concerned with a delay of a few seconds when deleting the files. I haven't used Java threads much myself but I sure others can help if you need further details.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default

    I'm replaying from work so I didn't had enough time to read all the replays, when i'll get home I'll treat to everyone.
    Quote Originally Posted by KevinWorkman View Post
    When do you want the text to change? Change it then.
    For the loop, instead of doing while(true), do something like while(someBoolean), and set someBoolean whenever you want.
    Theoretically I know what to do .. that would be while the variable that hold the button value %2==1 keep running.
    else stop.
    This variable for example would be x and the first value would be 0.
    When I click start, x will be equal to 1 which means (x%2==1)=true.
    Now when I press again the value will be increased to 2 which means (x%2==1)=false and then the loop stoped.

    The reason that I've opened the thread is that I dont know how to implement it.
    So please, replay with a coded solution and not a theoretically one.

    Btw, what is EDT? because you right about that one .. my GUI freezed.

    Thanks for replaying guys.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by tnrh1 View Post
    I'm replaying from work so I didn't had enough time to read all the replays, when i'll get home I'll treat to everyone.

    Theoretically I know what to do .. that would be while the variable that hold the button value %2==1 keep running.
    else stop.
    This variable for example would be x and the first value would be 0.
    When I click start, x will be equal to 1 which means (x%2==1)=true.
    Now when I press again the value will be increased to 2 which means (x%2==1)=false and then the loop stoped.
    Why not just use a boolean? Also, once you exit the loop, it's not going to keep checking the condition, so you'll have to start the thread again.

    Quote Originally Posted by tnrh1 View Post
    The reason that I've opened the thread is that I dont know how to implement it.
    So please, replay with a coded solution and not a theoretically one.
    Absolutely not. Asking for code like that is extremely rude. Don't do it again. See the link in my signature on asking questions the smart way before you post again.

    Quote Originally Posted by tnrh1 View Post
    Btw, what is EDT? because you right about that one .. my GUI freezed.
    What did google tell you?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default

    Quote Originally Posted by KevinWorkman View Post
    Why not just use a boolean?
    Hmm let's say the default is false ..
    ProcesStarted = false;

    -----Button clicked-----
    if(ProcesStarted)
    ProcesStarted = false;
    else
    ProcesStarted = true;
    ------------------------
    This if condition is necessary since you can't know what the value was before the action performed, so after all it doesn't matters if it's boolen or not.
    Or did you mean to something else?

    Quote Originally Posted by KevinWorkman View Post
    Also, once you exit the loop, it's not going to keep checking the condition, so you'll have to start the thread again.
    Of course when you exit the loop it's will stop checking the condition .. I don't really understand what did you tryed to say.

    Quote Originally Posted by KevinWorkman View Post
    Absolutely not. Asking for code like that is extremely rude.
    I'm sorry then, I had no intention to be rude.

    Let's seperate it into 2 goals:
    1. Make the application to be able to stop and start again.
    2. Change the text each time the above line happend.

    Let's focus on the first one first :)
    Last edited by tnrh1; 08-17-2011 at 07:04 PM.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Step 1- Get that processing off the EDT, otherwise you're not going to be able to respond to any GUI input until it completes, so you won't be able to stop it.
    Step 2- Add a condition to the loop that you change when the button is pressed. Boolean makes more sense to me, and I think you'll see why if you start coding it your way. You'll also have to add a way to restart the loop after stopping it again- which I also think will make more sense once you start writing code.

    Switching the text is a one liner.

    Try that out, and if you still have questions, feel free to provide an SSCCE that demonstrates where you're stuck.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default

    Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread.
    Event Dispatch Thread. The thread that you do all your AWT and Swing GUI work on, perhaps with SwingUtilities. invokeLater, or EventQueue. If a program bug aborts the EDT thread, it is immedately restarted. This means the EDT thread is not necessarily the same thread throughout a program’s execution.
    This is the most usefull information I've found about EDT, if I'm not wrong the edt is where the action performed or I still didn't get it
    Why to take the loop outside of the ActionPerformed?I dont want it to run always .. only when I click the button.

    Thanks for replaying.

    EDIT:
    maybe somthing like this?:
    Java Code:
    static class Action implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			startDelete();
    		}
    	}
    	public static void startDelete()
    	{
    		while(true)
    		{
    		    File directory = new File("blackuser");
    		    String[] fileArr = directory.list(); 
    		    int length = fileArr.length;
    		    
    		    File fileName;
    		    for(int i=0;i<length;i++)
    		    {
    		    	fileName = new File("blackuser/"+fileArr[i]);
    		    	fileName.delete();
    		    }
    		}
    	}
    Last edited by tnrh1; 08-18-2011 at 06:41 AM.

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

    Default

    Quote Originally Posted by tnrh1 View Post
    This is the most usefull information I've found about EDT, if I'm not wrong the edt is where the action performed or I still didn't get it :
    An ActionListener's actionPerformed method is called on the EDT.


    Why to take the loop outside of the ActionPerformed?I dont want it to run always .. only when I click the button.

    EDIT:
    maybe somthing like this?:
    Java Code:
    static class Action implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			startDelete();
    		}
    	}
    	public static void startDelete()
    	{
    		while(true)
    		{
    		    File directory = new File("blackuser");
    		    String[] fileArr = directory.list(); 
    		    int length = fileArr.length;
    		    
    		    File fileName;
    		    for(int i=0;i<length;i++)
    		    {
    		    	fileName = new File("blackuser/"+fileArr[i]);
    		    	fileName.delete();
    		    }
    		}
    	}
    No, you most definitely do not want to do this (as has been mentioned to you already several times above!). Any thread that has the while (true) in it will freeze and nothing can be done in that thread while the while loop is on-going. If you do this to the EDT, then it will freeze and not be able to perform any of its functions such as interactions with the user and painting the application. So please believe us when we tell you this advice *again* -- don't do long processes on the EDT. Please read the link that Darryl provided for you above. If you've read it, then please re-read it.

  11. #11
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default

    Thank you guys so much for replaying, I learned alot by making this small project:
    Java Code:
    public class Index
    {
    	public static void main(String[] args)
    	{
    		
    		GUI g = new GUI();
    		g.setVars();
    		g.go();
    		while (true)
    			g.loopDelete();
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    public class GUI extends JFrame
    {
    	// Variables declaration
    	boolean clicked=false;
    	JFrame window;
    	JPanel p;
    	GridBagConstraints c;
    	Font f1,f2;
    	JLabel label1,label2;
    	JButton b;
    	
    	public void setVars()
    	{
    		window = new JFrame();
    		p = new JPanel(new GridBagLayout());
    		c = new GridBagConstraints();
    		f1 = new Font("Arial",Font.BOLD,16);
    		f2 = new Font("Arial",Font.BOLD,10);
    		label1 = new JLabel("Put me on your server files folder and click start.");
    		label2 = new JLabel("Then, I will delete for you all the files in there :) 24/7");
    		b = new JButton("Start");
    		
    	}
    	
    	public void go()
    	{
    		// Window configuration
    		window.setSize(340, 100);
    		window.setTitle("BlackUser deleter Made By Imri");
    		window.setVisible(true);
    		window.setLocation(650,200);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		// Add Objects
    		window.add(p);
    			
    		// label1
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = new Insets(1,1,1,1);
    		label1.setFont(f2);
    		p.add(label1,c);
    		
    		// label2
    		c.gridx = 0;
    		c.gridy = 1;
    		c.insets = new Insets(1,1,1,1);
    		label2.setFont(f2);
    		p.add(label2,c);
    			
    		// b
    		c.gridx = 0;
    		c.gridy = 2;
    		p.add(b,c);
    		b.addActionListener(new Action());
    		
    	}
    
    	class Action implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(clicked)
    			{
    				clicked = false;
    				b.setText("Start");
    			}
    			else
    			{
    				clicked = true;
    				b.setText("Stop");
    			}
    		}
    	}
    	
    	public void loopDelete()
    	{
    		while (clicked)
    		{
    			File directory = new File("blackuser");
    		    String[] fileArr = directory.list(); 
    		    int length = fileArr.length;  
    		    File fileName;
    		    int i;
    		    for(i=0;i<length;i++)
    		    {
    		    	fileName = new File("blackuser/"+fileArr[i]);
    		    	fileName.delete();
    		    } 
    		    i=0;
    		}
    	}
    }

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

    Default

    You didn't read the link. :headdesk:

  13. #13
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default

    Quote Originally Posted by Fubarable View Post
    You didn't read the link. :headdesk:
    lol I'm working alot this time, so when I arrived home I just tryed to fix my error and looks like I did.
    Since you said this I guess the way was wrong but the result is fine.
    I will try to find some time to read this,
    Thanks.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    That might technically sorta work, but you're eating up WAY too many processor cycles just busy waiting. Most people wouldn't use a program that did 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!

Similar Threads

  1. how to start and stop other java applicaton
    By Jhovarie in forum Threads and Synchronization
    Replies: 1
    Last Post: 02-23-2011, 09:51 AM
  2. Stopping a thread using a stop button - GUI
    By Ben1 in forum New To Java
    Replies: 5
    Last Post: 01-27-2011, 05:21 PM
  3. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 01:50 PM
  4. Applets (init, start, stop, destroy)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 11:57 AM
  5. stop button in the browser
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 07-04-2007, 08:21 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
  •