Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1188
  • Last Modified:

(Java) How to display multiple row of column data into one JTextArea?

A pc has multiple special software(no standard) and can be setup with more than 1 addition peripheral.

How to get SWDesc(software Description) and PeripDesc(Peripheral Description) which has multiple record for 1 PC to display in a single entry into their respective JTextArea which is in my GUIForm.
eg
For Software JTextArea
MSVB, Oracle
For PeripDesc JTextArea
Cd-Writer, Zip Drive

I can only display the last record for SWdesc and PeripDesc the W01 workstation. ie For Software JTextArea only Oracle is display instead of 'MSVB, Oracle'. Same for PeripDesc

The following result were returned after querying resource id W01.

RID  Location Status PCDesc          SWDesc  PeripDesc
W01 L5-201  avail    HP Pentium4   MSVB     Cd-writer
W01 L5-201  avail    HP Pentium 4  Oracle    Zip Drive
W01 L5-201  avail    HP Pentium 4  MSVB     Cd-writer
W01 L5-201  avail    HP Pentium 4  Oracle    Zip Drive

//*****GUIForm*****/

BookingController ac = new BookingController();
Desktop desk;
String id= searchTf.getText();
ArrayList alist = ac.retrieveDesktopDetails(id);
String desktop1="", locat=""; String pcDescription="";String swDescription=""; String peripDescription="";
String status="";

for ( i=0; i<alist.size(); i++)
{      
desk=(Desktop)alist.get(i);
desktop1 = desk.getResourceId();
locat = desk.getLocation();
status= desk.getDesktopStatus();
pcDescription=desk.getComputerDesc();
swDescription =desk.getSoftwareDesc();
peripDescription=desk.getPeripheralDesc();
}      
desktopIdTf.setText(desktop1);
locationTf.setText(locat);
statusTf.setText(status);
pcDescTa.setText(pcDescription);
softwareTa.setText(swDescription) ;
peripheralTa.setText(peripDescription);
}

/******BookingController.java*******/

 public static ArrayList retrieveDesktopDetails2(String rId)
{ArrayList alist= new ArrayList();
 
try {
DBController dbc = new DBController();
dbc.setUp("Database");
String query ="SELECT desktop.resourceId, desktop.location, desktop.status, pc.pcDesc, software.SWDesc, peripherals.PeripDesc
FROM (((desktop INNER JOIN pc ON desktop.resourceId = pc.resourceId) INNER JOIN pcsoftware ON pc.pcId = pcsoftware.pcId) INNER JOIN software ON pcsoftware.softwareId = software.softwareId) INNER JOIN peripherals ON pc.pcId = peripherals.pcId
WHERE (((desktop.resourceId)='"+rId+"') AND ((desktop.active)=1));";
        
ResultSet rs = null;
ResultSet rs2 = null;
rs = dbc.Rrequest(query);
      
boolean gotIt = rs.next();
        
while (gotIt)
{      
String resourceId= rs.getString("resourceId");
String location = rs.getString("location");
String status = rs.getString("status");
String pcDescript = rs.getString("pcDesc");
String softw = rs.getString("softwareDesc");
String perip =rs.getString("PeripDesc");
Desktop d = new Desktop(resourceId, location, status, pcDescript, softw,perip);
alist.add(d);
gotIt = rs.next();      
}
dbc.terminate();
}
catch (Exception e) {
System.out.println("Error in retrieving desktop record.");
}
return alist;      
}
0
coffee_bean
Asked:
coffee_bean
  • 2
  • 2
1 Solution
 
JugglerWCommented:
Try to use JTextArea.append() instead of setText(). SetText() replaces all what is in your text area with the new string, what you don not want as I understand your code?

Hope this helps.
0
 
coffee_beanAuthor Commented:
Thanks :) but the JTextArea keep appending the same record. And not the next record as well.

eg softwareDesc JTextArea is display as:

Oracle Oracle Oracle instead of what I wanted MSVB Oracle

Probably has got to do with the way the result set from the controller is retrieving the record or the GUIForm in calling and displaying the records.

Hmm...maybe something to do with the looping too. I don't really know.

Hope u can really help me. Been stuck for many days on this problem.  Thanks.

0
 
JugglerWCommented:
Think problem is your loop:

for ( i=0; i<alist.size(); i++)
{    
desk=(Desktop)alist.get(i);
desktop1 = desk.getResourceId();
locat = desk.getLocation();
status= desk.getDesktopStatus();
pcDescription=desk.getComputerDesc();
swDescription =desk.getSoftwareDesc();
peripDescription=desk.getPeripheralDesc();
}    
desktopIdTf.setText(desktop1);
locationTf.setText(locat);
statusTf.setText(status);
pcDescTa.setText(pcDescription);
softwareTa.setText(swDescription) ;
peripheralTa.setText(peripDescription);
}

The SetTExt calls are outside of the loop, so they just get the last result in alist.

Try this e.g.

for ( i=0; i<alist.size(); i++)
{    
desk=(Desktop)alist.get(i);
desktop1 += desk.getResourceId() + ", ";
locat += desk.getLocation()  + ", ";
status += desk.getDesktopStatus()  + ", ";
pcDescription += desk.getComputerDesc()  + ", ";
swDescription += desk.getSoftwareDesc()  + ", ";
peripDescription += desk.getPeripheralDesc()  + ", ";
}    
desktopIdTf.setText(desktop1);
locationTf.setText(locat);
statusTf.setText(status);
pcDescTa.setText(pcDescription);
softwareTa.setText(swDescription) ;
peripheralTa.setText(peripDescription);
}

If this does not help put a System.out.println in your fetch loop (while (gotIt) ) and check what your DB returns.

0
 
coffee_beanAuthor Commented:
Hi JugglerW, thanks so much for your help. It works! :)

I have similar question that I too have been stuck for many days. But this one involves the JTable. Please help.

http://www.experts-exchange.com/Programming/Programming_Languages/Q_21175562.html
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now