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

    Default Store values in arrays

    Hi

    I'm trying to store any string value in an array but there is a problem which is when I press "Save" button it doesn't store the value in the array

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ArraysApplet extends JApplet implements ActionListener{
    	
    	private JTextField text;
    	private JButton save;
    	private JButton retrieve;
    	private JLabel label;
    	
    	public void init(){
    		text = new JTextField(10);
    		save = new JButton("Save");
    		retrieve = new JButton("Retrieve");
    		label= new JLabel("Here is the name");
    		
    		save.addActionListener(this);
    		retrieve.addActionListener(this);
    		
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new FlowLayout());
    		contentPane.add(text);
    		contentPane.add(save);
    		contentPane.add(retrieve);
    		contentPane.add(label);
    		
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		String fullName[] = {""};
    		if(e.getSource()==save){
    			String t = text.getText();
    			fullName[0]= ""+t;
    		}if(e.getSource()==retrieve){
    			label.setText(""+fullName[0]);
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    Hi

    I'm trying to store any string value in an array but there is a problem which is when I press "Save" button it doesn't store the value in the array
    That array is a local variable; it is gone when the method returns and a new one is created when the method is called again. Make that array a member variable so it'll exist as long as the entire object exists.

    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 11-22-2009, 06:24 PM
  2. Replies: 3
    Last Post: 11-21-2009, 06:48 PM
  3. Adding Values of 2 Arrays
    By core2duo5252 in forum Java 2D
    Replies: 7
    Last Post: 09-05-2009, 04:50 AM
  4. How to store extracted xml values in a database?
    By palanikumark in forum Advanced Java
    Replies: 6
    Last Post: 05-30-2008, 01:14 PM
  5. Replies: 0
    Last Post: 05-22-2008, 11:53 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
  •