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 10 of 10

Thread: How to build more actions in exicting code. A continum problem.

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default How to build more actions in exicting code. A continum problem.

    Firstly-Sorry about my spelling

    I'm trying to code, produce, calculating ''machine'' (hahaha) and I've tried various things(?). If else-statement.....blabla....see the text in yellow.

    I'm trying to build on existing code that is working fine, the plus action is working. I'm trying to build more actions in it like a minus function...for starters. What I need is a sugestions on how to proceed This is the error I get in the Eclipse console:

    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 693)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)


    mport java.applet.Applet;// The code starts here.
    import java.awt.*;
     
    	public class Reiknivel extends Applet // Reiknivel means calculator machine
    	{
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = 1L;
    		TextField t1, t2, tsvar;// svar means answer
    		Button bplus; Button bminus;
    		Label a1, a2, b3;
    		Double D1, D2;
    		double d1, d2, dsvar;
     
    		public void init()
    		{
    			t1 = new TextField(3);
    			t2 = new TextField(3);
    			tsvar = new TextField(15);
    			bplus = new Button("+");
    			bminus = new Button("-");
    			a1 = new Label ("Tala 1");//means number 1
    			a2 = new Label ("Tala 2");//means number 2
    			b3 = new Label ("Samanlagt");
     
    			this.add(a1); 
    			this.add(t1);
    			this.add(bplus);
    			this.add(bminus);
    			this.add(a2);
    			this.add(t2);
    			this.add(b3);
    			this.add(tsvar);
    		}
     
    		public boolean action(Event e,Object o)
    		{
    			if (e.target == bplus);
    			{
     
    			D1 = new Double(t1.getText());
    			d1 = D1.doubleValue();
    			D2 = new Double(t2.getText());
    			d2 = D2.doubleValue();
    			dsvar = d1 + d2;
    			tsvar.setText("Summan er: " + dsvar);
    			return true;
    		}
    		return false;// Here ends the original code. And it works just fine.[COLOR="#FFD700"] AND HERIN LIES MY PROBLEM. The thing that I'm trying to ''produce'' is to add -, * , / ...buttons and actions or functions        [/COLOR]
    		}
    		public boolean action2(Event e,Object o)// I'm trying to continue here
    		{
    			if (e.target == bminus);
     
     
    			D1 = new Double(t1.getText());
    			d1 = D1.doubleValue();
    			D2 = new Double(t2.getText());
    			d2 = D2.doubleValue();
    			dsvar = d1 - d2;
    			tsvar.setText("Summan er: " + dsvar);
    			return true;
    		}
    	}
    Last edited by jps; August 5th, 2013 at 07:14 PM. Reason: code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    Please use code tags when posting code, assistance can be found on the Announcements page

    Beware that placing a semicolon after an if statement "ends" the condition and the following statements will always be executed
    if(false);//if ends here
    {
       System.out.println("this will print");
    }

  3. The Following User Says Thank You to jps For This Useful Post:

    einar123 (August 6th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    If you're trying to run the code you've posted (with the first 'mport' fixed), you should be seeing a red squiggly under this line:

    return false;// Here ends the original code. . . . (Line 50 to me)

    If you put your cursor on the red squiggly, a tool tip-like message will appear to tell you why it's there, hinting at the problem jps highlighted above. If you correct the mistake that jps pointed out, then the red squiggly should go away.

    Why are you using Applet instead of the more modern Swing version, JApplet?

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (August 6th, 2013)

  6. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    Einar123,
    You know what action2() is?
    But does JVM know when is it supposed to exeucte action2()? If not do you think you can tell JVM somehow to consider action2() also?
    If not then what is the solution?
    Sometimes JVM does not listen to us. We will have to listen to what it says.
    You don't have to define any extra action methods. Intelligently put your extra code in the single action() and try to get the output.

    Syed.

  7. The Following User Says Thank You to syedbhai For This Useful Post:

    einar123 (August 6th, 2013)

  8. #5
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    Hahah, thank you GregBrannon. I'm using an old textbook-but it is my language icelandic. I have no idea what Swing version, JApplet is. I also have a 2012 textbook: Java HOW TO PROGRAM by Paul Deitel but it's a bit overwhelming at first so I'm using this old book to start with...and it's fun

    --- Update ---

    Thank you jps. Code tags. I will try next timež. Thanks

  9. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    JApplets were included in Swing in Java 1.1 or 1.2 (fuzzy historical references, and I wasn't there).

    Applets are AWT (not Swing). Your book should be consistent in the time frame of the material presented, so learning as you are is probably okay, but it's unfortunate that you have to learn the AWT classes that have been built upon - extended and improved - by the Swing classes. When you are able to begin using Swing classes, it is recommended to not mix AWT components with Swing components in the same class or program.

    Good luck and keep coding!

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (August 6th, 2013)

  11. #7
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    Hey guys. I'm just not cutting it. I can not build another action, action2. I've tried viping it out, then I get red dots all over the place. Can you give me any ideas?

    I'm trying to build another button, ''-'' button onto + action in a ''calculating machine''


     
    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Event;
    import java.awt.Label;
    import java.awt.TextField;
     
     
     
    	public class Reiknivel extends Applet 
    	{
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = 1L;
    		TextField t1, t2, tsvar;
    		Button bplus; Button bminus;
    		Label a1, a2, b3;
    		Double D1, D2;
    		double d1, d2, dsvar;
     
    		 public void init()
    		{
    			t1 = new TextField(3);
    			t2 = new TextField(3);
    			tsvar = new TextField(15);
    			bplus = new Button("+");
    			bminus = new Button("-");
    			a1 = new Label ("Tala 1");
    			a2 = new Label ("Tala 2");
    			b3 = new Label ("Samanlagt");
     
    			this.add(a1); 
    			this.add(t1);
    			this.add(bplus);
    			this.add(bminus);
    			this.add(a2);
    			this.add(t2);
    			this.add(b3);
    			this.add(tsvar);
    		}
     
    		public boolean action(Event e,Object o)
    		{
    			if (e.target == bplus)
    			{
     
    			D1 = new Double(t1.getText());
    			d1 = D1.doubleValue();
    			D2 = new Double(t2.getText());
    			d2 = D2.doubleValue();
    			dsvar = d1 + d2;
    			tsvar.setText("Summan er: " + dsvar);
    			return true;
    			}
    			return false;
     
    		}
    		public boolean action2(Event e,Object o)
    		{
    			if (e.target == bminus)
    			{
     
    			D1 = new Double(t1.getText());
    			d1 = D1.doubleValue();
    			D2 = new Double(t2.getText());
    			d2 = D2.doubleValue();
    			dsvar = d1 - d2;
    			tsvar.setText("Summan er: " + dsvar);
    			return true;
    		    }
    			return false;
    	}
    	}

  12. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    I suggest you do this:
    	public boolean action(Event e,Object o)
    	{
    		if (e.target == bplus)
    		{
    			D1 = new Double(t1.getText());
    			d1 = D1.doubleValue();
    			D2 = new Double(t2.getText());
    			d2 = D2.doubleValue();
    			dsvar = d1 + d2;
    			tsvar.setText("Summan er: " + dsvar);
    			return true;
    		}
    		else if (e.target == bminus)
    		{
    			D1 = new Double(t1.getText());
    			d1 = D1.doubleValue();
    			D2 = new Double(t2.getText());
    			d2 = D2.doubleValue();
    			dsvar = d1 - d2;
    			tsvar.setText("Summan er: " + dsvar);
    			return true;
    		}
    		return false;
     
    	}

  13. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (August 6th, 2013)

  14. #9
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    Master GregBrannon. In Iceland it's about twelve, it's sleeping time. I will sleep well, because of your contribution (hahah). Problem solved!

  15. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to build more actions in exicting code. A continum problem.

    Glad to help, and sleep well!

Similar Threads

  1. java pop-up actions
    By Ravi Raj in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2013, 12:44 AM
  2. Help in putting actions to buttons
    By juliusmasa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 14th, 2011, 07:22 AM
  3. [SOLVED] need help with struts actions
    By arvindbis in forum Web Frameworks
    Replies: 0
    Last Post: October 4th, 2011, 05:40 AM
  4. Need Hellp!! problem with org.apache.struts.actions.DownloadAction.copy
    By nikshri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2010, 01:06 AM
  5. do actions in ComboBox
    By java_cs in forum AWT / Java Swing
    Replies: 2
    Last Post: October 1st, 2009, 10:53 AM