Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to do two functions at the same time ?  RSS feed

 
Avin Sinanan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am wondering how do you do two fucntions at the same time.. The code I've included does the following -
It creates a button and the buttons moves to a locations by using a "for" loop and a "setBounds" expression.
Then it creates a second button and moves it to a diffrent location by using the same method as above. The problem is that the second button has to wait till the first button is finished moving.
How do I get both buttons to move at the same time. I need a method that will allow me to move 3 or even 10 buttons at the same time with all buttons having diffrent motion paths.
Here is the code... any help would be greatly appreciated. thanks in advance.. yours respctufully Avin Sinanan
CODE -->
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
class TwoFunctions
{
public static void main(String[] args)
{
Frame frame = new JFrame();
JPanel pane = new JPanel();
pane.setLayout(null);

frame.setSize(600,600);
frame.setVisible(true);

JButton button1 = new JButton("One");
JButton button2 = new JButton("Two");

for(int i=0 ; i<400 ; i++)
{
button1.setBounds(i,50,70,70);
pane.add(button1);
frame.getContentPane().add(pane);
pane.updateUI();

}


for(int j = 0 ; j<500 ; j++)
{
button2.setBounds(200,j,70,70);
pane.add(button2);
frame.getContentPane().add(pane);
pane.updateUI();

}

}
}
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Avin,
If you just want to move two buttons you could try this:


If you might want to do this with several buttons in the future you'd probably want to look into threading - check out the sun java Tutorial on the SwingWorker class ( a handy implementation of threads specially for swing )

Tying up your application for a loop of 500 steps is not something you should be doing anyway without threads as it will make your interface seem unresponsive.
Hope that helps,
Bye,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!