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 |