8 Replies - 1946 Views - Last Post: 24 July 2012 - 10:28 AM Rate Topic: -----

#1 suemarchant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-July 12

Basic Java - output report to scrolling window

Posted 22 July 2012 - 08:35 PM

Hi there. I'm in a (very) beginning java class. In my last assignment, I created an amortization program that pulled data about three homeowners from an input text file and generated an amortization schedule for each homeowner using system.out.println. For our next assignment, our instructor has asked us to modify our code so that it outputs to a scrolling pane in a window - nothing fancy, just a very basic scrolling window. He has given us almost no information at all about how to do this. We are using a book he wrote himself and this information is not covered in the text. He wants us to figure it out. He told us that we will need to use JFrame, JScrollPane, JTextArea, and jta.append, but that's about it.

My current report uses output from two methods - displayHeading, and displayReport. So basically I need to figure out how to get the output from each section into a window. I have an idea that I need to put my primary window code into my main method, but I'm not sure how I then go about combining the output from the two methods into one window.

I'm pretty stumped and wondering if someone could point me in the direction of some good related reading, examples, and/or tutorials that will teach me about how to do this.

Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Basic Java - output report to scrolling window

#2 SilverEleak  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 18-July 12

Re: Basic Java - output report to scrolling window

Posted 22 July 2012 - 08:48 PM

Could you post your code here?
Was This Post Helpful? 0
  • +
  • -

#3 suemarchant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-July 12

Re: Basic Java - output report to scrolling window

Posted 23 July 2012 - 06:16 AM

Sure!

I'm not sure how relevant it is - I haven't started revising it to output to a window yet. If you could point me in the direction of any tutorials or reading that will help me revise my code I'd really appreciate it. So far everything I can find by searching and googling has more to do with creating a GUI, and that's not really what he is asking for at this point. (He doesn't want us to create a GUI object.)

Thanks for any help you can ofer!


import amortization.InputFile;
import java.text.DecimalFormat;
import java.text.NumberFormat;


/**
 *
 * @author smarchant
 */
public class Amortization {

    static String name, address1, city, state;
    static int zip, month, day, year, code;
    static double amount=0, rate=0;

    public static void main(String[] args) {
     
        InputFile loaninfo;
        loaninfo = new InputFile("fcrc loan data.txt");

        while (!loaninfo.eof()) {
            name = loaninfo.readString();
            address1 = loaninfo.readString();
            city = loaninfo.readString();
            state = loaninfo.readString();
            zip = loaninfo.readInt();
            month = loaninfo.readInt();
            day = loaninfo.readInt();
            year = loaninfo.readInt();
            amount = loaninfo.readDouble();
            rate = loaninfo.readDouble();
            code = loaninfo.readInt();

            displayHeading(name, address1, city, state, zip, month, day, year, amount, code, rate);
            displayTable(amount);
        }
        
    }

    public static void displayTable(double amount) 
    {
        int paymentNumber;

        double payment=0, interest, principal, balance;

        DecimalFormat numFormat;
        numFormat = new DecimalFormat("0.00");
        NumberFormat currencyFormat;
        currencyFormat = NumberFormat.getCurrencyInstance();
        balance = amount;
        paymentNumber = 0;
        double totalPayment = 0;
        double totalIntPaid = 0;
        while (balance > 0) 
        {

            ++paymentNumber;
            String dueDate = calcDueDate(month, day, year, paymentNumber - 1);
            interest = calcInterest(balance, rate);
            if (balance<=payment) 
            {
                payment = balance;
                principal = balance;
                balance = 0;
            } 
            else 
            {
                payment = calcPayment(code, amount);
                principal = calcPrincipal(payment, interest);
                balance = calcBalance(balance, principal);
            }
           totalPayment = totalPayment + payment;
           totalIntPaid = totalIntPaid + interest;
           System.out.println(paymentNumber + "\t\t" + dueDate + "\t\t$" + numFormat.format(payment) + "\t\t" + currencyFormat.format(interest) + "\t\t" + currencyFormat.format(principal) + "\t\t" + currencyFormat.format(balance));

        }
          System.out.println("");
          System.out.println(" " + "\t\t" + " " + "\t\t" + "Totals: " +currencyFormat.format(totalPayment)+ "\t" + currencyFormat.format(totalIntPaid));
          System.out.println("");
          System.out.println("");
    }

    public static void displayHeading(String name, String address1, String city, String state, int zip, int month, int day, int year, double amount, int code, double rate) 
    {
        DecimalFormat numFormat;
        numFormat = new DecimalFormat("0.00");
        NumberFormat currencyFormat;
        currencyFormat = NumberFormat.getCurrencyInstance();
      
        System.out.println("First Community Redevelopment Corporation");
        System.out.println("101 1st Street");
        System.out.println("Bloomington, TN 41663");
        System.out.println("");
        System.out.println("\t\t\t\t" + name);
        System.out.println("\t\t\t\t" + address1);
        System.out.println("\t\t\t\t" + city + ", " + state + ",  " + zip);
        System.out.println("");
        System.out.println("LOAN AMOUNT:\t\t" + currencyFormat.format(amount));
        System.out.println("INTEREST RATE:\t\t" + numFormat.format(rate) + "%");
        System.out.println("");
        System.out.println("Payment #\tDue Date\t\tPayment\t\tInterest\tPrincipal\tBalance");
        System.out.println("---------------------------------------------------------------------------------------------------");
    }

    public static double calcPayment(int code, double amount) 
    {
        double payment;
        payment = 0;

        switch (code) {
            case 0:
                payment = 50.00;
                break;
            case 1:
                payment = 55.00;
                break;
            case 2:
                payment = 75.00;
                break;
            case 3:
                payment = 100.00;
                break;
            case 4:
                payment = .05 * amount;
                break;
            case 5:
                payment = .06 * amount;
                break;
            case 6:
                payment = .05 * amount;
                break;
            case 7:
                payment = .04 * amount;
                break;
            case 8:
                payment = .03 * amount;
                break;
            case 9:
                payment = .02 * amount;
                break;
            default:
                System.out.println("Bad payment code");
        }
        return payment;
    }

    public static double calcInterest(double balance, double rate) 
    {
        double interest;
        interest = rate / 12 * balance;
        return interest;
    }

    public static double calcPrincipal(double payment, double interest) 
    {
        double principal;

        principal = payment - interest;
        return principal;
    }

    public static double calcBalance(double balance, double principal) 
    {
        balance = balance - principal;
        return balance;
    }

    public static String calcDueDate(int month, int day, int year, int payNum) 
    {
        String monthStr = "";
        month = month + payNum -1;
        year = year + (month /12);
        month = month % 12;
        if(month ==0)year--;
        switch (month) 
        {
            case 1:
                monthStr = "Jan.";
                break;
            case 2:
                monthStr = "Feb.";
                break;
            case 3:
                monthStr = "Mar.";
                break;
            case 4:
                monthStr = "Apr.";
                break;
            case 5:
                monthStr = "May";
                break;
            case 6:
                monthStr = "Jun.";
                break;
            case 7:
                monthStr = "Jul.";
                break;
            case 8:
                monthStr = "Aug.";
                break;
            case 9:
                monthStr = "Sept.";
                break;
            case 10:
                monthStr = "Oct.";
                break;
            case 11:
                monthStr = "Nov.";
                break;
            case 0:
                monthStr = "Dec.";
                break;
            default:
                System.out.println("Invalid month: "+month);

        }
        return monthStr + " " + day + ", " + year;
    }
}




Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: Basic Java - output report to scrolling window

Posted 23 July 2012 - 06:38 AM

He just wants you to start gui programming without any introduction to it at all? wow that is rough.

I found a tutorial here on DIC, you can see how to make a window and use it, but I don't think it explains too much of the basics
tutorial

I'm sure you can find many tutorials using google.

The JFrame will be your window. The JFrame has a container of components(that you add to it). These components could be buttons, textareas, scrollpane etc.. The JFrame, and any other component with a container, has a layoutmanager that it uses. The layoutmanager decides how to layout components on the screen.
I think you should first read about how to setup a JFrame, and then read about the different layoutmanagers.
Hint: Use BorderLayout and add your JScrollPane to the CENTER region. The textarea should be added to the scrollpane.
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Basic Java - output report to scrolling window

Posted 23 July 2012 - 07:05 AM

Quote

He told us that we will need to use JFrame, JScrollPane, JTextArea, and jta.append, but that's about it.

This may not be as bad as it sounds, but it is a leap to get from where you are to where you need to be.

As already indicated, independently from the code you've posted, learn to program a JFrame. Then to the JFrame, you'll add a JTextArea that is added to a JScrollPane. Since the default layout for a JFrame is BorderLayout, the JScrollPane will be added to to the JFrame at BorderLayout.CENTER by default and will fill the whole JFrame.

Once that's done, you can add your logic above and redirect output to the JTextArea using the JTextArea.append() method as the instructor suggested. So, roughly (and way over-simplified) every System.out.println() statement you have above will instead be appended to the JTextArea using the append() method. I haven't spent much time figuring out how much work that will be, but it doesn't seem that bad.

In addition to the above suggestions, Oracle's JFrame tutorial might be useful. Just don't forget to tell the instructor how great his book was at getting you through this project.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Basic Java - output report to scrolling window

Posted 23 July 2012 - 11:38 AM

View Postsuemarchant, on 23 July 2012 - 09:16 AM, said:

Sure!

I haven't started revising it to output to a window yet. If you could point me in the direction of any tutorials or reading that will help me revise my code I'd really appreciate it. So far everything I can find by searching and googling has more to do with creating a GUI, and that's not really what he is asking for at this point. (He doesn't want us to create a GUI object.)

A Window is a GUI... might be a really basic GUI with a JFrame containing a JScrollPane containing a JTextArea so 3 Swing components. And you can append your text to the JTextArea
Was This Post Helpful? 0
  • +
  • -

#7 suemarchant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-July 12

Re: Basic Java - output report to scrolling window

Posted 24 July 2012 - 04:46 AM

Hi there! Thanks very much for sending me in the right direction. I think I'm making progress. I now have the output going to a window - problem is, it's going to TWO windows - one for my displayHeading method and one for displayTable. I know this is because I'm putting the window code in each method instead of my main method, but - I can't figure out how to put it in my main method and call it out from within displayHeading and displayTable. I'm not sure what code needs to put in my displayHeading and displayTable methods, and what goes in the main definition. Any advice?

Here is my updated code:


import amortization.InputFile;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


/**
 *
 * @author smarchant
 */
public class Amortization {

    static String name, address1, city, state;
    static int zip, month, day, year, code;
    static double amount=0, rate=0;

    public static void main(String[] args) {
     
        InputFile loaninfo;
        loaninfo = new InputFile("fcrc loan data.txt");
        
        JFrame jf;
        JTextArea jta;
        JScrollPane jsp;
        
        jf = new JFrame();
        jta = new JTextArea();
        jsp = new JScrollPane(jta);
        
        jf.setSize(1200,350);
        jf.setLocation(400,250);
        
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        jf.add(jsp);
        jf.setVisible(true);
        
        while (!loaninfo.eof()) {
            name = loaninfo.readString();
            address1 = loaninfo.readString();
            city = loaninfo.readString();
            state = loaninfo.readString();
            zip = loaninfo.readInt();
            month = loaninfo.readInt();
            day = loaninfo.readInt();
            year = loaninfo.readInt();
            amount = loaninfo.readDouble();
            rate = loaninfo.readDouble();
            code = loaninfo.readInt();

            displayHeading(name, address1, city, state, zip, month, day, year, amount, code, rate);
            displayTable(amount);
            
              
        
        
        }
        
    }

    public static void displayTable(double amount) 
    {
        int paymentNumber;

        double payment=0, interest, principal, balance;
                
        JFrame jf;
        JTextArea jta;
        JScrollPane jsp;
        
        jf = new JFrame();
        jta = new JTextArea();
        jsp = new JScrollPane(jta);
        
        jf.setSize(1200,350);
        jf.setLocation(400,250);
        
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        jf.add(jsp);
        jf.setVisible(true);

        DecimalFormat numFormat;
        numFormat = new DecimalFormat("0.00");
        NumberFormat currencyFormat;
        currencyFormat = NumberFormat.getCurrencyInstance();
        balance = amount;
        paymentNumber = 0;
        double totalPayment = 0;
        double totalIntPaid = 0;
        while (balance > 0) 
        {

            ++paymentNumber;
            String dueDate = calcDueDate(month, day, year, paymentNumber - 1);
            interest = calcInterest(balance, rate);
            if (balance<=payment) 
            {
                payment = balance;
                principal = balance;
                balance = 0;
            } 
            else 
            {
                payment = calcPayment(code, amount);
                principal = calcPrincipal(payment, interest);
                balance = calcBalance(balance, principal);
            }
           totalPayment = totalPayment + payment;
           totalIntPaid = totalIntPaid + interest;
           jta.append(paymentNumber + "\t\t" + dueDate + "\t\t$" + numFormat.format(payment) + "\t\t" + currencyFormat.format(interest) + "\t\t" + currencyFormat.format(principal) + "\t\t" + currencyFormat.format(balance)+ "\n");

        }
          jta.append("\n");
          jta.append(" " + "\t\t" + " " + "\t\t" + "Totals: " +currencyFormat.format(totalPayment)+ "\t" + currencyFormat.format(totalIntPaid) + "\n");
          jta.append("\n\n");

    }

    public static void displayHeading(String name, String address1, String city, String state, int zip, int month, int day, int year, double amount, int code, double rate) 
    {
        DecimalFormat numFormat;
        numFormat = new DecimalFormat("0.00");
        NumberFormat currencyFormat;
        currencyFormat = NumberFormat.getCurrencyInstance();
        
        JFrame jf;
        JTextArea jta;
        JScrollPane jsp;
        
        jf = new JFrame();
        jta = new JTextArea();
        jsp = new JScrollPane(jta);
        
        jf.setSize(1200,350);
        jf.setLocation(400,250);
        
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        jf.add(jsp);
        jf.setVisible(true);
      
        jta.append("First Community Redevelopment Corporation\n");
        jta.append("101 1st Street\n");
        jta.append("Bloomington, TN 41663\n");
        jta.append("\n");
        jta.append("\t\t\t\t" + name + "\n");
        jta.append("\t\t\t\t" + address1 + "\n");
        jta.append("\t\t\t\t" + city + ", " + state + ",  " + zip + "\n");
        jta.append("\n");
        jta.append("LOAN AMOUNT:\t\t" + currencyFormat.format(amount) + "\n");
        jta.append("INTEREST RATE:\t\t" + numFormat.format(rate) + "%\n");
        jta.append("\n");
        jta.append("Payment #\tDue Date\t\tPayment\t\tInterest\tPrincipal\tBalance\n");
        jta.append("---------------------------------------------------------------------------------------------------\n");
    }

    public static double calcPayment(int code, double amount) 
    {
        double payment;
        payment = 0;

        switch (code) {
            case 0:
                payment = 50.00;
                break;
            case 1:
                payment = 55.00;
                break;
            case 2:
                payment = 75.00;
                break;
            case 3:
                payment = 100.00;
                break;
            case 4:
                payment = .05 * amount;
                break;
            case 5:
                payment = .06 * amount;
                break;
            case 6:
                payment = .05 * amount;
                break;
            case 7:
                payment = .04 * amount;
                break;
            case 8:
                payment = .03 * amount;
                break;
            case 9:
                payment = .02 * amount;
                break;
            default:
                System.out.println("Bad payment code");
        }
        return payment;
    }

    public static double calcInterest(double balance, double rate) 
    {
        double interest;
        interest = rate / 12 * balance;
        return interest;
    }

    public static double calcPrincipal(double payment, double interest) 
    {
        double principal;

        principal = payment - interest;
        return principal;
    }

    public static double calcBalance(double balance, double principal) 
    {
        balance = balance - principal;
        return balance;
    }

    public static String calcDueDate(int month, int day, int year, int payNum) 
    {
        String monthStr = "";
        month = month + payNum -1;
        year = year + (month /12);
        month = month % 12;
        if(month ==0)year--;
        switch (month) 
        {
            case 1:
                monthStr = "Jan.";
                break;
            case 2:
                monthStr = "Feb.";
                break;
            case 3:
                monthStr = "Mar.";
                break;
            case 4:
                monthStr = "Apr.";
                break;
            case 5:
                monthStr = "May";
                break;
            case 6:
                monthStr = "Jun.";
                break;
            case 7:
                monthStr = "Jul.";
                break;
            case 8:
                monthStr = "Aug.";
                break;
            case 9:
                monthStr = "Sept.";
                break;
            case 10:
                monthStr = "Oct.";
                break;
            case 11:
                monthStr = "Nov.";
                break;
            case 0:
                monthStr = "Dec.";
                break;
            default:
                System.out.println("Invalid month: "+month);

        }
        return monthStr + " " + day + ", " + year;
    }
}




Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Basic Java - output report to scrolling window

Posted 24 July 2012 - 05:01 AM

You're currently creating 3 JFrames with 3 sets of components, one set in each of the main(), displayTable() and displayHeading() methods. The concept of how to correct is to create a single container with a single set of components that will be used by all methods to present the data to the user.

This can be most satisfactorily achieved while adhering to OOP principles by reducing your main() method to a single line or two that simply calls the constructor of a single JFrame which defines and constructs the display portion of the interface and then calls methods that get the data from the file and table and display it in the constructed display.

I can't go deeper than that at this time, but I'm sure others will.
Was This Post Helpful? 0
  • +
  • -

#9 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Basic Java - output report to scrolling window

Posted 24 July 2012 - 10:28 AM

The problem here has to do with variable scope. The methods, displayHeader and displayTable, cannot reference variables in the main methods scope. You don't want to have to modify the methods to accept parameters. What you really should do is try to move away from all of those static keywords.

import amortization.InputFile;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * 
 * @author smarchant
 */
public class Amortization {

	// First fix: remove all static modifiers. Now all of your variables require
	// a Amortization object to be referenced.
	String name, address1, city, state;
	int zip, month, day, year, code;
	double amount = 0, rate = 0;

	// add a reference to the GUI
	JFrame frame;
	JScrollPane scrollPane;
	JTextArea textArea;
	// or even better make a subclass of JFrame for this assignment to handle
	// these

	public Amortization() {
		// Setup the GUI here, initialize any variables
	}

	public static void main(String[] args) {
		Amortization amort = new Amortization();
		amort.go();
	}

	public void go() {
	/*
	 * This is the method you want to put most of your old main method stuff
	 * into. You can call all non-static methods as you did previously with
	 * static ones, because in this method they belong to an object. (this
	 * object)
	 * 
	 * PS: Consider a better name for this method.
	 */
	}

	// notice no static keyword
	public void displayTable(double amount) {

		// ...
	}

	// notice no static keyword
	public void displayHeading(String name, String address1, String city,
			String state, int zip, int month, int day, int year, double amount,
			int code, double rate) {

		// ...

	}
}


Now you can reference the GUI, specifically the textArea because it is a field of the object. The static modifiers are something you do because you can't call your own methods from main() without being attached to an object, and we don't learn how to create/design objects properly early enough, imo.

The main method is short and sweet and that's what we want. However, this is not ideal, there are tons of things you can do to improve it, but it is easily translated over from your old code. Let us know how you do.

This post has been edited by Sheph: 24 July 2012 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1