Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

773 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