Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Calling method cant get the parameters right

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Angry Calling method cant get the parameters right

    im trying to call a method in my main class (GUI) from a different class (ActionHandeler)

    heres the method in GUI:
    public static void main(String[] args) {
    		// Schedule a job for the event dispatch thread:
    		// creating and showing this application's GUI.
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				GUI gui = new GUI();
    				// Turn off metal's use of bold fonts
    				UIManager.put("swing.boldMetal", Boolean.FALSE);
    				gui.launchFrame();
    				action.actionPerformed(null);
    			}
    		});
    	}

    here is 'actionPerformed' in ActionHandeler:
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		String cmd = e.getActionCommand();
    		if (cmd != null) {
    			if (cmd.equals("submit")) {
    				try {
    					int guessed;
    					String input = g.type.getText();
    					int value = random(10);
    					guessed = Integer.parseInt(input);
    					if (guessed < 1 || guessed > 10) {
    						JOptionPane.showMessageDialog(null, guessed
    								+ " is not a valid number!");
    					} else if (guessed == value) {
    						JOptionPane.showMessageDialog(null,
    								"You guessed it right! :D");
    						wins += 1;
    					} else {
    						JOptionPane.showMessageDialog(null,
    								"Sorry the correct awenser was " + value
    										+ ". :/");
    						losses += 1;
    					}
    				} catch (Exception e1) {
     
    				}
    			}
    		}
    	}

    I know that the parameters cant equal 'null'
    action.actionPerformed(null);
    I tried making it 'e' and 'ActionEvent' but its not working

    any help would be appreciated!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Calling method cant get the parameters right

    This seems like a bad design. Shouldn't you be adding the action to something else that calls actionPerformed?

    But if you really want to go this way, there's nothing stopping you from constructing your own instance of ActionEvent.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Calling method cant get the parameters right

    Why are u calling the event's method? I'm not sure how efficient that may be. But if you can't find a solution, i suggest putting all that code in actionPerformed and creating your own method, and when an event occurs you just call that method from actionPerformed. Same with your main class, call your method from the main class instead of the actionPerformed. This way the method of your code is not limited to listening to ActionEvent variables only.

  4. #4
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Calling method cant get the parameters right

    i fixed it. I implement ActionEvent into my main class instead of going to a different class to handle it

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  3. [SOLVED] method calling
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 01:43 AM
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  5. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM