Results 1 to 6 of 6
  1. #1
    byte237 is offline Member
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    5
    Rep Power
    0

    Default Actionlisenter not reacting.

    Hi,
    I'm making a tool for a game called RuneScape so I can easier manage various things, but I'm having some problems with my action lisenter. Need's to be said that I'm no IT grad, so bear over with obvious mistakes, heh. I can't see what I'm doing wrong here.

    Source where the problem is: "compostTracker.java"
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class compostTracker extends JPanel{
    	private JButton addTracker;
    	private JLabel[] trackLabel;
    	private int POSX = 5, POSY = 10, INCREMENT = 20, tracks = 0, LENGTH = 200, HEIGHT = 50;
    	private JOptionPane trackCaption;
    	private newTracker[] NT;
    	private actionHandler actionHandler = new actionHandler();
    	
    	public compostTracker() {
    		this.setLayout(null);
    		
    		JButton addTracker = new JButton("Add new tracker");
    		addTracker.addActionListener(actionHandler);
    		addTracker.setBounds(3, 3, 130, 19);
    		
    		this.add(addTracker);	
    	}
    	public class newTracker extends Thread {
    		String caption;
    		boolean run = true;
    		int trackNumber;
    		long startTime = System.currentTimeMillis(), currentTime, timeLeft;
    		public void run() {
    		if (caption.length() <= 0)
    			caption = "Tracker #" + tracks;
    		try {
    			while(run == true) {
    		
    				currentTime = System.currentTimeMillis();
    				timeLeft = 3600000 - (currentTime - startTime / 1000); // 3600000 = 1 hour
    				if (timeLeft <= 0) {
    					run = false;
    					trackLabel[trackNumber].setText(String.format("Your compost should be ready."));
    					this.stop();
    				}
    				else {
    					trackLabel[trackNumber].setText(String.format("Seconds left: %d.", timeLeft));
    				}
    				this.sleep(999);
    			}
    		
    		} catch(InterruptedException ex) {}
    	}
    	
    	public class actionHandler implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			if (e.getSource() == addTracker) {
    				NT[tracks] = new newTracker();
    				NT[tracks].caption = trackCaption.showInputDialog("Enter track caption.");
    				NT[tracks].trackNumber = tracks;
    				trackLabel[tracks] = new JLabel();
    				trackLabel[tracks].setBounds(POSX, POSY + INCREMENT, LENGTH, HEIGHT);
    				add(trackLabel[tracks]);
    				NT[tracks].start();
    				tracks++;
    				POSY += INCREMENT;
    			}
    		}
    	}
    	
    }
    If you want to compile the program on your own machine, heres the rest of my files to make it work:

    "start.java":
    Java Code:
    public class start {
    	public static void main(String[] args) {
    		engine Engine = new engine();
    		if (Engine.initGUI() == true) {
    			//if the GUI starts
    			
    		}
    	}
    	
    	
    }
    "engine.java":
    Java Code:
    import java.awt.Component;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    
    public class engine {
    	public JFrame window;
    	private JTabbedPane tabPanel = new JTabbedPane();
    	private Component[] tabComponents = {new compostTracker()};
    	private String[] tabCaptions = {"Compost Tracker"};
    	
    	public boolean initGUI() {
    		try {
    			window = new JFrame("Farm Master X");
    			for(int i = 0; i  <= tabCaptions.length-1; i++) {
    				tabPanel.addTab(tabCaptions[i], new JScrollPane(tabComponents[i]));
    			}		
    			window.add(tabPanel);
    			window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			window.setSize(500, 450);
    			window.setVisible(true);
    			return true;
    		} catch(Exception ex) { 
    			ex.printStackTrace();
    			return false;
    		}	
    	}
    	
    	
    }
    Thanks in advance.
    Last edited by byte237; 01-06-2010 at 08:49 PM.

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

    Default

    Quote Originally Posted by byte237 View Post
    Hi,
    I'm making a tool for a game called RuneScape so I can easier manage various things, but I'm having some problems with my action lisenter. Need's to be said that I'm no IT grad, so bear over with obvious mistakes, heh. I can't see what I'm doing wrong here.
    Most importantly, I think you need to tell us what is not working here as I can't guess this from your post. How is the program not working? What errors are you seeing?

  3. #3
    byte237 is offline Member
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    5
    Rep Power
    0

    Default

    My bad. When I press the button, nothing happends. I've tried to add a JLabel to change text just to see if anything happends, but nothing happends, which is a bit odd.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    I suspect that the listener does get called.
    If so, it will do nothing because of this test:
    Java Code:
    if (e.getSource() == addTracker) {
    There are two declarations of addTracker
    -- one as a class variable and one as a method variable in the constructor.
    The test above is comparing the two values and will always find them unequal.

  5. #5
    byte237 is offline Member
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    5
    Rep Power
    0

    Default

    Thank you very much!
    But now I'm getting some errors and I can't really figgure out what I'm doing wrong, again...

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at compostTracker$theHandler.actionPerformed(compostTracker.java:56)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The exception trace tells you exactly where the problem occurred.
    At that line you are derefencing null.

Similar Threads

  1. Reacting Radio button event
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:46 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
  •