#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2

    Threads and actions


    Hi!
    I want to make a simple program to learn threads and actions.
    I wrote a code that creates frame with 2 progress bars and 2 buttons on it:
    Code:
    class MyFrame extends JFrame{
    
    	private GridBagLayout layout;
    	private JProgressBar bar1, bar2;
    	private JButton btn1, btn2, btn3, btn4;
    	private GridBagConstraints gbcbar1, gbcbar2, gbcbtn1, gbcbtn2, gbcbtn3, gbcbtn4;  
    	private Dimension dimbar, dimbtn;
    	 
    	
    	public MyFrame(){
    		super("Thread test 1.0");
    		layout = new GridBagLayout();
    		final JPanel cpane = new JPanel(layout);
    		setContentPane(cpane);		
    		bar1 = new JProgressBar();
    		bar2 = new JProgressBar();
    		btn1 = new JButton("1st Thread Go");
    		btn2 = new JButton("2nd Thread Go");
    		btn3 = new JButton("1st Pause/Unpause");
    		btn4 = new JButton("2nd Pause/Unpause");
    		
    		dimbar = new Dimension();
    		dimbar.width = 200;
    		dimbar.height = 20;
    		bar1.setPreferredSize(dimbar);
    		bar2.setPreferredSize(dimbar);
    		
    		gbcbar1 = new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.CENTER,
    				GridBagConstraints.BOTH,new Insets(8,8,8,8),0,0);
    		gbcbar2 = new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.CENTER,
    				GridBagConstraints.BOTH,new Insets(8,8,8,8),0,0);
    		gbcbtn1 = new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.CENTER,
    				GridBagConstraints.BOTH,new Insets(8,8,8,8),0,0);
    		gbcbtn2 = new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.CENTER,
    				GridBagConstraints.BOTH,new Insets(8,8,8,8),0,0);
    		gbcbtn3 = new GridBagConstraints(0,2,1,1,0,0,GridBagConstraints.CENTER,
    				GridBagConstraints.BOTH,new Insets(8,8,8,8),0,0);
    		gbcbtn4 = new GridBagConstraints(1,2,1,1,0,0,GridBagConstraints.CENTER,
    				GridBagConstraints.BOTH,new Insets(8,8,8,8),0,0);
    	
    		layout.setConstraints(bar1, gbcbar1);
    		layout.setConstraints(bar2, gbcbar2);
    		layout.setConstraints(btn1, gbcbtn1);
    		layout.setConstraints(btn2, gbcbtn2);
    		layout.setConstraints(btn3, gbcbtn3);
    		layout.setConstraints(btn4, gbcbtn4);
    	
    		cpane.add(bar1);
    		cpane.add(bar2);
    		cpane.add(btn1);
    		cpane.add(btn2);
    		cpane.add(btn3);
    		cpane.add(btn4);
    		
    		btn1.addActionListener(new ActionStart(bar1));
    		btn3.addActionListener(new ActionStop(bar1));
    		
    		setSize(500,200);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setResizable(false);
    	}
    }
    After pressing 1st button creates the Thread :
    Code:
    class MyThread extends Thread{
    	private JProgressBar jpb;
    	
    	MyThread(JProgressBar pb){
    		jpb = pb;
    	}
    	
    	public void run(){
    		try{
    			for(int i=0; i<25; i++){
    				Thread.sleep(100);
    				jpb.setValue(i*5);
    			}
    		}catch(Exception e){
    			JOptionPane.showMessageDialog(new JFrame(), "Thread was Corrupted", 
    					"Error",JOptionPane.ERROR_MESSAGE);
    		} finally {
    			JOptionPane.showMessageDialog(new JFrame(), "Thread was Ended"+this, 
    					"Dialog",JOptionPane.DEFAULT_OPTION);
    		}
    	}
    	
    }
    after pressing second button thread must stop. but it can't be stoped, because i don't know how to get MyThread from my Actions class:
    Code:
    class ActionStart implements ActionListener{
    	
    	JProgressBar jpb;
    	
    	ActionStart(JProgressBar pb){
    		jpb = pb;
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		new MyThread(jpb).start();
    		
    	}	
    }
    class ActionStop implements ActionListener{
    	
    	JProgressBar jpb;
    	
    	ActionStop(JProgressBar pb){
    		jpb = pb;
    	}
    	
    	@SuppressWarnings("deprecation")
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		new MyThread(jpb).stop();
    		
    	}	
    }
    And my Root class :
    Code:
    class Root{
    	
    	public static void main(String[] args){
    		new MyFrame();
    	}
    	
    }
    So the question is : How to make my thread be polite ? 2nd action can't get him, because 1 action creates it and the other should stop existing thread. But other button don't know about that thread.
    P.S. I can't get use to all this Listeners in java.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    For a method in one class to get to the methods of another class, it needs a reference to that other class.
    Pass a reference to the class that wants to call the methods.

    What class needs the reference?
    How can that class get a reference?
    Last edited by NormR; October 27th, 2013 at 06:52 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    I've found a way out. I declared each of my actions as a static. Here is it:
    Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.EventListener;
    
    import javax.swing.JProgressBar;
    
    class Actions{
    	public static MyThread T;
    	
    static class ActionStart implements ActionListener{
    	
    	
    	JProgressBar jpb;
    	
    	ActionStart(JProgressBar pb){
    		jpb = pb;
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		T = new MyThread(jpb);
    		T.start();
    		
    	}	
    }
    static class ActionStop implements ActionListener{
    	
    	JProgressBar jpb;
    	
    	ActionStop(JProgressBar pb){
    		jpb = pb;
    	}
    	
    	@SuppressWarnings("deprecation")
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		T.stop();
    		
    	}	
    }
    }
    So new Actions class gets created thread and leaves it as a public Field. So each action can freely use the thread !
    But now i want to know : is there much more accurate way to implement my programm ( or Actions)? Or this is the only way?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Using static is a poor way out.
    Is there a way to get the reference to the class to the location where you want to call its methods?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    I'm not sure that i clearly understand you .
    My thread creates in 1st method of "Actions". There i'm also creating the refrence of the class . So this calss is available in my "Actions" class. All the managment of my thread is defined in actions. Here is how

    button.addsAnAction( instance of myActions.someaction( here is bar , that will show the process of the thread))
    But instance of "myActions.someaction" doesnt get the created thread , it creates the thread .
    I couldn't find more appropriate way than create static representation for Each my action , because if first action creates the thread , the secnod action need new ActionListener implementation. Following this i need to create a new class which wouldn't know about the thread , but i need it to do another action with the same thread.

    I hope you've understood me .
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    What about making the two Threads be instance variables in ActionStart and ActionStop. Then instead of just creating a new instance of each of these MyFrame, name the instance. That way you can call the instance variables.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    It the reference to the MyThread class is created in the ActionStart class, then a reference to the ActionStart class should be passed to the ActionStop class so it can call a method in ActionStart to get the reference to the MyThread class object.
    Perhaps the method should do the stop in ActionStart instead of in ActionStop.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    I'm understanding what you, guys, whant to say to me. The thing is i'm giving the refrence of MyThree to class, that keeps ActionStart and ActionStop. But i don't understand why i should stop in ActionStart?
    Last edited by FAQer; October 29th, 2013 at 04:47 AM.
  16. #9
  17. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by FAQer
    I'm understanding what you, guys, whant to say to me. The thing is i'm giving the refrence of MyThree to class, that keeps ActionStart and ActionStop. But i don't understand why i should stop in ActionStart?
    One issue to be aware of is that the stop method is deprecated.

IMN logo majestic logo threadwatch logo seochat tools logo