Solved

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

Posted on 2004-10-16
4
1,032 Views
Last Modified: 2008-03-17
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
Comment
Question by:coffee_bean
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:JugglerW
ID: 12333912
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
 

Author Comment

by:coffee_bean
ID: 12334531
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
 
LVL 7

Accepted Solution

by:
JugglerW earned 250 total points
ID: 12336613
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
 

Author Comment

by:coffee_bean
ID: 12358554
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now