Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retain values between cards in an CardLayout?

Status
Not open for further replies.

rohithere

Programmer
Oct 27, 2003
51
0
0
IN
Hi,
How to retain values between cards in an CardLayout?
I have a report which shows certain records.This report has card Layout as its layout manager and each pages of the report are actually each card.
Now,I want to take the totals of 1st card(i.e. Report's Page)and add it with the totals of 2nd card and so on...

Note: The totals of each card are computed in that card.

PLEASE HELP ME AS IT IS VERY URGENT.


THANKS.
 
I don't understand your problem. ??

CardLayout is simply a way of laying out components so that you only see one at a time. They still exist within your program (unless you have some very odd code). Why not just get the values from each one. The component doesn't have to be visible for your program to access the values for it.

Perhaps you could expand on your problem a bit more.

Lentil
 
Hi,
the foll is the code:

class SalesReportStateCityWiseView extends JPanel implements Printable
{

SimpleDateFormat formatter = new SimpleDateFormat ("dd/MM/yyyy");
DecimalFormat numberformatter = new DecimalFormat("#0.00");
Font f;
FontMetrics fm;
int pageno;
String statename,fromdate,todate;
String[]companyinfo;
String[][]transinfo;
boolean print=false;
public SalesReportStateCityWiseView(int pageno,String statename, String[][]transinfo, String[]companyinfo, String fromdate,String todate)
{
//setSize(800,800);
this.pageno=pageno;
this.statename=statename;
this.transinfo=transinfo;
this.companyinfo=companyinfo;
this.fromdate=fromdate;
this.todate=todate;
setMinimumSize(new Dimension(900,3500));
setPreferredSize(new Dimension(900,3500));
}


public void paintBody(Graphics g){
f=new Font(null,Font.PLAIN,10);
g.setFont(f);
g.drawString ("Sales Analysis State/City Wise "+fromdate+" To "+todate,10,120);
//String acname=transactionhelper.getAcName(filerefno)[0];
g.drawString ("State Name : "+statename,10,135);
//f=new Font(null,Font.PLAIN,12);
//g.setFont(f);
fm = this.getFontMetrics(g.getFont());//get FontMatrics
g.drawLine(0,140,800,140);
g.drawString("City Name",10,155);
g.drawString("AMT.AFTER DIS.",160,155);
g.drawString("F.O.R.",310- fm.stringWidth("F.O.R"),155);
g.drawString("NET SALE",380-fm.stringWidth("NET SALE"),155);
g.drawLine(0,170,800,170);

int y=190;
int srno=1;

double amtafterdis=0;
double foramt=0;
double netsale=0;

// for (int i=((pageno*20)-20);i<transinfo.length ;i++ ){

for (int i=0;i<transinfo.length ;i++ ){
// if(i==(pageno*20))return;

g.drawString(transinfo[6],10,y);
double amt=Double.parseDouble(transinfo[0]);
amt-=Double.parseDouble(transinfo[1]);
amt-=Double.parseDouble(transinfo[2]);
amt-=Double.parseDouble(transinfo[3]);
amt-=Double.parseDouble(transinfo[4]);
g.drawString(numberformatter.format(amt),220 - fm.stringWidth(numberformatter.format(amt)),y);
amtafterdis+=amt;
double famt=Double.parseDouble(transinfo[5]);
g.drawString(numberformatter.format(famt),320 - fm.stringWidth(numberformatter.format(famt)),y);
foramt+=famt;
g.drawString(numberformatter.format(amt-famt),380 - fm.stringWidth(numberformatter.format(amt-famt)),y);
netsale+=(amt-famt);
y=y+20;
}
g.drawLine(0,y,800,y);
g.drawString(&quot;Grand Total&quot;,10,y+20);
g.drawString(numberformatter.format(amtafterdis),220 - fm.stringWidth(numberformatter.format(amtafterdis)),y+20);
g.drawString(numberformatter.format(foramt),320 - fm.stringWidth(numberformatter.format(foramt)),y+20);
g.drawString(numberformatter.format(netsale),380 - fm.stringWidth(numberformatter.format(netsale)),y+20);
g.drawLine(0,y+30,800,y+30);

}//End paintBody
public void paint(Graphics g){
if(print){
((Graphics2D)g).scale(1,1);
}else{
((Graphics2D)g).scale(1.5,1.5);
}
Dimension d=getSize();
g.setColor(Color.white);
g.fillRect(0,0,(int)d.getWidth(),(int)d.getHeight());
g.setColor(Color.black);
paintBody(g);
PaintHeader.paintHeader(companyinfo,g);
//paintHeader(g);
//paintFooter(g);
}//end paint

//Add method print(Graphics g,PageFormat pageFormat,int pages)for printing
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {

Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//setDoubleBuffered(false);
disableDoubleBuffering(this);
this.paint(g2d);
enableDoubleBuffering(this);
//setDoubleBuffered(true);
return(PAGE_EXISTS);


}//End print

public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}

/** Re-enables double buffering globally. */

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}//end file

Please help.

Thanks
 
WOW. I am not sure how to help since it looks like a 'due very soon assignment'. And I am still not sure what you are trying to do, and how you are trying to do it.

I see your problem though. You are not writing in an object oriented way. More procedural. Also you are mixing up the different types of code for different functions. You need to display the data so you pass in the information and then just throw it away. In this direction lies chaos.

You really should be thinking in terms of objects. For example you could have a Company class and an overall BusinessControl class. A single control class keeps a list of company objects. All the company information (cashflow, profits, names etc) should be encapsulated in each company object; and the interactions of companies should either be in the companies themselves or in the control class.

The following are a few general suggestions which you might be able to incorporate or at least use next time. This probably just reiterates the type of information in your text book.

1. For a start at the very least you need to separate out the business code from the GUI code. eg use the objects I suggested above. You keep all these objects for the life of the program then (or at least for as long as necessary).

2. You then write your GUI panel to display the information from a company passed to it.

3. Since all you seem to want to do is print text, why don't you use components like JLabels, JTextBoxes, etc It would make the interface much simpler.

4. At this point you don't actually need a card layout if all the companies have the same type of information. Just swap companies and get the panel to redraw itself. (Note you only need cardlayout if you need to display essentially a different set of information and you need a different layout etc)

5. Then you could get each company to print out its own data in a specified layout if necessary (Probably on a part of a single page.)

6. if you want the total data then your control class should be able to iterate over the companies and collect it all. Then print it out.(Also probably on a part of a single page.)


7. Without looking at all your code too closely I did notice that you used paint(Graphcis g). Since this is a Swing component (JPanel) then this should be changed to paintComponent(Graphics g) for custom painting.

Cheers

Andrew
 
Hi,
Well it is like this:
In SalesReport.java I retrieve the value from th db and based upon the values retrieved(stored in array) I determine the num of pages it will span for displaying.Then I pass those values to the above code file by calling its constructor in a for loop , having the length of the array the foll code wiil explain it:

String[][] analysisinfo = transactionhelper.getSalesAnalysisStateCityWise ((String)txtState.getSelectedItem(),txtFromM.getText()+&quot;/&quot;+txtFromD.getText()+&quot;/&quot;+txtFromY.getText(), txtToM.getText()+&quot;/&quot;+txtToD.getText()+&quot;/&quot;+txtToY.getText());

String [] companyinfo = transactionhelper.getCompanyHeaderInfo();
if(analysisinfo==null || companyinfo==null){
mb.errMsg(&quot;Error&quot;,&quot;No Records Found !!!&quot;, this);
return;
}
//mb.errMsg(&quot;Error&quot;,&quot;Error !!!&quot;+analysisinfo[0][0], this);
int pages = (int)(analysisinfo.length/32);

JFrame j = new JFrame(&quot;Sales Analysis Report - State-City Wise From &quot;+txtFromD.getText()+&quot;/&quot;+txtFromM.getText()+&quot;/&quot;+txtFromY.getText() +&quot; To &quot;+txtToD.getText()+&quot;/&quot;+txtToM.getText()+&quot;/&quot;+txtToY.getText());
JScrollPane sp[] = new JScrollPane[pages+1];
SalesReportStateCityWiseView viewform[]=new SalesReportStateCityWiseView[pages+1];
view = new JPanel();
card= new CardLayout();
view.setLayout(card);
next = new JButton(&quot;Next&quot;);
previous = new JButton(&quot;Previous&quot;);
print = new JButton(&quot;Print&quot;);
next.addActionListener(this);
previous.addActionListener(this);
print.addActionListener(this);
JPanel bottom =new JPanel();
bottom.setLayout( new FlowLayout(FlowLayout.LEFT,30,5));
bottom.add(next);
bottom.add(previous);
bottom.add(print);
for(int i=0;i<=pages;i++)
{
viewform=new SalesReportStateCityWiseView(i+1,(String)txtState.getSelectedItem(), analysisinfo, companyinfo, txtFromD.getText()+&quot;/&quot;+txtFromM.getText()+&quot;/&quot;+txtFromY.getText(), txtToD.getText()+&quot;/&quot;+txtToM.getText()+&quot;/&quot;+txtToY.getText());
viewform.print=false;
sp = new JScrollPane(viewform);
view.add(sp,i+&quot;&quot;);
}



... and the code of SalesReportStateCityWiseView :

the foll is the code:

class SalesReportStateCityWiseView extends JPanel implements Printable
{

SimpleDateFormat formatter = new SimpleDateFormat (&quot;dd/MM/yyyy&quot;);
DecimalFormat numberformatter = new DecimalFormat(&quot;#0.00&quot;);
Font f;
FontMetrics fm;
int pageno;
String statename,fromdate,todate;
String[]companyinfo;
String[][]transinfo;
boolean print=false;
public SalesReportStateCityWiseView(int pageno,String statename, String[][]transinfo, String[]companyinfo, String fromdate,String todate)
{
//setSize(800,800);
this.pageno=pageno;
this.statename=statename;
this.transinfo=transinfo;
this.companyinfo=companyinfo;
this.fromdate=fromdate;
this.todate=todate;
setMinimumSize(new Dimension(900,3500));
setPreferredSize(new Dimension(900,3500));
}


public void paintBody(Graphics g){
f=new Font(null,Font.PLAIN,10);
g.setFont(f);
g.drawString (&quot;Sales Analysis State/City Wise &quot;+fromdate+&quot; To &quot;+todate,10,120);
//String acname=transactionhelper.getAcName(filerefno)[0];
g.drawString (&quot;State Name : &quot;+statename,10,135);
//f=new Font(null,Font.PLAIN,12);
//g.setFont(f);
fm = this.getFontMetrics(g.getFont());//get FontMatrics
g.drawLine(0,140,800,140);
g.drawString(&quot;City Name&quot;,10,155);
g.drawString(&quot;AMT.AFTER DIS.&quot;,160,155);
g.drawString(&quot;F.O.R.&quot;,310- fm.stringWidth(&quot;F.O.R&quot;),155);
g.drawString(&quot;NET SALE&quot;,380-fm.stringWidth(&quot;NET SALE&quot;),155);
g.drawLine(0,170,800,170);

int y=190;
int srno=1;

double amtafterdis=0;
double foramt=0;
double netsale=0;

// for (int i=((pageno*20)-20);i<transinfo.length ;i++ ){

for (int i=0;i<transinfo.length ;i++ ){
// if(i==(pageno*20))return;

g.drawString(transinfo[6],10,y);
double amt=Double.parseDouble(transinfo[0]);
amt-=Double.parseDouble(transinfo[1]);
amt-=Double.parseDouble(transinfo[2]);
amt-=Double.parseDouble(transinfo[3]);
amt-=Double.parseDouble(transinfo[4]);
g.drawString(numberformatter.format(amt),220 - fm.stringWidth(numberformatter.format(amt)),y);
amtafterdis+=amt;
double famt=Double.parseDouble(transinfo[5]);
g.drawString(numberformatter.format(famt),320 - fm.stringWidth(numberformatter.format(famt)),y);
foramt+=famt;
g.drawString(numberformatter.format(amt-famt),380 - fm.stringWidth(numberformatter.format(amt-famt)),y);
netsale+=(amt-famt);
y=y+20;
}
g.drawLine(0,y,800,y);
g.drawString(&quot;Grand Total&quot;,10,y+20);
g.drawString(numberformatter.format(amtafterdis),220 - fm.stringWidth(numberformatter.format(amtafterdis)),y+20);
g.drawString(numberformatter.format(foramt),320 - fm.stringWidth(numberformatter.format(foramt)),y+20);
g.drawString(numberformatter.format(netsale),380 - fm.stringWidth(numberformatter.format(netsale)),y+20);
g.drawLine(0,y+30,800,y+30);

}//End paintBody
public void paint(Graphics g){
if(print){
((Graphics2D)g).scale(1,1);
}else{
((Graphics2D)g).scale(1.5,1.5);
}
Dimension d=getSize();
g.setColor(Color.white);
g.fillRect(0,0,(int)d.getWidth(),(int)d.getHeight());
g.setColor(Color.black);
paintBody(g);
PaintHeader.paintHeader(companyinfo,g);
//paintHeader(g);
//paintFooter(g);
}//end paint

//Add method print(Graphics g,PageFormat pageFormat,int pages)for printing
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {

Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//setDoubleBuffered(false);
disableDoubleBuffering(this);
this.paint(g2d);
enableDoubleBuffering(this);
//setDoubleBuffered(true);
return(PAGE_EXISTS);


}//End print

public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}

/** Re-enables double buffering globally. */

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}//end file

Please help.Because it is that urgent.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top