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: Adding data to a text box on another class

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding data to a text box on another class

    Hi,

    I have a class that creates a GUI interface with a text box on it.
    I want to be able to add text into the text box from another class but I can't seem to do it.

    Does anyone have any examples they can share?

    Thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Adding data to a text box on another class

    Post what you've done and where you need help (doing so as an SSCCE will strengthen your chance of getting quicker and more meaningful response).

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding data to a text box on another class

    ok thanks.

    So this is my main class where I have created what is now a ListBox and a button. When I click on the button I want to be able to load a set of results from a csv file and add them to the lisbox.

    thanks
    import org.eclipse.swt.widgets.Display;
     
    public class guiwindow {
     
    	protected Shell shell;
     
    	/**
    	 * Launch the application.
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		try {
    			guiwindow window = new guiwindow();
    			window.open();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
     
    	/**
    	 * Open the window.
    	 */
    	public void open() {
    		Display display = Display.getDefault();
    		createContents();
    		shell.open();
    		shell.layout();
    		while (!shell.isDisposed()) {
    			if (!display.readAndDispatch()) {
    				display.sleep();
    			}
    		}
    	}
     
    	/**
    	 * Create contents of the window.
    	 */
    	protected void createContents() {
    		shell = new Shell();
    		shell.setSize(450, 300);
    		shell.setText("SWT Application");
     
    		List lstItems = new List(shell, SWT.BORDER);
    		lstItems.setBounds(10, 10, 197, 242);
     
    		Button btnLoadFile = new Button(shell, SWT.NONE);
    		btnLoadFile.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseUp(MouseEvent e) {
    				[B][COLOR="red"]//run import from csv here[/COLOR][/B]
    			}
    		});
    		btnLoadFile.setBounds(220, 10, 75, 25);
    		btnLoadFile.setText("Load File");
     
    	}
     
    }
     
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
     
    public class CSVFileReader {
     
    	ArrayList <String>storeValues = new ArrayList<String>();
    	ArrayList <String>CleanList = new ArrayList<String>();
    	private String fileName;
     
    	public CSVFileReader(String FileName)	{		
    		this.fileName=FileName;	} 
     
    	public ArrayList<String> ReadFile()
    	{
    		try {
    			//storeValues.clear();//just in case this is the second call of the ReadFile Method./
    			BufferedReader br = new BufferedReader( new FileReader(fileName));
     
    			StringTokenizer st = null;
    			int lineNumber = 0, tokenNumber = 0;
     
    			while( (fileName = br.readLine()) != null)
    			{
    				lineNumber++;
    				//System.out.println(fileName);
    				storeValues.add(fileName);
    				//break comma separated line using ","
    				st = new StringTokenizer(fileName, ",");
     
    				while(st.hasMoreTokens())
    				{	
     
    					//System.out.println("Line # " + st.nextToken());
    					CleanList.add(st.nextToken());
     
    				}
     
    				//reset token number
    				tokenNumber = 0;
     
    			} 
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return CleanList;
     
    	}
     
     
    	//mutators and accesors 
     
    	public void displayArrayList()
    	{
    		for(int x=0;x<this.CleanList.size();x++)
    		{
    			System.out.println(CleanList.get(x));
    		}
    	}
     
    }
    Last edited by copeg; April 3rd, 2011 at 09:29 AM.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding data to a text box on another class

    ok to fix it i did the following

    public class guiwindow {

    protected Shell shell;
    public List lstItems;

    /**
    * Launch the application.
    * @param args
    */
    public static void main(String[] args) {
    try {
    guiwindow window = new guiwindow();
    window.open();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * Open the window.
    */
    public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
    display.sleep();
    }
    }
    }

    /**
    * Create contents of the window.
    */
    protected void createContents() {
    shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    lstItems = new List(shell, SWT.BORDER);
    lstItems.setBounds(10, 10, 197, 242);

    Button btnLoadFile = new Button(shell, SWT.NONE);
    btnLoadFile.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseUp(MouseEvent e) {
    //run import from csv here
    }
    });
    btnLoadFile.setBounds(220, 10, 75, 25);
    btnLoadFile.setText("Load File");

    }

    }

Similar Threads

  1. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  2. Adding entered text from one GUI to another
    By fride360 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 24th, 2011, 01:08 PM
  3. [SOLVED] Storing info into a text file & adding to it
    By BlackFlame in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 08:06 PM
  4. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  5. CashRegister class- adding a method getItemCount
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 21st, 2010, 07:29 PM