-3

I have a few classes and one main method. The program is used to connect to an Access database and retrieve information.

I have a class which deals with the GUI only (to display the results) in one JTextArea box. Another class runs a while loop and pulls in data from the database and assigns it to a String as such:

line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");

Basically my question is how do I access the String line from the GUI Class so that I can use txtArea.setTextto display line bearing in mind the GUI has no Main Method?

EDIT:

To try get around this, I have created a LineObject which takes line in as a parameter. I then call the getLine from the void addComponents but it gives a nullPointerException?

Here is the searchProps class:

import java.awt.Container;
import java.sql.*;
import java.util.*;
import javax.swing.*;


public class searchProps 
{
    protected String price, area, query, suburb, date, SaleOrRent, strQuery, out, line="";
    protected int agentID, upid, StreetNum, numBeds, numBaths, numGarages, ownerID, size;
    protected boolean spool;
    PropertyObject PropertyArray[] = new PropertyObject[3];
    LineObject obj;
    JFrame jf;  
    JTextArea txtArea = new JTextArea();
     {
        initialFrame();
        addComponents();
    }

public searchProps(int propID) //search using UPID only
   {
       try 
               {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection("jdbc:odbc:PropertyOracleDatabase");
            Statement s = conn.createStatement();



            query = ("SELECT * FROM Properties WHERE UPID = "+propID);

            // Fetch table
            s.execute(query);
            ResultSet rs = s.getResultSet();
            while((rs!=null) && (rs.next()))
            {
                upid=rs.getInt(1);
                StreetNum=rs.getInt(2);
                suburb=rs.getString(3);
                area=rs.getString(4);
                price=rs.getString(5);
                agentID= rs.getInt(6);
                numBeds=rs.getInt(7);
                numBaths=rs.getInt(8);
                spool=rs.getBoolean(9);
                numGarages=rs.getInt(10);
                date=rs.getString(11);
                ownerID=rs.getInt(12);
                SaleOrRent=rs.getString(13);
                size++;


              line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");
              obj= new LineObject(line);
              System.out.println(line);
              String out = obj.getLine();
              System.out.println(out);
            }



            // close and cleanup
            s.close();
            conn.close();
               }

        catch(Exception ex)
        {
            ex.printStackTrace();
        }



   }
    void initialFrame()
    {
        jf=new JFrame();
        jf.setSize (1300,700);
        jf.setTitle("Property Oracle | Results Page");
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

     void addComponents()
     {

         Container con = jf.getContentPane();
         con.setLayout(null);
         txtArea.setBounds(20,30,1200,600);
         con.add(txtArea);
         txtArea.setText("UPID\tStreetNum\tSuburb\tArea\tPrice\tAgentID\tBedrooms\tBathrooms\tSwimming Pool\tGarages\tDate\tOwner\tSale/Rent\n");
         out = obj.getLine();
         System.out.println(out);



     }

}

And here is the LineObject class: public class LineObject

{
    protected String line;


    public LineObject(String a)
    {
        line = a;
    }


    public String getLine()
    {
        return line;
    }

}
user992057
  • 21
  • 1
  • 5
  • Make it `public` or add an accessor (getter) method. Is it static, or does it belong to an instance of the GUI class? – superEb Sep 18 '13 at 19:05
  • The regular way is to create a `getter()` method for `line` in its class. If the other class is in the same package it can access `line` directly after instantiating the object of the class that defines `line`. – PM 77-1 Sep 18 '13 at 19:06

3 Answers3

1

I will assume your database access code runs in a separate thread, otherwise typical latency would block the event dispatch thread (EDT). Pass a reference to your JTextArea as a parameter to your database code. Use the reference to update the JTextArea on the EDT:

final String line = …
EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        ta.append(line);
    }
});

A related example is seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Make line a private class field of your class (the one that runs the while loop).

public LoopingClass {
  private String line;

  public void loopMethod() {
    line = //...
  }
}

Then create a public getter to this variable.

public LoopingClass {
  private String line;

  public void loopMethod() {
    line = //...
  }

  public String getLine() {
    return line;
  }
}

Then from your GUI, call the getter of the object instance.

// somewhere in your GUI class
loopingClassInstance.getLine();
superbob
  • 1,628
  • 13
  • 24
  • Tried this, however, when called it gives a null pointer exception. Thing is, I trace line and it definitely has data up until it gets called in the void addComponents method. – user992057 Sep 21 '13 at 08:50
  • To make the whole thing work, you must be sure that the used classes are correctly instanciated (`new LoopingClass` somewhere), then that you can actually access it from the calling class (GUI class), there should be something like `loop = new LoopingClass()` somewhere in the GUI class – superbob Sep 23 '13 at 08:22
0

Take a look at the MVC pattern: it's always good practice to decouple the business logic (putting data in a database and building the string "line") from the frontend (GUI).

By the way, since you're building the string by appending more data to it, you should consider using a StringBuilder instead:

StringBuilder lineBuilder = new StringBuilder();

// append data:
lineBuilder.append(someString);

// create a string only when you need it:
String line = lineBuilder.toString();

In this way you are not continuosly creating new strings (which can be expensive in the long run especially if the string keeps growing), but using the buffer provided by the StringBuilder and then creating an actual string only when you need it, e.g., when you need to update your JTextArea.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94