Results 1 to 2 of 2
  1. #1
    themanepalli is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Angry adding textfields

    Im a 15 year old noob to java. I am trying to make a basic program trader that asks for the stock price, the stock name, the stock market value and the type of order. Based on the type of order, i want a new textfield to appear. do i have to add the textfield in the init first or can i do it in the action performed. I googled someother ones but they are a little too complicated for me. heres my code.

    Java Code:
    import java.awt.*;
    import java.applet.*;
    // import an extra class for the ActionListener
    import java.awt.event.*;
    
    
    public class mathFair extends Applet implements ActionListener
    {
    	TextField stockPrice2;
    	TextField stockName2;
    	TextField orderType2;
    	TextField marketValue2;
    	TextField buyOrder2;
    	TextField sellOrder2;
    	TextField limitOrder2;
    	TextField stopLossOrder2;
    	Label stockPrice1;
    	Label stockName1;
    	Label orderType1;
    	Label marketValue1;
    	Label buyOrder1;
    	Label sellOrder1;
    	Label limitOrder1;
    	Label stopLossOrder1;
    	Button calculate;
    
    
    
    
    
    public void init()
    {
    
    	      stockPrice1 = new Label ("Enter Stock Price:");
    
    	      stockName1 = new Label ("Enter Name of Stock: ");
    
    	      orderType1 = new Label ("Enter Type of Order: 1 for Buy, 2 for Sell, 3 for Stop Loss, 4 for Limit");
    
    	      marketValue1= new Label("Enter The Current Price Of The Market");
    
    
    
    
    
    	      stopLossOrder1 = new Label ("Enter The Lowest Price The Stock Can Go");
    
    	      limitOrder1 = new Label ("Enter The Highest Price The Stock Can Go");
    
    	      stockPrice2 = new TextField (35);
    
    		  stockName2 = new TextField (35);
    
    	      orderType2 = new TextField (35);
    
    	      marketValue2= new TextField(35);
    
    	      calculate= new Button("Start The Simulation");
    
    	      add (stockPrice1);
    
    	      add (stockPrice2);
    
    	      add (stockName1);
    
    	      add (stockName2);
    
    	      add (marketValue1);
    		  	      ;
    	      add(marketValue2);
    
    	      add (orderType1);
    
    	      add (orderType2);
    
    	      add(calculate);
    
    
    		 ;
    
    
    
    		  calculate.addActionListener(this);
    
    
    
    
    
    
     }
    
    
    
    
    
    
    	         public void actionPerformed (ActionEvent e)
    	         {
    
    					String stock= stockPrice2.getText();
    					int stockPrice= Integer.parseInt(stock);
    	            	stockPrice2.setText(stockPrice +"");
    
    
    	            	String marketV= marketValue2.getText();
    					int marketValue= Integer.parseInt(marketV);
    					marketValue2.setText(marketValue+"");
    
    
    
    
    					String orderT= orderType2.getText();
    					int orderType= Integer.parseInt(orderT);
    					orderType2.setText(orderType+"");
    
    
    
    					if((e.getSource()==calculate)&& (orderType==1))
    					{
    						buyOrder2= new TextField(35);
    						buyOrder1 = new Label("Enter Price You Would Like To Buy At");
    						add(buyOrder2);
    						add(buyOrder1);
    					}
    					else
    					if((e.getSource()==calculate)&& (orderType==2))
    					{
    						 sellOrder2= new TextField(35);
    						 sellOrder1 = new Label("Enter Price You Would Like To Sell At");
    						 add(sellOrder2);
    		 				 add(sellOrder1);
    
    					}
    					else
    					if((e.getSource()==calculate)&& (orderType==3))
    					{
    
    						stopLossOrder2= new TextField(35);
    						stopLossOrder1=new Label("Enter The Lowest Price The Stock Can Go");
    						add(stopLossOrder2);
    						add(stopLossOrder1);
    
    
    					}
    					else
    					if((e.getSource()==calculate)&& (orderType==4))
    					{
    
    						limitOrder2=new TextField(35);
    						limitOrder1= new Label("Enter the Highest Price The Stock Can Go");
    						add(limitOrder2);
    						add(limitOrder1);;
    					}
    
    
    			}
    
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Try to invoke validate(); after you have added some new components e.g. after add(buyOrder1); in your actionPerformed

Similar Threads

  1. How to validate textfields if data is modifier
    By sasi.tati in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2010, 09:10 AM
  2. Show/Hide Textfields in Struts 2 form
    By aruna.hcu in forum Web Frameworks
    Replies: 3
    Last Post: 07-08-2010, 03:36 PM
  3. Buttons and TextFields
    By Xystus777 in forum New To Java
    Replies: 27
    Last Post: 03-17-2009, 01:26 AM
  4. Replies: 0
    Last Post: 02-11-2009, 11:12 PM
  5. information from a textfields
    By bbq in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-28-2007, 05:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •