Results 1 to 11 of 11
  1. #1
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default Output to JScrollPane

    I am writing a program for school to calculate a mortage and output it using a GUI. I've gotten most of it figured out except how to output my text to a JScrollPane I'm attaching my code below, which should compile fine (but if someone wouldn't mind compiling it to verify that it compiles elsewhere than on my machine I'd appreciate it). The math is wrong I know, but I can figure that out, what I can't figure out is how to have the output go to the JScrollPane I've created in my GUI. If someone can show me how to do that in a way that wouldn't be doing my work for me I'd appreciate it. I don;t want anyone to do the homework for me, but if someone could give me a different example on how to output to a JScrollPane that is perhaps minimally related I'd really appreciate it!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.text.*;
    
    
    public class MotgageCalcGUI extends javax.swing.JFrame {
    
        /** Creates new form MotgageCalcGUI */
        public MotgageCalcGUI() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        //Inititalize components
        private void initComponents() {
    
            jLabel2 = new JLabel();
            amountTextField = new JTextField();
            termTextField = new JTextField();
            interestTextField = new JTextField();
            loanJLabel = new JLabel();
            termJLabel = new JLabel();
            interestJLabel = new JLabel();
            instructionsJLabel1 = new JLabel();
            instructionsJLabel2 = new JLabel();
            calculateJButton = new JButton();
            instructionsJLabel3 = new JLabel();
            outputJScrollPane = new JScrollPane();
            instructionsJLabel4 = new JLabel();
    
            jLabel2.setText("jLabel2");
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setTitle("Mortgage Calculator");
    
            loanJLabel.setText("Loan Amount");
    
            termJLabel.setText("Term");
    
            interestJLabel.setText("Interest Rate");
    
            instructionsJLabel1.setText("This program will calculate an Amoritization Table for any loan which uses a compounding interest, where the payment terms are the same as the compounding terms");
    
            instructionsJLabel2.setText("Please enter the amount of the loan, without comma or dollar sign, the term as number of years, and the interest rate as a decimal number without a percent sign");
    
            calculateJButton.setText("Calculate");
            calculateJButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent calc) {
                    calculateJButtonActionPerformed(calc);
                }
            });
    
            instructionsJLabel3.setText("When you are done entering your specifics please press the button below.");
    
            instructionsJLabel4.setText("The amoritization table will show below");
    
            GroupLayout layout = new GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                            .addComponent(outputJScrollPane, GroupLayout.Alignment.LEADING)
                            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                    .addComponent(amountTextField)
                                    .addComponent(loanJLabel, GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                    .addComponent(termJLabel)
                                    .addComponent(termTextField, GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                    .addComponent(interestTextField)
                                    .addComponent(interestJLabel)))
                            .addComponent(instructionsJLabel1, GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(instructionsJLabel2, GroupLayout.Alignment.LEADING)
                            .addComponent(instructionsJLabel3, GroupLayout.Alignment.LEADING)
                            .addComponent(calculateJButton, GroupLayout.Alignment.LEADING))
                        .addComponent(instructionsJLabel4))
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(instructionsJLabel1)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(instructionsJLabel2)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(amountTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(loanJLabel))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(termTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(termJLabel))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(interestTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(interestJLabel, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)))
                    .addGap(11, 11, 11)
                    .addComponent(instructionsJLabel3)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(calculateJButton)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(instructionsJLabel4)
                    .addGap(8, 8, 8)
                    .addComponent(outputJScrollPane, GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }
        
        // Variables for JFrame
        private JScrollPane outputJScrollPane;
        private JTextField amountTextField;
        private JButton calculateJButton;
        private JLabel instructionsJLabel1;
        private JLabel instructionsJLabel2;
        private JLabel instructionsJLabel3;
        private JLabel instructionsJLabel4;
        private JLabel interestJLabel;
        private JTextField interestTextField;
        private JLabel jLabel2;
        private JLabel loanJLabel;
        private JLabel termJLabel;
        private JTextField termTextField;
        // End of variables
        
        //Declare variables which make up loan
        double interestRate;
        double term;
        double loanAmount;
      
      
        //Constructor to create Mortgage instance
        public void MortageCalc (double iR, double t, double lA)
        {
          interestRate = iR;
          term = t;
          loanAmount = lA;
        }
      
        //Method to determine Monthly payment
            double payment() {
                return loanAmount * ((interestRate/12) * (Math.pow((1+(interestRate/12)),(term * 12))))/(Math.pow((1+(interestRate/12)),(term * 12) )-1); //Monthrate = loan * [c(1+c)^n]/[(1 + c)^n -1] Determine monthly rate
            }
            //Method to determine First Interest Payment
            double monthInterest(){
              return (payment() - (loanAmount * interestRate/12));
            }
            //Method to Determine First Principle Payment
            double monthPrinciple(){
              return (payment() - monthInterest());
            }
                                  
            
      public class MortgageBalance {
              double interest, principle, payment, loanRemainder;
              MortgageBalance(double intr, double princ, double pay, double loanR)
              {
                interest = intr;
                principle = princ;
                payment = pay;
                loanRemainder = loanR;
              }
              
              
              //Calculate interest payed per payment
            double interest() {
              return loanRemainder * (.0535/12);
            }
            
            //Calculate principle payed per payment
            double principle() {
              return payment - interest;
            }
            //Calculate loan remainder
            double loanRemainder(){
              return ((200000 * (1 + (.0535/12))) - payment);
            }
            //Calculate loan remainder after first run
            double loanBalance(){
            return ((loanRemainder * (1 + (.0535/12))) - payment);
            }
            
      }
    
        private void calculateJButtonActionPerformed(ActionEvent calc) {
          //Declare variables
          double payment1;
          double customInterest = Double.parseDouble(interestTextField.getText());
          double customTerm = Double.parseDouble(termTextField.getText());
          double customLoan = Double.parseDouble(amountTextField.getText());
      
          //Create array with loan variables
          double [] loans = new double [3];
          loans [0] = customInterest;
          loans [1] = customTerm;
          loans [2] = customLoan;
          
          //Create new loan instances and calculate monthly payments
          MortageCalc m1 = new MortageCalc (loans [0], loans [1], loans [2]);
          payment1 = m1.payment();
          
          DecimalFormat df = new DecimalFormat("$###,###.##");
          
          double loanBalance = loans[2];
          double interest1 = m1.monthInterest();
          double principle1 = m1.monthPrinciple();
          int loancounter = 1;
      
          //While loop to create amoritization table
          while (loanBalance > 0 && loancounter <85)
          {
            MortgageBalance balance = new MortgageBalance(interest1, principle1, payment1, loanBalance);
            //begin output and then calculate new variables
            System.out.println(loancounter + "               " + df.format(payment1) + "       " + df.format(interest1) + "         " + df.format(principle1) + "          " + df.format(loanBalance));
            interest1 = balance.interest();
            principle1 = balance.principle();
            loanBalance= balance.loanBalance();
            loancounter++;
        }
        }
    
        //Main method to dispatch on seperate thread
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MotgageCalcGUI().setVisible(true);
                }
            });
        }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Wait, what? What did I do wrong here? It can't compile from a .java file? I guess I am not understanding something, can you explain for me, I am totally new to Java and still trying to learn.. Thank you and sorry for my slowness...

  4. #4
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    It has something to do with the constructor for the MortgageCalc right? Can I redo the public void MortgageCalc constructor as a class?
    Last edited by gaborn415; 03-19-2011 at 07:21 AM. Reason: Poor wording

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Go through the third link in Fubarable's signature.

    db

  6. #6
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Thanks I'll go through and repost.

  7. #7
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Ok, I've simplified the example and it should compile without problem as well. How do I code the below example so that when the button is pushed the text "You've pushed a button" outputs the the JScrollPane? Thanks for the help!

    Java Code:
    public class NewJFrame extends javax.swing.JFrame {
    
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(148, 148, 148)
                            .addComponent(jButton1))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            System.out.println("You've Pushed a button");
        }
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
    }

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    no, the thing is, you need to tell us what the problem is with outputting your text. is it that you don't know how to output text?

    text can be output to labels (JLabel) or text fields or text areas, the last 2 having the option of accepting user input. if you don't need user input, just use JLabel. to edit a JLabel, you need to make a statement calling its variable name as you would with any variable, and use the JLabel methods to alter the properties.

    Java Code:
    private JLabel labelLoanBalance = new JLabel();
    ...
    Double thisLoanBalance = loanBalance();
    String s1 = String.format("£ %.2f",thisLoanBalance);
    labelLoanBalance.setText(s1);

  9. #9
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    I don't know how to output text to a JScrollPane. I want to output a long list of text, longer than the screen, or the JFrame. So I want to use a scrollpane so it can all go there and the user can scroll down to read it all. Is there some other way to do this? Can I put a JTextField in a JScrollPane and have the text go there? If I do that can the user then use the JScrollPane to scroll down the list?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    a JScrollPane is only a container, you need to add text areas or text labels or some other text "boxes" to output the text to, and add those text containers to the JScrollPane.

Similar Threads

  1. Help with jScrollPane
    By weikang in forum SWT / JFace
    Replies: 1
    Last Post: 02-04-2011, 03:30 AM
  2. Need Help With JScrollPane Please
    By MemorableSmile in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 02:39 AM
  3. JScrollPane
    By hiddenpremise in forum AWT / Swing
    Replies: 2
    Last Post: 12-25-2010, 05:15 AM
  4. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •