Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-16
4
Medium Priority
?
1,153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

715 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