Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default List of Items in JTextArea

    Hi

    In the following code, I have to allow the user to enter 5 items

    Once he tries to enter the 6th item, he will get an error message

    Also, I have to write before each item the number of item

    For example, if he enters "car", in the textArea he will get
    1) car

    the second item "book", he will get in the textArea
    2) book


    In my code, there is a problem with the numbers that come before each item

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GroceryList extends JApplet implements ActionListener{
    	
    	private JLabel enterLabel;
    	private JTextField groceryField;
    	private JButton addButton;
    	private JTextArea listArea;
    	private JButton clearButton;
    	
    	public void init(){
    		enterLabel = new JLabel("Enter a grocery item that you want:");
    		groceryField = new JTextField(30);
    		addButton = new JButton("Add To My List");
    		listArea = new JTextArea("I would like:",6,20);
    		listArea.setEditable(false);
    		clearButton = new JButton("Clear My List");
    		
    		addButton.addActionListener(this);
    		clearButton.addActionListener(this);
    		
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new FlowLayout());
    		contentPane.add(enterLabel);
    		contentPane.add(groceryField);
    		contentPane.add(addButton);
    		contentPane.add(listArea);
    		contentPane.add(clearButton);
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource()==addButton){
    			[COLOR="Red"]int counter=1;
    			int number=1;
    			while(counter<6){
    				String text = groceryField.getText();
    				listArea.append("\n"+number+") "+text);
    				 groceryField.setText("");
    				 counter++;
    				 number++;[/COLOR]
    			}
    				enterLabel.setText("I'm sorry - Your list is full.");
    			 
    		}else{
    			listArea.setText(" ");
    			listArea.setText("I would like:");
    			enterLabel.setText("Enter a grocery item that you want:");
    		}
    		}
    }

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

    Default

    You're mixing up console programming with event driven programming. Your actionPerformed will be called once each time the user tries to enter an item, so there's no place for that while loop in there -- just get rid of it. Instead use an int index variable that is declared and initialized outside of the actionPerformed method and increment it in the actionPerformed and have an if block in there that will respond to the value of this index.
    Last edited by Fubarable; 03-15-2010 at 02:23 AM.

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Thanks bro for your cooperation

    I appreciate that

    I did what you said and it's fine

    but there is problem which is when the user enters the 6th item it gives him an error message but it put the 6th item under the 5th one and I don't want this one.

    This is the code after editing

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GroceryList extends JApplet implements ActionListener{
    	
    	private JLabel enterLabel;
    	private JTextField groceryField;
    	private JButton addButton;
    	private JTextArea listArea;
    	private JButton clearButton;
    	private int itemNumber = 1;
    	
    	public void init(){
    		enterLabel = new JLabel("Enter a grocery item that you want:");
    		groceryField = new JTextField(30);
    		addButton = new JButton("Add To My List");
    		listArea = new JTextArea("I would like:",6,20);
    		listArea.setEditable(false);
    		clearButton = new JButton("Clear My List");
    		
    		addButton.addActionListener(this);
    		clearButton.addActionListener(this);
    		
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new FlowLayout());
    		contentPane.add(enterLabel);
    		contentPane.add(groceryField);
    		contentPane.add(addButton);
    		contentPane.add(listArea);
    		contentPane.add(clearButton);
    		
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource()==addButton){
    		
    				String text = groceryField.getText();
    				listArea.append("\n"+itemNumber+") "+text);
    				itemNumber++;
    				 groceryField.setText("");
    			if(itemNumber==7){
    				
    				enterLabel.setText("I'm sorry - Your list is full.");
    				
    			}	
    		}else{
    			listArea.setText(" ");
    			listArea.setText("I would like:");
    			enterLabel.setText("Enter a grocery item that you want:");
    		}
    		}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Why don't you think through your logic and give it another try -- I know that you can figure this out. You're smart!

  5. #5
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    What I think is I have to enable the button so that when the user tries to enter another item he won't be able to do that

    I'm really really confused with this program.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Think about where you want your if block here and what you want to include inside of it:
    Java Code:
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource()==addButton){
    		
    				String text = groceryField.getText();
    				listArea.append("\n"+itemNumber+") "+text);
    				itemNumber++;
    				 groceryField.setText("");
    			if(itemNumber==7){
    				
    				enterLabel.setText("I'm sorry - Your list is full.");
    				
    			}	
    		}else{
    			listArea.setText(" ");
    			listArea.setText("I would like:");
    			enterLabel.setText("Enter a grocery item that you want:");
    		}
    		}
    }
    Remember that it will always do the code before the if block regardless of the state of the itemNumber variable.

  7. #7
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    I finally discovered it

    Java Code:
    public void actionPerformed(ActionEvent e){
    		if(e.getSource()==addButton){
    			if(itemNumber!=6){
    				String text = groceryField.getText();
    				listArea.append("\n"+itemNumber+") "+text);
    				itemNumber++;
    				 groceryField.setText("");
    			}else{
    				enterLabel.setText("I'm sorry - Your list is full.");
    				
    			}

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    I finally discovered it
    Cool. Nice going.

Similar Threads

  1. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  2. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  3. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  4. jlist items
    By bullstreetboy in forum New To Java
    Replies: 6
    Last Post: 04-12-2009, 05:30 PM
  5. Replies: 8
    Last Post: 12-29-2008, 07:12 AM

Posting Permissions

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