Results 1 to 14 of 14
Thread: START/STOP Button
- 08-17-2011, 01:52 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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.
Kinda hard to find a solution after 10 hours infront the pcimport 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();
}
}
}
}
}.gif)
Thanks in advanced.Last edited by tnrh1; 08-17-2011 at 01:58 PM.
- 08-17-2011, 02:48 PM #2
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!
- 08-17-2011, 02:59 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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.
- 08-17-2011, 03:40 PM #4
Or use a SwingWorker.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
- 08-17-2011, 05:06 PM #5
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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.
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.
- 08-17-2011, 05:22 PM #6
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.
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.
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!
- 08-17-2011, 05:59 PM #7
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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?
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.
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 06:04 PM.
- 08-17-2011, 06:09 PM #8
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!
- 08-18-2011, 05:39 AM #9
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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.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 itEvent 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..gif)
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 05:41 AM.
-
An ActionListener's actionPerformed method is called on the EDT.
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.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(); } } }
- 08-18-2011, 07:44 AM #11
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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; } } }
-
You didn't read the link. :headdesk:
- 08-18-2011, 01:49 PM #13
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
- 08-18-2011, 02:40 PM #14
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
-
how to start and stop other java applicaton
By Jhovarie in forum Threads and SynchronizationReplies: 1Last Post: 02-23-2011, 08:51 AM -
Stopping a thread using a stop button - GUI
By Ben1 in forum New To JavaReplies: 5Last Post: 01-27-2011, 04:21 PM -
Use stop button to stop moving (stop timers) on JPanel
By mneskovic in forum New To JavaReplies: 3Last Post: 07-23-2010, 12:50 PM -
Applets (init, start, stop, destroy)
By Java Tip in forum Java TipReplies: 0Last Post: 12-12-2007, 10:57 AM -
stop button in the browser
By Peter in forum Java ServletReplies: 2Last Post: 07-04-2007, 07:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks