Results 1 to 6 of 6
  1. #1
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Issue using Timer to constantly refresh Information

    I have a program with several tabs, all of which modify a "total" variable. The only problem with it is that if I make a change on one tab and then switch tabs, it will not reflect until I make a change on the tab that I switched to.

    To remedy this I am attempting to use a Timer to constantly call the textArea.setText(orderTotal); method, however it is not succeeding.

    In main I am using the following code
    Java Code:
     Timer timer;
                timer = new Timer(1, new TimerListener());
                timer.start();
    and the timerlistener class
    Java Code:
        public class TimerListener implements ActionListener
    		{	public void actionPerformed (ActionEvent event)
    				{	textArea.setText(orderTotal);
    				}
    		}
    The thing is, I am not getting any syntax errors and if i put a system.out.printline, it immediatly floods console with whatever i put in the S.O.P, so it is clearly calling the timer code as I want. So why is it not working?

    For reference, here is the code for the only tab that I have tried to implement the timer refresh in
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.*;
    
    public class pizza extends JPanel implements ActionListener 
    {	static int large = 5;
    	static int medium = 4;
    	static int small = 3;
    	static String orderTotal;
    	static String pizza, side, drink;
    	
    	public JTextArea textArea = new JTextArea();
    	public float total = 0;
    	public JCheckBox bacon = new JCheckBox("bacon");
        public pizza()
       		{	//String output = "cost";
        	wipe();
        	textArea.setText(orderTotal);
        	
        	
        		setLayout (new BorderLayout());
                setBackground (Color.white);
                
                JPanel b1 = new JPanel(new GridLayout(0,1));
                JButton b2 = new JButton ("Top");
                JPanel b3 = new JPanel(new GridLayout(0,1));
                
                JRadioButton large = new JRadioButton("Large");
                JRadioButton medium = new JRadioButton("Medium");
                JRadioButton small = new JRadioButton("Small");
               
                ButtonGroup size = new ButtonGroup();
                size.add(large);
                size.add(medium);
                size.add(small);
                
                b3.add(textArea);
                            
                large.addActionListener(this);
                medium.addActionListener(this);
                small.addActionListener(this);
                bacon.addActionListener(this);
               
                b1.add(large);
                b1.add(medium);
                b1.add(small);
                b1.add(bacon);
               
                add (b1, BorderLayout.CENTER);
                add (b2, BorderLayout.NORTH);
                add (b3, BorderLayout.EAST);
               // textArea.setText(orderTotal);
                
                //test timer
                Timer timer;
                timer = new Timer(1, new TimerListener());
                timer.start();
         	}
        
       public void actionPerformed(ActionEvent e) 
       		{	String input=e.getActionCommand();
       			//begin radio buttons
       			if (input.equalsIgnoreCase("Small"))
       				{	total=0;
       					total+=5;
       					deselect();
       					writePizza(total);
       					total();
       				
       					//testing output code
       					System.out.println(orderTotal);
       				}
       			
       			else if (input.equalsIgnoreCase("Medium"))
    				{	total=0;
    					total+=7;
    					deselect();
       					writePizza(total);
       					total();
       					
       					//testing output code
       					System.out.println(orderTotal);
    				}
    			
       			else if (input.equalsIgnoreCase("Large"))
    				{	total=0;
    					total+=10;
    					deselect();
       					writePizza(total);
       					total();
       					
       					//testing output code
       					System.out.println(orderTotal);
    				}
    			//begin checkboxes
       			else if (input == "bacon" && bacon.isSelected())
    				{	total+=0.50;
    					writePizza(total);
    					total();
    					
    					//testing output code
    					System.out.println(orderTotal);
    				}
       			
       			else if (input == "bacon" && !bacon.isSelected())
       				{	total-=0.50;
    					writePizza(total);
    					total();
    					
    					//testing output code
    					System.out.println(orderTotal);
       				}
       			textArea.setText(orderTotal);
       		}
       //write classes
       public void writePizza (float input)
       		{	try
       				{	String writeString = input +  "\n";
       					FileWriter fstream = new FileWriter("pizzaData.txt");
       					PrintWriter out = new PrintWriter(fstream);
       					out.write(writeString);
       					//Close the output stream
       					out.close();
       				}
       			catch (Exception d)
       				{	System.err.println("Error: " + d.getMessage());
       				}
       		}
       
       public void writeDrink (float input)
    		{	try
    				{	String writeString = input +  "\n";
    					FileWriter fstream = new FileWriter("drinkData.txt");
    					PrintWriter out = new PrintWriter(fstream);
    					out.write(writeString);
    					//Close the output stream
    					out.close();
    				}
    			catch (Exception d)
    				{	System.err.println("Error: " + d.getMessage());
    				}
    		}
       
       public void writeSide (float input)
    		{	try
    				{	String writeString = input +  "\n";
    					FileWriter fstream = new FileWriter("sideData.txt");
    					PrintWriter out = new PrintWriter(fstream);
    					out.write(writeString);
    					//Close the output stream
    					out.close();
    				}
    			catch (Exception d)
    				{	System.err.println("Error: " + d.getMessage());
    				}
    		}
    
    
       
       //read classes
       private static void readPizza() throws java.io.IOException
    		{	try
    				{	FileReader fr = new FileReader("pizzaData.txt");
    					BufferedReader br = new BufferedReader(fr); 
    					pizza =  br.readLine();
    				}
    			catch (Exception d)
    				{	System.err.println("Error: " + d.getMessage());
    				}
    		}
       
       private static void readDrink() throws java.io.IOException
    		{	try
    				{	FileReader fr = new FileReader("drinkData.txt");
    					BufferedReader br = new BufferedReader(fr); 
    					drink =  br.readLine();
    				}
    			catch (Exception d)
    				{	System.err.println("Error: " + d.getMessage());
    				}
    		}
       
       private static void readSide() throws java.io.IOException
    		{	try
    				{	FileReader fr = new FileReader("sideData.txt");
    					BufferedReader br = new BufferedReader(fr); 
    					side =  br.readLine();
    				}
    			catch (Exception d)
    				{	System.err.println("Error: " + d.getMessage());
    				}
    		}
       
    //total class
       
       private static void total()
       		{	try 
       				{	readPizza();
       					readDrink();
       					readSide();
       				}
       			catch (IOException e) 
       				{	e.printStackTrace();
       				}
       		float tempPizza = Float.valueOf(pizza).floatValue();
       		float tempDrink = Float.valueOf(drink).floatValue();
       		float tempSide = Float.valueOf(side).floatValue();
       		  		
       		
       		orderTotal= tempPizza + tempDrink + tempSide + "";
       		}
       
       private void wipe()
    		{	writePizza(0);
    			writeDrink(0);
    			writeSide(0);
      
    		}
    		
    	
       private void deselect()
       		{bacon.setSelected(false);
    	   
       		}
       
    //test timer usage
       public class TimerListener implements ActionListener
    		{	public void actionPerformed (ActionEvent event)
    				{	textArea.setText(orderTotal);
    				}
    		}
       
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Issue using Timer to constantly refresh Information

    Do you have a main() method and frame for testing the code?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Issue using Timer to constantly refresh Information

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Issue using Timer to constantly refresh Information

    Do you really need to update every millisecond? (hint: you don't.) Your Timer's actionPerformed(...) is probably bogging down the EDT so much that it can't get around to servicing the repaint requests generated bty setText(...)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Issue using Timer to constantly refresh Information

    Quote Originally Posted by Norm View Post
    Do you have a main() method and frame for testing the code?
    I am not entirely sure what you mean by this, I can get the timer to work by in a program by its self, for instance, I created a program to use the timer and to do a S.O.P every time the timer triggers, and it works fine. The problem is that when I take my working code from my test program and place it in here, it will successfully do any S.O.P that I want, however it refuses to set the textarea value to what it should be.


    Quote Originally Posted by DarrylBurke View Post
    Do you really need to update every millisecond? (hint: you don't.) Your Timer's actionPerformed(...) is probably bogging down the EDT so much that it can't get around to servicing the repaint requests generated bty setText(...)
    db
    I have played with various different values from 20 to 200 with no success. Is it possible that It does not have permission to access the textarea in the jpanel?

  6. #6
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Issue using Timer to constantly refresh Information

    Well I seem to have solved it by placing a call to total(); within the timerlistener. My best guess is that it is was actually properly setting the text, it just had not properly retotaled it. Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 05-01-2012, 08:35 PM
  2. Timer Class Issue
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 10:31 PM
  3. NetBeans GUI Builder + constantly updating information
    By Inventor22 in forum New To Java
    Replies: 5
    Last Post: 07-07-2010, 11:27 PM
  4. Java Game Timer Issue! Help
    By smithywill in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2010, 09:09 AM
  5. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM

Posting Permissions

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