Results 1 to 3 of 3
  1. #1
    Graham is offline Member
    Join Date
    Oct 2013
    Location
    United States
    Posts
    4
    Rep Power
    0

    Default passing Integers and Strings between classes

    I'm working on a project where the "main" is a GUI and the peripheral functions are separate classes
    within the project. I have the objects in the JPanel; JButtons, JTextboxes and JTextAreas working
    fine. I have the event listeners and event triggers working. I have the peripheral classes working
    discretely where they print their output to the console. I'm now at a stage where I need to pass
    I/O directly between the GUI and the other classes. I've figured out how to launch the classes
    from the GUI and to use "getters" and "setters" to move text from a textbox to a textarea,
    but only within the GUI class. I've tried various ways to pass a variable containing an integer
    value from a class to the textarea in the GUI with no joy..

    Below is getter/setter that works to write text inputted into the textbox to the textarea in the GUI.

    public void actionPerformed(ActionEvent e) {

    // Create Action Event Handler for Login button
    System.out.println(" You pressed Login button and entered the following text");


    //Grab text from TextBox "nameField"
    String TextField = nameField.getText();

    //Import text from "nameField" and port it to TextArea "outputField"
    //Left to right, go and get it and haul it back.
    outputField.setText(nameField.getText());
    System.out.println(TextField);

    System.out.println("Done");


    Below is the section of code that defines the JButton and manages operation for the Template Button.

    JButton btnTemplate = new JButton("Template");
    btnTemplate.setFont(new Font("Tahoma", Font.BOLD, 12));
    btnTemplate.setBounds(166, 373, 106, 24);
    contentPane.add(btnTemplate);

    //Create Action Listener for Template Button

    btnTemplate.addActionListener (new ActionListener()

    {
    public WipTemplateGenerator counter;

    public void actionPerformed(ActionEvent e) {


    // Create Action Event Handler for Template button


    System.out.println(" You pressed the Template button");

    //Launch the template generator.
    {WipTemplateGenerator WipTemplateGeneratorObject = new WipTemplateGenerator ();
    try {WipTemplateGenerator.TemplateGenerator(WipTemplat eGeneratorObject) ;
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();


    System.out.println("The Counter is" + counter);


    "Counter" is created in the WipTemplateGenerator class.

    The template generator runs but outputs the counter value to the console only with e1.printstacktrace. I tried various get/set lines
    to write it back to "outputField", but no luck. Can anyone give me some ideas on what protocol I can use to move data back and
    forth with action event triggers??

    Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: passing Integers and Strings between classes

    Why can't you use getters and setters? You just need to maintain an instance to the classes involved. Of course you could also use PropertyChange events. But you still need to have access the the other object to add the listeners.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Graham is offline Member
    Join Date
    Oct 2013
    Location
    United States
    Posts
    4
    Rep Power
    0

    Default Re: passing Integers and Strings between classes

    Jim,
    I assume I can use them, I just haven't figured out exactly how yet. I'm not sure how to maintain the instance properly and gain access to the
    object in the origin class. Basically I'm trying to do an FTP "Pull" :=) . I've tried but I'm missing something because the classes execute but independently
    and thats the problem. The get and set in the gui just doesn't retrieve the "counter" value. Any more specific suggestions?

    Best
    Graham

Similar Threads

  1. Draw integers like strings
    By MartyP in forum AWT / Swing
    Replies: 2
    Last Post: 03-16-2011, 04:36 AM
  2. switch case with integers and strings...
    By JavaComplient in forum New To Java
    Replies: 2
    Last Post: 10-21-2010, 06:52 AM
  3. Combining strings and integers into collection
    By neilvb in forum New To Java
    Replies: 2
    Last Post: 10-05-2010, 10:23 PM
  4. Finding Strings, booleans and Integers
    By Pez in forum SWT / JFace
    Replies: 1
    Last Post: 07-19-2009, 02:24 PM
  5. Replies: 7
    Last Post: 04-28-2008, 04:28 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
  •