I would like to add the button controls in a timely manner.
That means, after the shell opened, it should start placing the buttons one by one
in a 1 second delay. I wrote the program,however it does not work. all the buttons
are visible only after all the controls are placed. Some kind of refresh issue I guess.
Following is my code.
public class DelayAddingComponentsExample {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(200, 200);
shell.setLayout(new FillLayout(SWT.VERTICAL));
addAutomatically(shell);
// removeAutomatically(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void addAutomatically(final Shell shell) {
for (int i = 0; i < 5; i++) {
final Button button = new Button(shell, SWT.NONE);
button.setText("Button" + i);
button.setVisible(false);
}
shell.getDisplay().timerExec(0, new Runnable() {
#Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
final Button button = new Button(shell, SWT.NONE);
button.setText("Button" + i);
button.setVisible(true);
shell.pack();
shell.layout(true);
shell.redraw();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
public static void removeAutomatically(final Shell shell) {
for (int i = 0; i < 5; i++) {
final Button button = new Button(shell, SWT.NONE);
button.setText("Button" + i);
shell.layout(true);
}
shell.getDisplay().timerExec(0, new Runnable() {
#Override
public void run() {
Control[] controls = shell.getChildren();
for (Control control : controls) {
try {
Thread.sleep(500);
control.dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
The Runnable given to timerExec runs in the UI thread. So the Thread.sleep calls you are making are blocking the UI thread - it is vital that you never block this thread. Never call Thread.sleep in the UI thread.
You must do each step using a separate timeExec call and use the delay parameter of the timerExec call to specify how long to wait.
So
shell.getDisplay().timerExec(500, new Runnable() {
#Override
public void run()
{
// TODO code for the first button only
// Schedule next update
shell.getDisplay().timerExec(500, .... code for second button);
}
});
Runs the Runnable after 500 millseconds, the Runnable should just do the first step and then call timerExec again to schedule the next step.
Related
#Test
public void testHeaderlinks() throws IOException {
homePage=new HomePage();
homePage.ClickmenuHolidays();
homePage.Clickmenuattraction();
homePage.Clickmenuhotdeal();
String originalHandle = driver.getWindowHandle();
for(String handle : driver.getWindowHandles()) {
if (!handle.equals(originalHandle)) {
driver.switchTo().window(handle);
driver.close();
}
}
driver.switchTo().window(originalHandle);
}
public static void Takescreenshot(String filename) throws IOException {
File file= ((TakesScreenshot))driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("C:\\Users\\User\\Desktop\\POMMabuhayFinal\\src\\main\\java\\Screnshots\\"+filename+".jpg"));
}
In my code i am click hyperlinks in new tab and close those tabs (closing all tabs after all hyperlinks clicks)
My question is how can i take screenshot of opened tabs( i wanna take all new tabs which are open)
Use WebDriverWait to wait for tab/page load and then call your Takescreenshot method while you iterate tabs
#Test
public void testHeaderlinks() throws IOException {
homePage=new HomePage();
homePage.ClickmenuHolidays();
homePage.Clickmenuattraction();
homePage.Clickmenuhotdeal();
String originalHandle = driver.getWindowHandle();
int count = 1;
for(String handle : driver.getWindowHandles()) {
if (!handle.equals(originalHandle)) {
// Initialize WebDriverWait with 15 secs wait timeout or expected wait timeout
WebDriverWait wait = new WebDriverWait(myDriver, 15);
// Wait for page to load
wait.until(webDriver -> ((JavascriptExecutor) myDriver).executeScript("return document.readyState").toString().equals("complete"));
driver.switchTo().window(handle);
// Take screenshot of your current tab
Takescreenshot("tabImage" + String.valueOf(count)) // You can use your own imageName
driver.close();
count++;
}
}
driver.switchTo().window(originalHandle);
}
public static void Takescreenshot(String filename) throws IOException {
File file= ((TakesScreenshot))driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("C:\\Users\\User\\Desktop\\POMMabuhayFinal\\src\\main\\java\\Screnshots\\"+filename+".jpg"));
}
Please Try this
public void TakeScreenShot()
{
try
{
string text = AppDomain.CurrentDomain.BaseDirectory + "\\screenshots\\";
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
if (Directory.Exists(text))
{
string text2 = text + XYZ+ ".jpeg";
if (File.Exists(text2))
{
File.Delete(text2);
}
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(text2, ScreenshotImageFormat.Jpeg);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I'm pretty new to java (first post) and trying to make a loop that does the following:
Post to a textview, Wait, Post to another textview, Wait, Clear both (by posting " " to each one), Sleep, Repeat loop.
The loop send the messages to the que almost instantaneously, without doing the sleep part inbetween
Runnable postRight = new Runnable() {
public void run() {
right_side.setText("post right");
}
};
Runnable postLeft = new Runnable() {
public void run() {
left_side.setText("post left");
}
};
Runnable clear = new Runnable() {
public void run() {
left_side.setText(" ");
right_side.setText(" ");
}
};
/////////////////////////////////////////////////////////////
while (i < 3) {
Log.i(TAG, "Entered while");
long Timer = SystemClock.uptimeMillis();
handler.postAtTime(postLeft, Timer + 1000);
Log.i(TAG, "post left");
handler.postAtTime(postRight, Timer + 2000);
Log.i(TAG, "postright");
handler.postAtTime(clear, Timer + 3000);
Log.i(TAG, "clear");
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(4000);
runOnUiThread(new Runnable() {
public void run() {
Log.i(TAG, "inside runnable");
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
i++;
}
the thread t is sleeping, but it's its own thread, so the main thread just keeps on going after initiating and the main thread never sleeps
I am trying to display a counter but it is not working here is my code:
protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EditText v2 = (EditText)FindViewById(Resource.Id.editText5);
counter1--;
if (counter1 <= 0) {
counter1 = 59;
timer1--;
}
if (timer1 <= 0) {
t.Stop ();
}
try{
v2.Text = timer1+ ":"+ counter1;
}
catch( Exception e8)
{
AlertDialog.Builder builder = new AlertDialog.Builder (this);
builder.SetMessage (e8.Message);
builder.Create().Show();
}
}
` I get this error : e8.Message "Only the original thread that created a view hierarchy can touch its views." string
how can i fix this ?
Invoke your code to the UI thread instead.
// Get the camera
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
// Turning On flash
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
My code that gets involved with flash on is above, any suggestions,how to make the flash not open the moment my app opens but only when i press the button
I would guess you are calling turnFlashOn() in onCreate() and don't have a button ClickListener. But it is possible you could be calling turnFlashOn() in onStart() or onResume().
To have it turn on with a button press you should do something like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_view);
flashlight_button = (Button) findViewById(R.id.your_button);
flashlight_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getCamera();
turnOnFlash();
}
});
}
I built an open source flashlight for Android that you can check out at Flashlight by Joe github.
I have a slider in my windows phone 7.1 project. When manipulated, this slider fires an event which starts a background worker to performs several trigonometric operations.
If I move the cursor on the slider, I have a certain delay in the response although I have implement background worker cancelAsync method in manipulationstarted event, I would like more responsiveness, how can I achieve this?
Code:
private void sliderCosinus_ManipulationStarted(object sender,ManipulationStartedEventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
{
bw.CancelAsync(); // Cancel the asynchronous operation.
}
}
private void sldCosinus_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
try
{
Value = Convert.ToInt32(sldCosinus.Value) * 10;
}
catch
{
// errore message here
}
finally
{
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if ((worker.CancellationPending == true))
{
e.Cancel = true;
}
else
{
Dispatcher.BeginInvoke(() => app.IsEffectApplied=TrigonometricTrans()
// TrigonomtriecTrans calculate sin and cosinus for every pixel in image
}
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// progress bar here
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
//this.tbProgress.Text = "Canceled!";
}
else if (!(e.Error == null))
{
//this.tbProgress.Text = ("Error: " + e.Error.Message);
}
else
{
DoubleBufferToScreen();
}
}