Results 1 to 15 of 15
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default setText is not firing...! Why?

    Hi,
    Heres my setup:

    3 classes:

    main
    Java Code:
    Thread timer1 = new Thread(new GameTimer1());
    		timer1.start();
    Timer
    Java Code:
    public class GameTimer1 implements Runnable{
    
    	GameForm gf1 = new GameForm();
    	
    	
    		public void run() {
    			int i=0;
    			while (true)
    			{System.out.println(i++);
    	           try { 
    	        	   gf1.changeNoteFromTimer();
    	        	   Thread.sleep(1000);}
    	           catch(InterruptedException e){};
    
    	           }
    			}
    		}
    Gameform:
    Java Code:
    int secondsToChange=0;
    	 public void changeNoteFromTimer()
    	 {
    		 
    		[B][COLOR="Lime"] System.out.print(" *r"+secondsToChange+"* ");[/COLOR]
                    [COLOR="Red"] noteLabel.setText("dd "+secondsToChange);[/COLOR][/B]
    secondsToChange++;
    	 }
    The part in green runs perfectly, but the part in red does not execute... why?

    Thanks!
    R
    Last edited by N00Bie; 02-20-2011 at 08:16 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If the code in green is executing, you can be sure that the code in red is executing as well. Your problem is likely one of reference -- which JLabel is getting its text changed? The one being displayed? No. Likely that of another object that is not being displayed.

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I did not post the entire form code as its big and messy, but here ya go :D

    No JLabel is getting changed on the form...

    Java Code:
    import java.awt.EventQueue;
    
    
    public class GameForm extends JFrame {
    
    	private JPanel contentPane;
    	private final ButtonGroup bGroup1 = new ButtonGroup();
    
        Game_Sub1 gs = new Game_Sub1();
        int lastNo=gs.returnRandNo();
        String presentScaleNote="";
    	
    
        /*
        public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					GameForm frame = new GameForm();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    */
    	/**
    	 * Create the frame.
    	 */
    	public GameForm() {
    		setTitle("A Major ver 0.1");
    		
    			gs.setToggleSwitch(0);
        	
    			JFrame frame = new JFrame("Test2");
    		      frame.getContentPane().add(new Test2KeyBindings());
    		
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		GridBagLayout gbl_contentPane = new GridBagLayout();
    		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    		contentPane.setLayout(gbl_contentPane);
    		
    		switchChkbox = new JCheckBox("Switch");
    		switchChkbox.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent arg0) {
    				//System.out.println(arg0);
    			doRyanSwitch();
    			}
    		});
    		
    		timeLabel = new JLabel("Time: ");
    		GridBagConstraints gbc_timeLabel = new GridBagConstraints();
    		gbc_timeLabel.anchor = GridBagConstraints.WEST;
    		gbc_timeLabel.gridwidth = 2;
    		gbc_timeLabel.insets = new Insets(0, 0, 5, 5);
    		gbc_timeLabel.gridx = 1;
    		gbc_timeLabel.gridy = 0;
    		contentPane.add(timeLabel, gbc_timeLabel);
    		
    		levelLabel = new JLabel("Level: ");
    		GridBagConstraints gbc_levelLabel = new GridBagConstraints();
    		gbc_levelLabel.anchor = GridBagConstraints.WEST;
    		gbc_levelLabel.gridwidth = 2;
    		gbc_levelLabel.insets = new Insets(0, 0, 5, 5);
    		gbc_levelLabel.gridx = 3;
    		gbc_levelLabel.gridy = 0;
    		contentPane.add(levelLabel, gbc_levelLabel);
    		
    		correctRunLabel = new JLabel("Correct: ");
    		GridBagConstraints gbc_correctRunLabel = new GridBagConstraints();
    		gbc_correctRunLabel.anchor = GridBagConstraints.WEST;
    		gbc_correctRunLabel.gridwidth = 2;
    		gbc_correctRunLabel.insets = new Insets(0, 0, 5, 5);
    		gbc_correctRunLabel.gridx = 5;
    		gbc_correctRunLabel.gridy = 0;
    		contentPane.add(correctRunLabel, gbc_correctRunLabel);
    		GridBagConstraints gbc_switchChkbox = new GridBagConstraints();
    		gbc_switchChkbox.gridwidth = 3;
    		gbc_switchChkbox.insets = new Insets(0, 0, 5, 0);
    		gbc_switchChkbox.gridx = 6;
    		gbc_switchChkbox.gridy = 2;
    		contentPane.add(switchChkbox, gbc_switchChkbox);
    		
    		noteLabel = new JLabel("Music note: ");
    		noteLabel.setHorizontalAlignment(SwingConstants.LEFT);
    		noteLabel.setFont(new Font("Sylfaen", Font.BOLD | Font.ITALIC, 30));
    		GridBagConstraints gbc_noteLabel = new GridBagConstraints();
    		gbc_noteLabel.anchor = GridBagConstraints.WEST;
    		gbc_noteLabel.insets = new Insets(0, 0, 5, 0);
    		gbc_noteLabel.gridx = 1;
    		gbc_noteLabel.gridy = 3;
    		gbc_noteLabel.gridwidth = 8;
    		contentPane.add(noteLabel, gbc_noteLabel);
    		
    		radio1 = new JRadioButton("Sharps");
    		radio1.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent arg0) {
    				
    				/* *** old way of doing things ***
    				String[] scale;
                	scale=gs.sharpsOrFlats(1);
                	noteLabel.setText( "Music note: "+scale[lastNo]+ " ");*/
    				noteLabel.setText( "Music note: "+gs.returnMusicNote(1,lastNo)+ " "); // new way.. ***
    				
    				
    			}
    		});
    		
    
    		
    		 answerLabel = new JLabel("Answer:");
    		 answerLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
    		 GridBagConstraints gbc_answerLabel = new GridBagConstraints();
    		 gbc_answerLabel.anchor = GridBagConstraints.WEST;
    		 gbc_answerLabel.gridwidth = 4;
    		 gbc_answerLabel.insets = new Insets(0, 0, 5, 5);
    		 gbc_answerLabel.gridx = 1;
    		 gbc_answerLabel.gridy = 5;
    		 contentPane.add(answerLabel, gbc_answerLabel);
    		bGroup1.add(radio1);
    		radio1.setSelected(true);
    		GridBagConstraints gbc_radio1 = new GridBagConstraints();
    		gbc_radio1.anchor = GridBagConstraints.WEST;
    		gbc_radio1.gridwidth = 2;
    		gbc_radio1.insets = new Insets(0, 0, 5, 5);
    		gbc_radio1.gridx = 5;
    		gbc_radio1.gridy = 5;
    		contentPane.add(radio1, gbc_radio1);
    		
    		
    
    		
    			System.out.println(gs.returnMusicNote(getRadioValue(),lastNo));
    
    			noteLabel.setText( "Music note: "+gs.returnMusicNote(getRadioValue(),lastNo)+ " ");
    			gs.setLastNote(getRadioValue(),lastNo);
    		
    		
    		
    		
    		
    		
    		
    		
    		radio2 = new JRadioButton("Flats");
    		radio2.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent arg0) {
        			noteLabel.setText( "Music note: "+gs.returnMusicNote(2,lastNo)+ " ");
    			}
    		});
    		bGroup1.add(radio2);
    		GridBagConstraints gbc_radio2 = new GridBagConstraints();
    		gbc_radio2.anchor = GridBagConstraints.WEST;
    		gbc_radio2.gridwidth = 2;
    		gbc_radio2.insets = new Insets(0, 0, 5, 5);
    		gbc_radio2.gridx = 5;
    		gbc_radio2.gridy = 6;
    		contentPane.add(radio2, gbc_radio2);
    		
    // *** Here we put in the window bindings code 
    //(Note we added keybindings to the frame in the beginning of this constructor)
    		
    		final String[] buttonScale = gs.returnNotesOrNumbersArray(2);
    
    		/// Part 1 of 3: make the abstract methods/actions for all buttons
    		AbstractAction action = new AbstractAction(buttonScale[0]) {
                public void actionPerformed(ActionEvent e) {
                   System.out.println("button " + buttonScale[0] + " pressed");
                }
             };
             
             AbstractAction action2 = new AbstractAction(buttonScale[1]) {
                 public void actionPerformed(ActionEvent e) {
                    System.out.println("button " + buttonScale[1] + " pressed");
                 }
              };
    		
              
              AbstractAction action3 = new AbstractAction(buttonScale[2]) {
                  public void actionPerformed(ActionEvent e) {
                     System.out.println("button " + buttonScale[2] + " pressed");
                  }
               };
     		
               
               AbstractAction action4 = new AbstractAction(buttonScale[3]) {
                   public void actionPerformed(ActionEvent e) {
                      System.out.println("button " + buttonScale[3] + " pressed");
                   }
                };
      		
                
                AbstractAction action5 = new AbstractAction(buttonScale[4]) {
                    public void actionPerformed(ActionEvent e) {
                       System.out.println("button " + buttonScale[4] + " pressed");
                    }
                 };
       		
                 
                 AbstractAction action6 = new AbstractAction(buttonScale[5]) {
                     public void actionPerformed(ActionEvent e) {
                        System.out.println("button " + buttonScale[5] + " pressed");
                     }
                  };
        		
                  
                  AbstractAction action7 = new AbstractAction(buttonScale[6]) {
                      public void actionPerformed(ActionEvent e) {
                         System.out.println("button " + buttonScale[6] + " pressed");
                      }
                   };
         		
                           
              
           		/// Part 2 of 3: add the action,action2, action3 etc to the buttons constructors  
              
              
    		but1 = new JButton(action);
    		//but1.setMnemonic('1');
    		but1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				changeLabel(0); // Because the notes array starts from zero the button is -1
    			}
    		});
    		
    		int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; 
    // *** the below 9 lines of code are almost identical for all buttons and should be copied
    
    		
    		
    		JLabel lblNewLabel = new JLabel("\"A\",\"B\",\"C#\",\"D\",\"E\",\"F#\",\"G#\"");
    		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    		gbc_lblNewLabel.gridwidth = 5;
    		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
    		gbc_lblNewLabel.gridx = 1;
    		gbc_lblNewLabel.gridy = 7;
    		contentPane.add(lblNewLabel, gbc_lblNewLabel);
    		GridBagConstraints gbc_but1 = new GridBagConstraints();
    		gbc_but1.insets = new Insets(0, 0, 0, 5);
    		gbc_but1.gridx = 1;
    		gbc_but1.gridy = 8;
    		contentPane.add(but1, gbc_but1);
    		
    
    		
    		 but2 = new JButton(action2);
    		but2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				changeLabel(1); // Because the notes array starts from zero the button is -1
    			}
    		});
    
    		GridBagConstraints gbc_but2 = new GridBagConstraints();
    		gbc_but2.insets = new Insets(0, 0, 0, 5);
    		gbc_but2.gridx = 2;
    		gbc_but2.gridy = 8;
    		contentPane.add(but2, gbc_but2);
    		
    		 but3 = new JButton(action3);
    		but3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				changeLabel(2); // Because the notes array starts from zero the button is -1
    			}
    		});
    		GridBagConstraints gbc_but3 = new GridBagConstraints();
    		gbc_but3.insets = new Insets(0, 0, 0, 5);
    		gbc_but3.gridx = 3;
    		gbc_but3.gridy = 8;
    		contentPane.add(but3, gbc_but3);
    		
    		 but4 = new JButton(action4);
    		but4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				changeLabel(3); // Because the notes array starts from zero the button is -1
    			}
    		});
    		GridBagConstraints gbc_but4 = new GridBagConstraints();
    		gbc_but4.insets = new Insets(0, 0, 0, 5);
    		gbc_but4.gridx = 4;
    		gbc_but4.gridy = 8;
    		contentPane.add(but4, gbc_but4);
    		
    		 but5 = new JButton(action5);
    		but5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				changeLabel(4); // Because the notes array starts from zero the button is -1
    			}
    		});
    		GridBagConstraints gbc_but5 = new GridBagConstraints();
    		gbc_but5.insets = new Insets(0, 0, 0, 5);
    		gbc_but5.gridx = 5;
    		gbc_but5.gridy = 8;
    		contentPane.add(but5, gbc_but5);
    		
    		 but6 = new JButton(action6);
    		but6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				changeLabel(5); // Because the notes array starts from zero the button is -1
    			}
    		});
    		GridBagConstraints gbc_but6 = new GridBagConstraints();
    		gbc_but6.insets = new Insets(0, 0, 0, 5);
    		gbc_but6.gridx = 6;
    		gbc_but6.gridy = 8;
    		contentPane.add(but6, gbc_but6);
    		
    		 but7 = new JButton(action7);
    		but7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				changeLabel(6); // Because the notes array starts from zero the button is -1
    			}
    		});
    		GridBagConstraints gbc_but7 = new GridBagConstraints();
    		gbc_but7.insets = new Insets(0, 0, 0, 5);
    		gbc_but7.gridx = 7;
    		gbc_but7.gridy = 8;
    		contentPane.add(but7, gbc_but7);
    		
    		
    		
    		// Part 3 of 3 of Key bindings: Do for all 7 buttons
    		
    		
    		// but1
            InputMap inputMap = but1.getInputMap(condition);
            ActionMap actionMap = but1.getActionMap();
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_0+1, 0), buttonScale[0]);
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1 , 0), buttonScale[0]);
            
            actionMap.put(buttonScale[0], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
             	   but1.doClick();
                }
             });
    		
    		// but2
    		
    		InputMap inputMap2 = but2.getInputMap(condition);
            ActionMap actionMap2 = but2.getActionMap();
            inputMap2.put(KeyStroke.getKeyStroke(KeyEvent.VK_0+2, 0), buttonScale[1]);
            inputMap2.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2 , 0), buttonScale[1]);
            
            actionMap2.put(buttonScale[1], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
             	   but2.doClick();
                }
             });
    		
    		
    		
    		// but3
    		
    		InputMap inputMap3 = but3.getInputMap(condition);
            ActionMap actionMap3 = but3.getActionMap();
            inputMap3.put(KeyStroke.getKeyStroke(KeyEvent.VK_0+3, 0), buttonScale[2]);
            inputMap3.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD3 , 0), buttonScale[2]);
            
            actionMap3.put(buttonScale[2], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
             	   but3.doClick();
                }
             });
    		
    		
    		
    		// but4
    		
    		InputMap inputMap4 = but4.getInputMap(condition);
            ActionMap actionMap4 = but4.getActionMap();
            inputMap4.put(KeyStroke.getKeyStroke(KeyEvent.VK_0+4, 0), buttonScale[3]);
            inputMap4.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD4 , 0), buttonScale[3]);
            
            actionMap4.put(buttonScale[3], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
             	   but4.doClick();
                }
             });
    		
    		
    		
    		// but5
    		
    		InputMap inputMap5 = but5.getInputMap(condition);
            ActionMap actionMap5 = but5.getActionMap();
            inputMap5.put(KeyStroke.getKeyStroke(KeyEvent.VK_0+5, 0), buttonScale[4]);
            inputMap5.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD5 , 0), buttonScale[4]);
            
            actionMap5.put(buttonScale[4], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
             	   but5.doClick();
                }
             });
    		
    		
    		
    		// but6
    		
    		InputMap inputMap6 = but6.getInputMap(condition);
            ActionMap actionMap6 = but6.getActionMap();
            inputMap6.put(KeyStroke.getKeyStroke(KeyEvent.VK_0+6, 0), buttonScale[5]);
            inputMap6.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD6 , 0), buttonScale[5]);
            
            actionMap6.put(buttonScale[5], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
             	   but6.doClick();
                }
             });
    		
    		
    		
    		// but7
    		
    		InputMap inputMap7 = but7.getInputMap(condition);
            ActionMap actionMap7 = but7.getActionMap();
            inputMap7.put(KeyStroke.getKeyStroke(KeyEvent.VK_0+7, 0), buttonScale[6]);
            inputMap7.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD7 , 0), buttonScale[6]);
           // inputMap7.put(KeyStroke.getKeyStroke(KeyEvent.VK_CAPS_LOCK  , 0), buttonScale[6]);
            inputMap7.put(KeyStroke.getKeyStroke(KeyEvent.VK_A  , 0), buttonScale[6]);
             
            
            
            actionMap7.put(buttonScale[6], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
             	   but7.doClick();
                }
             });
    		
    		
    		
    		
    		
    		
    		
    		
    		
    	}
    	
    	 private javax.swing.JLabel noteLabel;
    	 private javax.swing.JLabel answerLabel;
    	 private javax.swing.JRadioButton radio1;
    	 private javax.swing.JRadioButton radio2;
    	 private javax.swing.JButton but1,but2,but3,but4,but5,but6,but7;
    	 private JCheckBox switchChkbox;
    	 private JLabel timeLabel;
    	 private JLabel levelLabel;
    	 private JLabel correctRunLabel;
    	 
     
    	 
    	 
    	 public void doRyanSwitch(){
    		 if(switchChkbox.isSelected()){
    			 
    		 
    			 gs.setToggleSwitch(1);
    			 
    			 if(gs.getToggleSwitch()==true){
    			 String[] theScale=gs.returnNotesOrNumbersArray(getRadioValue()-1);
    			 
    			 	but1.setText(theScale[0]);
    			 	but2.setText(theScale[1]);
    			 	but3.setText(theScale[2]);
    			 	but4.setText(theScale[3]);
    			 	but5.setText(theScale[4]);
    				but6.setText(theScale[5]);
    				but7.setText(theScale[6]);
    				noteLabel.setText("Note: "+(lastNo+1)+" ");
    			 	}
    			 }
    		 else{
    			 gs.setToggleSwitch(0);
    			 if(gs.getToggleSwitch()==false){
    			 String[] theScale=gs.returnNotesOrNumbersArray(2);
    			 
    				but1.setText(theScale[0]);
    				but2.setText(theScale[1]);
    				but3.setText(theScale[2]);
    				but4.setText(theScale[3]);
    				but5.setText(theScale[4]);
    				but6.setText(theScale[5]);
    				but7.setText(theScale[6]);
    				noteLabel.setText("Note: "+gs.returnMusicNote(getRadioValue(), lastNo)+" ");
    			 }
    		 	}
    	 }
    	 
    	
    	 private int getRadioValue() {
    		 boolean b;
    		 b = 	radio1.isSelected();
    		 
    		   if(b==true)
    	            return 1;
    	        else
    	            return 2;
    		}
    	
    	 
    	 
    	 
    	[COLOR="Red"] int secondsToChange=0;
    	 public void changeNoteFromTimer()
    	 {
    		 
    		 System.out.print(" *r"+secondsToChange+"* ");/*
    	 noteLabel.setText("dd");
    		 if(secondsToChange>=3){System.out.print(" *here* ");
    			 noteLabel.setText("ss");//changeLabel(10); // basically just send a wrong answer
    			 secondsToChange=0;
    		 }else{secondsToChange++;}
    		*/ answerLabel.setText("dd");secondsToChange++;
    
    	 }
    [/COLOR]
        public void changeLabel(int i) {
        	
        	int randNote = gs.returnRandNo();
        	int randNote2 = gs.returnRandNo(lastNo);
        	String x;
        	
        	if(gs.getToggleSwitch()==true){
        		System.out.println(gs.getLastNote()+" "+i+ "  - "+lastNo);
        		if(i==lastNo){
           		 x=gs.getLastNote()+"-BINGO!";
           		 gs.setLastNote(getRadioValue(),randNote2);
           		}else{
           		 x=gs.getLastNote();
           		 gs.setLastNote(getRadioValue(),randNote2);
           		} 
               noteLabel.setText("Note: "+(randNote2+1)+" ");//( "-:"+i+ " Last no: "+lastNo+ ": ");
           	answerLabel.setText("Answer: "+x);
           	lastNo = randNote2;
        	}else{
        	if(i==lastNo){
        		 x=gs.getLastNote()+"-BINGO!";
        		 gs.setLastNote(getRadioValue(),randNote2);
        		}else{
        		 x=gs.getLastNote();
        		 gs.setLastNote(getRadioValue(),randNote2);
        		}
        		
        		if(i==10){System.out.println("in RRRR"+i);
        			noteLabel.setText("RYyyyyyyyyyyyy");
        		}
        		
        		
            noteLabel.setText("Note: "+gs.returnsNote(getRadioValue(),randNote2)+" ");//( "-:"+i+ " Last no: "+lastNo+ ": ");
        	answerLabel.setText("Answer: "+x);
        	lastNo = randNote2;
        }
    	}
    
    }

  4. #4
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Edited the above to make more sense by marking the problem code in red.

    Seems like everything is working fine as long as I dont call it from the timer script, if I call it from the timer script it goes to hell...

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by N00Bie View Post
    No JLabel is getting changed on the form...
    But not the same form:

    Java Code:
    public class GameTimer1 implements Runnable{
    
    	[color="red"][b]GameForm gf1 = new GameForm();[/b][/color]
    	
    	
    		public void run() {
    			int i=0;
    			while (true)
    			{System.out.println(i++);
    	           try { 
    	        	   gf1.changeNoteFromTimer();
    	        	   Thread.sleep(1000);}
    	           catch(InterruptedException e){};
    
    	           }
    			}
    		}

    This line:
    GameForm gf1 = new GameForm();

    Creates a new GameForm, whose noteLabel gets set. Is this the same GameForm as the one that's being displayed? I'll bet not. Again, it's a problem of wrong reference. You need to get a reference to the actual GameForm being displayed. Just because it's an object of the same class doesn't mean that a call to a method in one object will magically change a property in another object. Again, pass a reference into this class via a parameter such as via a constructor parameter.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, you should be using a Swing Timer. This way all changes on your GUI are done on the EDT.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Note, that if I'm wrong, that you only have one GameForm object, then your problem may be a concurrency issue. It's hard to say without all the code.

  8. #8
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    (Multiple new questions)


    Ok, changed my main program to this:

    Java Code:
    GameForm f = new GameForm();
    		f.setVisible(true);
    		
    		Thread timer1 = new Thread(new GameTimer1(f));
    		timer1.start();
    Changed my timer class to this:
    Java Code:
    GameTimer1(GameForm Gf1){
    	//GameForm gf1 = new GameForm();
    	}
    I had to move my run() method outside of the constructor... so how do I now use the Gf1 reference in the run?

    and finally:
    Also, you should be using a Swing Timer. This way all changes on your GUI are done on the EDT.
    I have never used/heard of a swing timer... a link or example please?

    Thanks!

  9. #9
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Note, that if I'm wrong, that you only have one GameForm object, then your problem may be a concurrency issue. It's hard to say without all the code.
    I updated the gameform trouble part with this bit:
    Java Code:
    		answerLabel.setText("dd");
    		secondsToChange++;
    		System.out.print(noteLabel.getText());
    		noteLabel.setText("rrrrr");
    		System.out.print(noteLabel.getText());
    And it seems to be setting and getting what I put in... but it does not update the form with the changes... is there some kind of repaint/refresh method that I need to call to update the form again?

    I can send you all the code, its a bit much to add here, want me to upload it in a zip?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by N00Bie View Post
    I can send you all the code, its a bit much to add here, want me to upload it in a zip?
    Usually I say no, but yeah, go ahead and jar it (with source!) and then change the extension from jar to zip and upload it.

  11. #11
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Here ya go:
    elxotica.com/src.rar

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  13. #13
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I don't have zip making software, only winrar.
    Click that link its from a freinds site and its safe as can be ;)

    http://elxotica.com/src.rar

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    OK, what if you do this to your GameTimer1 class :

    Java Code:
    class GameTimer1 implements Runnable {
       GameForm gf1;
    
       // get the GameForm object from constructor parameter and set class field
       GameTimer1(GameForm gf1) {
          this.gf1 = gf1; //!!
       }
    
       //.... more code


    But again better is to use a Swing Timer. e.g.,

    Java Code:
    public class TimerListener implements ActionListener {
       private GameForm gForm;
       private int counter = 0;
       
       public TimerListener(GameForm gForm) {
          this.gForm = gForm;
       }
       
       public void actionPerformed(ActionEvent e) {
          gForm.changeNoteFromTimer();
       }
    }

    And to use it...

    Java Code:
    public class Game {
       private static final int TIMER_DELAY = 1000;
    
       public static void main(String[] args) {
          GameForm f = new GameForm();
          f.setVisible(true);
          //!! be sure to import javax.swing.Timer
          Timer timer = new Timer(TIMER_DELAY, new TimerListener(f));
          timer.start();
       }
    }

  15. #15
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    class GameTimer1 implements Runnable {
    GameForm gf1;

    // get the GameForm object from constructor parameter and set class field
    GameTimer1(GameForm gf1) {
    this.gf1 = gf1; //!!
    }

    //.... more code
    That worked!

    Thanks!

    Also thanks for the Swing Timer, I think I'll check it out later for now
    1. the code you gave me works!
    2. I want to play around with threads and understand them more as this was the whole point of this "test project" that I created.
    Other than me few people would be interested in this project :o
    3. Will be adding more test threads to try new things on the form, I am not sure how using multiple Swing Timers would effect the form

    Thanks again for taking the time to help me, I really appreciate it!

    (++Rep'ed)

Similar Threads

  1. problem with JLabel.setText();
    By nonabhai in forum AWT / Swing
    Replies: 5
    Last Post: 10-09-2010, 04:44 AM
  2. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 05:29 AM
  3. setText in JTextArea
    By hero in forum AWT / Swing
    Replies: 1
    Last Post: 10-12-2009, 09:38 PM
  4. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 10:34 PM
  5. setText in event
    By sniezna.stopa in forum SWT / JFace
    Replies: 0
    Last Post: 06-20-2008, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •