Results 1 to 6 of 6
- 05-13-2012, 02:30 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
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
and the timerlistener classJava Code:Timer timer; timer = new Timer(1, new TimerListener()); timer.start();
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?Java Code:public class TimerListener implements ActionListener { public void actionPerformed (ActionEvent event) { textArea.setText(orderTotal); } }
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); } } }
- 05-13-2012, 02:53 AM #2
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.
- 05-13-2012, 03:30 AM #3
Re: Issue using Timer to constantly refresh Information
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-13-2012, 03:32 AM #4
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(...)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-13-2012, 04:19 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: Issue using Timer to constantly refresh Information
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.
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?
- 05-13-2012, 04:23 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Eclipse RCP plugin GUI refresh issue(SWT_AWT bridge)
By mahii in forum EclipseReplies: 2Last Post: 05-01-2012, 08:35 PM -
Timer Class Issue
By CuppaCoffee in forum New To JavaReplies: 3Last Post: 01-05-2012, 10:31 PM -
NetBeans GUI Builder + constantly updating information
By Inventor22 in forum New To JavaReplies: 5Last Post: 07-07-2010, 11:27 PM -
Java Game Timer Issue! Help
By smithywill in forum Advanced JavaReplies: 2Last Post: 03-11-2010, 09:09 AM -
Inside a Timer thread loop,how to refresh a JTable in swing
By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 01:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks