User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Help Using Java Timer Page [1]  
BlackSheep
Suspended
1575 Posts
user info
edit post

Hey guys, I'm trying to use a timer to create a running stop watch when a user clicks a button for a time sheet program i'm writing. I've tried everything i know to get the timer going but it doesn't seem to be doing anything at all. I've set up some print lines to figure out if the line of code for the timer is being executed and it doesn't seem to be. I've tried other implementations for my program but the one below is the only way i've tried that hasn't given some sort of null pointer exception. Can any of you guys help me figure out what the problem is?


import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComponent;
import java.awt.Toolkit;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.Object.*;
import java.awt.event.*;
import java.util.*;
import java.text.DateFormat;
import java.awt.Graphics;
import javax.swing.Timer;
/**
*
* @author Jamie Snipes
*/
public class Main extends JFrame implements ActionListener{
public static void main(String[] args) {
new Main().setVisible(true);
}
/** Creates a new instance of Main */

public Main() {
initComponents();
getContentPane().setSize(800, 800);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation(new Point((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.width) / 2));
}
/**
* @param args the command line arguments
*/
private void initComponents() {
mainPanel = new javax.swing.JPanel();
mainPanel.setLayout(new BorderLayout());
projectTitleLabel = new javax.swing.JLabel();
buttonsPanel = new javax.swing.JPanel();
buttonsPanel.setLayout(new BorderLayout());
newItemPanel = new javax.swing.JPanel();
newItemPanel.setLayout(new FlowLayout());
removeProjectButton = new javax.swing.JButton("Remove");
notesButton = new javax.swing.JButton("Add Note");
stopButton = new javax.swing.JButton("Stop");
menuBar = new javax.swing.JMenuBar();
menuItem= new javax.swing.JMenuItem("Exit");
menu= new javax.swing.JMenu("File");
addTimeSheetItem= new javax.swing.JButton("Add") ;
eventTrackerPanel= new javax.swing.JPanel();
projectArray= new String[100];
removeProjectButton=new javax.swing.JButton("remove") ;



setTitle("Time Sheet");
//Menu Bar Itmes
menuBar.add(menu);
menuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
if(evt.getSource()==menuItem){
exitMenuItemActionPerformed(evt);
}
}
});
menu.add(menuItem);
setJMenuBar(menuBar);
mainPanel.setLayout(new java.awt.BorderLayout());
mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 5, 5, 5)));
mainPanel.setMinimumSize(new java.awt.Dimension(800, 800));
projectTitleLabel.setText("Project Name:");
//Top Panel
projectNameBox= new javax.swing.JComboBox();
projectNameBox.setEditable(true);
projectNameBox.addActionListener(this);
newItemPanel.add(projectNameBox);
newItemPanel.add(addTimeSheetItem);
// Main Panel
mainPanel.add(newItemPanel, BorderLayout.NORTH);
mainPanel.add(eventTrackerPanel, BorderLayout.CENTER);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(mainPanel);
pack();
}

public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
if(e.getActionCommand()=="comboBoxChanged"){
selectedProjectToggleButton= new javax.swing.JToggleButton[100];
timeField= new javax.swing.JTextField[100];
timeField[i]= new javax.swing.JTextField();
projectArray[i]= new String();
selectedProjectToggleButton[i]= new javax.swing.JToggleButton();
projectArray[i]=(String)projectNameBox.getSelectedItem();
System.out.println(projectArray[i]);
projectNameBox.addItem(projectArray[i]);
selectedProjectToggleButton[i].setText(projectArray[i]);
closeRemoveProjectCheckBox=new javax.swing.JCheckBox[100];
closeRemoveProjectCheckBox[i]= new javax.swing.JCheckBox();
projectsPanel = new javax.swing.JPanel[100];
projectsPanel[i]= new javax.swing.JPanel();
projectsPanel[i].setLayout(new BorderLayout());
projectsPanel[i].add(closeRemoveProjectCheckBox[i], BorderLayout.WEST);
closeRemoveProjectCheckBox[i].addActionListener(this);
projectsPanel[i].add(selectedProjectToggleButton[i], BorderLayout.CENTER);
selectedProjectToggleButton[i].addActionListener(this);
projectsPanel[i].add(timeField[i], BorderLayout.EAST);
eventTrackerPanel.setLayout(new GridLayout(i, 1));
eventTrackerPanel.add(projectsPanel[i], BorderLayout.CENTER);
removeProjectButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent remove) {
if(remove.getSource()==removeProjectButton){
timeField[i-1].setText("Seconds: " + time++);
timeField[i-1].repaint();
System.out.println("remove was clicked");
}
}
});

buttonsPanel.add(removeProjectButton, BorderLayout.WEST);
buttonsPanel.add(notesButton, BorderLayout.CENTER);
buttonsPanel.add(stopButton, BorderLayout.EAST);
i++;
System.out.println("i="+ i);
System.out.println("toggle text is " + selectedProjectToggleButton[i-1].getText());
}

else if(selectedProjectToggleButton[i-1].getText()==e.getActionCommand()){
System.out.println("hello");
timer= new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent evt){
timeField[i-1].setText("Time: " + time++);
timer.start();
timeField[i-1].repaint();
System.out.println("hello");


}
}

);


}

else if(selectedProjectToggleButton[i-1].getText()!=e.getActionCommand()){
for(a=0; a<=99; a++){
if(projectArray[a]==e.getActionCommand()){
System.out.println("the index for the clicked button is " + a);
}
}
}

pack();

}

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

// Variables declaration - do not modify

private javax.swing.JPanel buttonsPanel;
private javax.swing.JPanel newItemPanel;
private javax.swing.JPanel eventTrackerPanel;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JLabel feedbackLabel;
public javax.swing.JMenu fileMenu;
private javax.swing.JButton[] buttonArray;
private javax.swing.JButton startButton;
private javax.swing.JButton stopButton;
private javax.swing.JButton notesButton;
private javax.swing.JButton addTimeSheetItem;
private javax.swing.JButton removeProjectButton;
private javax.swing.JLabel projectTitleLabel;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem menuItem;
private javax.swing.JMenu menu;
private javax.swing.JComboBox projectNameBox;
private java.awt.event.ActionListener isClicked;
private int i=0;
private javax.swing.JToggleButton[] selectedProjectToggleButton;
private String[] projectArray;
private java.lang.Boolean projectEntered;
private String projectName;
private javax.swing.JCheckBox[] closeRemoveProjectCheckBox;
private javax.swing.JPanel[] projectsPanel;
private javax.swing.JTextField[] timeField;
private boolean selected;
private String button;
private int index;
private int a;
public Timer timer;
private int time=0;













// End of variables declaration

}

10/24/2006 5:42:49 PM

pttyndal
WINGS!!!!!
35217 Posts
user info
edit post

get the time at the beginning when the button is clicked and then get the time at the end and subtract the last from the original. Check out System.currentTimeMillis()

[Edited on October 24, 2006 at 7:00 PM. Reason : ]

10/24/2006 6:59:37 PM

BlackSheep
Suspended
1575 Posts
user info
edit post

wouldn't using the timer be less resource consuming since you don't have to loop?

If anyone knows how to get the timer working here i'd prefer that but i'll go ahead and try it with a while loop on the toggle button.

10/24/2006 8:38:24 PM

Lionheart
I'm Eggscellent
12775 Posts
user info
edit post

not sure about the implementation, but your timer is going to consume more memory, though it may not hold the clock like a spinlock would

10/24/2006 8:41:57 PM

scud
All American
10804 Posts
user info
edit post

http://jakarta.apache.org/commons/lang/
http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/time/StopWatch.html

10/24/2006 11:29:23 PM

BlackSheep
Suspended
1575 Posts
user info
edit post

hey, how do i download that class so that i can add it to my project?

10/25/2006 12:32:24 PM

scud
All American
10804 Posts
user info
edit post

rule #1 in software development

don't re-engineer the wheel - they're pretty good already and the odds that you're going to do a better job by yourself in a limited amount of time is infinitesimally small

http://jakarta.apache.org/site/downloads/downloads_commons-lang.cgi

10/25/2006 10:14:28 PM

 Message Boards » Tech Talk » Help Using Java Timer Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.