Solved

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

Posted on 2004-10-16
4
1,079 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 117
sum13 challenge 24 134
seriesUp challenge 7 191
collection output issue 9 59
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

763 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