This question already has answers here:
How to simulate a touch event in Android?
(7 answers)
Closed 4 years ago.
I've seen this route,
View.performClick();
but it doesn't show the actual press of the button. I've also tried this method,
btn.setPressed(true);
btn.invalidate();
but, it just shows the button being pressed down. I've narrowed it down to this code, which presses down, and releases, but doesn't click. Am I missing something? How can I do a complete click as if though the user was clicking (monkeyrunner is not an option as of right now)
btn = (Button) findViewById(R.id.btn_box);
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
btn.setPressed(true);
btn.invalidate();
Handler handler1 = new Handler();
Runnable r1 = new Runnable() {
public void run() {
btn.setPressed(false);
btn.invalidate();
}
};
handler1.postDelayed(r1, 1000);
}
};
handler.postDelayed(r, 1000);
Your code is fine. just add btn.performClick(); after the invalidate();
And for better look you can reduce the time of handler1.
Related
This question already has answers here:
Calling a method from a JButton is freezing a JFrame?
(2 answers)
Display indeterminate JProgressBar while batch file runs
(1 answer)
Something seems wrong with the layout, JButton showing unexpected behaviour at resize of the window
(4 answers)
Closed 5 years ago.
I am trying to make a program that has a toggle button (regular JButton) that when clicked, runs a while loop that runs until the button is clicked again to stop it.
I have done this, however, when I click the button, the entire JFrame freezes because of it being stuck in the while loop, as the loop will run forever until the button is pressed again. However, it is impossible to click the button again because the JFrame freezes. The button itself also just stays blue because the JFrame freezes before the colour change occurs; right before I click the button.
My code looks something like this:
boolean isRunning=false;
private void buttonClickEvent(ActionEvent evt) {
if(isRunning){
isRunning=false;
System.out.println("Stopped running!");
jButton.setText("Start Running");
} else { // BELOW IS THE CODE THAT CAUSES IT TO LOCK
isRunning=true;
jButton.setText("Stop Routine");
while(isRunning){
// DO STUFF
}
}
}
EDIT: I tried doing the following code (below) and it does print the text and allow the colour change to occur in the button, but the UI still freezes quickly afterward.
boolean isRunning=false;
private void buttonClickEvent(ActionEvent evt) {
if(isRunning){
isRunning=false;
System.out.println("Stopped running!");
jButton.setText("Start Running");
} else { // BELOW IS THE CODE THAT CAUSES IT TO LOCK
isRunning=true;
jButton.setText("Stop Routine");
new Thread(new Runnable() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
while(isInRoutine){
System.out.println("lolk");
}
}
});
}
}).start();
}
}
I am a beginner with multithreading and GUI development in Java with Swing and Window Builder in Eclipse.
I am trying to get a jlabel that shows the status of the program to display text and quickly fade from red to black when a button is pressed and then to disappear in ten seconds.
I tried to combine suggestions for this from several answers on Stack Overflow using a new thread and a timed task (How to fade color from white to black?) and the code works correctly the first time the button is pressed. However, when the button is pressed again and the method is called again, it only briefly blinks red and the text disappears very quickly.
I believe that it is because when the button is pressed a second time the old thread is still running and both are trying to control the label. I obviously found several question on Stack Overflow on how to kill treads in Java (including How do you kill a thread in Java?), but none of them seemed to help in my situation. I am relatively inexperienced with multithreading and Swing GUIs, so I probably made some really dumb mistakes with my code.
Here it the method that is called by a event listener when a button is pressed (this method is in a separate "Program Main" controller class):
private static void setBriefLblStatus(String content) {
one = new Thread() {
public void run() {
setLblStatus(content);
fading = Color.RED;
TimerTask fadingTask = new TimerTask() {
#Override
public void run() {
if (fading.getRed() > 0) {
fading = new Color(fading.getRed() - 1, 0, 0);
setLblStatusColor(fading);
}
}
};
timer = new Timer();
timer.scheduleAtFixedRate(fadingTask, 7, 7);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setLblStatus("");
}
};
one.start();
}
My Testprogram should change a TextViews text and after it is done, it should wait on second before the next text change. However my program runs the text changes instant behind each other:
t.setText("Test!");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
t.setText("Test - after 1 second!");
}
},1000);
The first text is not even there close to a second.
Hard to say why that wouldn't work from that limited amount of code but you can just add the post delayed to your View. You don't need a handler.
t.setText("Test!");
t.postDelayed(new Runnable() {
#Override
public void run() {
t.setText("Test - after 1 second!");
}
}, 1000);
All Views in Android have a built in handler class.
You are missing the following:
handler.postDelayed(this, 1000);
inside the run method. Here this will refer to the handler object
This question already has answers here:
Can a progress bar be used in a class outside main?
(3 answers)
Closed 8 years ago.
I have a swingworker that will be representing a jProgressbar. This is the code
private Swingworker timeOfProccess;
class Swingworker extends SwingWorker<Object, Object> {
#Override
protected Object doInBackground() throws Exception {
jProgressBar1.setStringPainted(true);
int progress = 0;
setProgress(0);
while (progress <= 100) {
jProgressBar1.setValue(progress);
Thread.sleep(5);
progress++;
}
mainProccess();
return null;
}
#Override
protected void done() {
jProgressBar1.setValue(100);
JOptionPane.showMessageDialog(null, "Proses Selesai");
jProgressBar1.setValue(0);
jProgressBar1.setStringPainted(false);
}
}
private void btnExecuteActionPerformed(java.awt.event.ActionEvent evt) {
timeOfProccess = new Swingworker();
timeOfProccess.execute();}
I dont know, why the progressbar running is uncontrolled. it is so fast to 100% even the process still working. But void done is success to pop-up the JoptionPane after main process end. where is I am lost in my code. thanks..
Visit How to Use Progress Bars where You will find good examples on progress bar along with detail description.
Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application.
Read more How to Use Swing Timers
sample code:
// delay for 1500 mill-seconds
Timer timer = new javax.swing.Timer(1500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// call for your next task
}
});
timer.setRepeats(true); // you can turn off the repetition
timer.start();
How to get the progressbar showing the number 1 - 100 %.
Use JProgressBar#setStringPainted() method to show the percent done status.
I am trying to achieve the following behavior:
You press a button which toggles through multiple elements (in swing). The point is, that a message or whatever should only appear if no toggle happened in the last second. That means you press button1, wait one second, then comes the message you pressed button1; and if you press button1, then (under one second) press button2, the message should say button2 was pressed.
I found some timer stuff like this (as i know now, this is a android example. perhaps there is something similar to this for swing??):
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 1000ms
}
}, 1000);
How could I stop the task? I don't need any code, just a hint.
For code
final Handler handler = new Handler();
Runnable runIt = new Runnable() {
#Override
public void run() {
//Do something after 1000ms
}
}
handler.postDelayed(runIt, 1000);
you can delete message from queue by
handler.removeCallbacks(runIt);