Results 1 to 3 of 3
  1. #1
    mbutler755 is offline Member
    Join Date
    Jul 2010
    Posts
    13
    Rep Power
    0

    Default Window needs to be re-sized to show components?

    I have built a program for my class assignment. There is one thing driving me nuts though: The form will not show any components unless it is re-sized. The funny thing is that you don't even need to re-size it at all; if you just put the mouse on the side of the window and wait for the double arrows and click, the components will show up. Any ideas are much appreciated. Here is my code:

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.text.DecimalFormat;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    
    
    public class Main {
    
        public static void main(String[] args) {
    
            // Create the Frame
            JFrame myFrame=new JFrame();                                // Create the frame
            myFrame.setSize(250,400);                                   // Set the size of the frame
            myFrame.setTitle("Matt's Calculator");                      // Set the title of the frame
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     // Set the default close operation
            myFrame.setVisible(true);                                   // Set the frame to be visible
    
            // Create the Menu bar for the program
            JMenuBar menuBar;                                           // Create the menu bar
            JMenu menu;
            JRadioButtonMenuItem rbMenuItem;                            // Create radio buttons for the menu
            
            //Create the menu bar.
            menuBar = new JMenuBar();                                   // Instantiate the menu
    
            //Build the first menu.
            menu = new JMenu("Loans");                                  // Create the menu and call it 'Loans"
            menu.setMnemonic(KeyEvent.VK_L);                            // Allow the menu to be called by pressing ALT + L
            menuBar.add(menu);                                          // Add the menu to the menubar
    
            //Create the panel for a custom loan
            JPanel customPanel=new JPanel(new GridLayout(8,2,5,5));                         // Create a custom panel with an integrated GridLayout
    
            JLabel lblMortgageAmount=new JLabel("Amount to finance: ");                     // Create a label for the mortgage amount
            final JTextField txtMortgageAmount=new JTextField("200000");                    // Create a text field for the mortgage amount
            customPanel.add(lblMortgageAmount);                                             // Add the label and text field to the panel
            customPanel.add(txtMortgageAmount);
    
            JLabel lblInterestRate=new JLabel("InterestRate (%): ");                        // Create a label for the interest rate
            final JTextField txtInterestRate=new JTextField("5.75");                        // Create a text field for the interest rate
            customPanel.add(lblInterestRate);                                               // Add the label and the text field to the panel
            customPanel.add(txtInterestRate);
    
            JLabel lblLoanLength=new JLabel("Loan Length (in years): ");                    // Create a label for the loan length
            final JTextField txtLoanLength=new JTextField("30");                            // Create a text field for the loan length
            customPanel.add(lblLoanLength);                                                 // Add the label and the textfield to the panel
            customPanel.add(txtLoanLength);
    
    
            //a group of radio button menu items
            ButtonGroup group = new ButtonGroup();                      					// Create a button group for the radio buttons
    
    
            JRadioButtonMenuItem lowLoan = new JRadioButtonMenuItem("5.35% @ 7 years");     // Add a radio button for 5.35% @ 7 years
            lowLoan.setMnemonic(KeyEvent.VK_7);                                             // Add the ability to call it by using the "7" key
            group.add(lowLoan);                                                             // Add the radio box to the button group
            menu.add(lowLoan);                                                              // Add the radio box to the menu
            class lowLoanListener implements ActionListener{                                // Class for the lowLoanListener
                public void actionPerformed(ActionEvent event){
                    txtInterestRate.setText("5.35");                                        // Set txtInterestRate to 5.35%
                    txtLoanLength.setText("7");                                             // Set txtLoanLength to 7
                    txtInterestRate.setEnabled(false);                                      // Disable txtInterestRate
                    txtLoanLength.setEnabled(false);                                        // Disable txtLoanLength
                } // end actionperformed
            } // end class lowLoanListener
            ActionListener LowLoanListener=new lowLoanListener();                           // Set the listener to lowLoanListener
            lowLoan.addActionListener(LowLoanListener);
    
            JRadioButtonMenuItem midLoan = new JRadioButtonMenuItem("5.50% @ 15 years");    // Create a radio button for 5.5% @ 15 years
            midLoan.setMnemonic(KeyEvent.VK_5);                                             // Allow the item to be called by pressing the "5" key
            group.add(midLoan);                                                             // Add the radiobox to the button group
            menu.add(midLoan);                                                              // Add the radiobox to the menu
            class midLoanListener implements ActionListener{
                public void actionPerformed(ActionEvent event){                             // Setup an action for the mid loan
                    txtInterestRate.setText("5.5");                                         // Set the interest rate to 5.5
                    txtLoanLength.setText("15");                                            // Set the loan length to 15
                    txtInterestRate.setEnabled(false);                                      // Disable txtInterestRate
                    txtLoanLength.setEnabled(false);                                        // Disable txtLoanLength
                } // end actionperformed
            } // end class midLoanListener
            ActionListener MidLoanListener=new midLoanListener();                           // Set the listener for the midLoanListener
            midLoan.addActionListener(MidLoanListener);
    
            JRadioButtonMenuItem highLoan = new JRadioButtonMenuItem("5.75% @ 30 years");   // Create a radio button for 5.75% @ 30 years
            highLoan.setMnemonic(KeyEvent.VK_3);                                            // Allow the item to be called by pressing the "3" key
            group.add(highLoan);                                                            // Add the radiobox to the button group
            menu.add(highLoan);                                                             // Add the radiobox to the menu
            class highLoanListener implements ActionListener{
                public void actionPerformed(ActionEvent event){                             // Setup the listener for the high button
                    txtInterestRate.setText("5.75");                                        // Set the interest rate to 5.75
                    txtLoanLength.setText("30");                                            // Set the loan length to 30
                    txtInterestRate.setEnabled(false);                                      // Disbable txtInterestRate
                    txtLoanLength.setEnabled(false);                                        // Disable txtLoanLength
                } // end actionperformed
            } // end class lowLoanListener
            ActionListener HighLoanListener=new highLoanListener();                         // Set the listener for the highLoanListener
            highLoan.addActionListener(HighLoanListener);
    
            menu.addSeparator();                                                            // Add a seperator between pre-defined and custom loans
            JRadioButtonMenuItem customLoan = new JRadioButtonMenuItem("Custom Loan...");   // Set the name of the item to "Custom Loans..."
            customLoan.setSelected(true);                                                   // Set this loan to be selected by default
            customLoan.setMnemonic(KeyEvent.VK_C);                                          // Allow it to be called by pressing the "C" key
            group.add(customLoan);                                                          // Add the item to the button group
            menu.add(customLoan);                                                           // Add the item to the menu
            class customLoanListener implements ActionListener{
                public void actionPerformed(ActionEvent event){                             // Set the listener for the customListener
                    txtInterestRate.setText("5.35");                                        // Reset the interest rate to 5.35
                    txtLoanLength.setText("7");                                             // Reset the loan length to 7
                    txtInterestRate.setEnabled(true);                                       // Enable the interest rate field
                    txtLoanLength.setEnabled(true);                                         // Enable the loan length field
                } // end actionperformed
            } // end class lowLoanListener
            ActionListener CustomLoanListener=new customLoanListener();                     // Set the listener for the customLoanListener
            customLoan.addActionListener(CustomLoanListener);
    
            myFrame.setJMenuBar(menuBar);                                                   // Set the menu for the frame
    
            JButton calcLoan=new JButton("Calculate");                                      // Create a calulate button for the loan
            class calcLoanListener implements ActionListener{                               // implement an action listener for the button
                public void actionPerformed(ActionEvent event){
                    int mortgageAmount=Integer.parseInt(txtMortgageAmount.getText());       // get the text from txtMortgageAmount
                    double interestRate=Double.parseDouble(txtInterestRate.getText());      // get the text from txtInterestRate
                    int loanLength=Integer.parseInt(txtLoanLength.getText());               // get the text from txtLoanLength
                    guiLoans.showOutput(mortgageAmount, interestRate, loanLength);          // calculate the information and throw it in a jtable
                } // end action performed
            } // end subclass calcLoanListener
            ActionListener myListener=new calcLoanListener();                               // Set the listener for the calculate button
            calcLoan.addActionListener(myListener);
    
            // Add a reset button that resets the forms in
            // the textboxes to default values
            JButton clearButton=new JButton("Reset");                                       // Create a clear (or reset) button
            class clearFieldsListener implements ActionListener{                            // implement an action listener for the button
                public void actionPerformed(ActionEvent event){
                    txtMortgageAmount.setText("200000");                                    // Set the text to the default of 200000
                    txtInterestRate.setText("5.75");                                        // Set the text to the default of 5.75
                    txtLoanLength.setText("30");                                            // Set the text to the default of 30
                } // end action performed
            } // end subclass clearFieldsListener
            ActionListener resetListener=new clearFieldsListener();
            clearButton.addActionListener(resetListener);
            
            customPanel.add(calcLoan);                                                      // Add the calculate button to the panel
            customPanel.add(clearButton);                                                   // Add the clear button to the panel
            myFrame.add(customPanel);                                                       // Add the panel to the frame
            myFrame.repaint();                                                              // Re-draw the frame
        } // end main
    }  // end Main
    
    class guiLoans{
        public static void showOutput(int mortgageAmount,double interestRate,int yearsOfLoan){
    
            DecimalFormat money=new DecimalFormat("$0,000.00");
    
            // Create and set some variables for the program
            int totalMortgage=mortgageAmount;                     // Set the amount of the total mortgage
            int loanLengthInYears=yearsOfLoan;                    // Set the loan length the variable of the constructor
            int loanLengthInMonths=loanLengthInYears*12;          // Calculate the total number of monthly payments
            double annualInterestRate=interestRate/100;           // Set the annual interest rate 5.75% / 100 = .0575
            double monthlyInterestRate=annualInterestRate/12;     // Set the monthly interest rate (AnnualInterestRate / 12 months per year)
            double monthlyPayment=0.00;                           // Declare a variable for the monthly payment
    
            // Create the formula to get the monthly mortgage payment
            // with interest
    		monthlyPayment=totalMortgage*(monthlyInterestRate/(1-Math.pow((1+monthlyInterestRate), -1*(loanLengthInMonths))));
    
            // Create some JOptionPanes and display loan summaries before the amortization is displayed
            JOptionPane loanSummary=new JOptionPane();
            loanSummary.showMessageDialog(loanSummary, "For a "+money.format(mortgageAmount) +" with a " +interestRate +"% interest rate\nfor "+loanLengthInYears +" years, it would cost "+money.format(monthlyPayment)+" per month.");
    
    		JFrame testFrame=new JFrame();                                          	// Create a frame for the output
    		testFrame.setSize(600,600);													// Set the size of the frame to 600 x 600 px
    		JScrollPane testScrollPane=new JScrollPane();								// Create a scrollPane for the frame
    
    		DefaultTableModel model=new DefaultTableModel();							// Create a DefaultTableModel to bring the necessary data in
            DecimalFormat paymentMoney=new DecimalFormat("$0.00");                  // Set the format for money
            int numPayments=yearsOfLoan*12;                                         // get the numPayments by multiplying the loanLengthInYears by 12 months
            double loanBalance=totalMortgage;                                       // Copy the loan balance that is brought in
            double interestPaid=loanBalance*(interestRate/100)/12;                  // Set the interest paid
            double principalPaid=monthlyPayment-interestPaid;                       // Set the principal paid
            JTable testTable=new JTable(model);										// Create the JTable
            testScrollPane.setViewportView(testTable);								// Set the Viewport to the table
    		testFrame.add(testScrollPane);											// Add the scrollpane to the frame
    		testFrame.setVisible(true);												// Make the frame visible
    
            testFrame.setTitle(money.format(totalMortgage)+" @ "+interestRate +"% for "+loanLengthInYears +" years = " +money.format(monthlyPayment) +"/mo");	// set the title of each frame to something useful
    
    		// Add some columns to the tables
    		model.addColumn("Payment #");
            model.addColumn("Balance");
            model.addColumn("Principal");
            model.addColumn("Interest Paid");
    
            for (int num=1;num<=numPayments;num++){                                 // for loop to loop through all payments to find the balance, principal paid and interest paid
    
                // Print out the payment number, balance, principal paid and interest paid.
                model.insertRow(num-1, new Object[]{num,paymentMoney.format(loanBalance),paymentMoney.format(principalPaid),paymentMoney.format(interestPaid)});
    
                // Decrement the values for the next payment
                loanBalance=loanBalance-principalPaid;
                interestPaid=loanBalance*(interestRate/100)/12;
                principalPaid=monthlyPayment-interestPaid;
    
            } // end for loop
        } // end showOutput
    } // end class guiLoans

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    you should move the line "myFrame.setVisible(true);" to the end of the main method (one line after myFrame.add(customPanel); )
    myFrame.repaint(); is not needed

  3. #3
    mbutler755 is offline Member
    Join Date
    Jul 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    you should move the line "myFrame.setVisible(true);" to the end of the main method (one line after myFrame.add(customPanel); )
    myFrame.repaint(); is not needed
    Sheer Genius!!!!!

    It works like a champ. Thanks!

Similar Threads

  1. Replies: 22
    Last Post: 05-19-2010, 02:00 PM
  2. How to show new window after click on button
    By javahoush in forum SWT / JFace
    Replies: 4
    Last Post: 12-29-2009, 09:25 AM
  3. How do i show all the values in one window(JOptionPane)??
    By Antonioj1015 in forum Advanced Java
    Replies: 1
    Last Post: 11-25-2009, 04:17 PM
  4. Read/Write of components of frame window
    By Harish kumara M in forum AWT / Swing
    Replies: 0
    Last Post: 09-17-2008, 08:14 AM
  5. Replies: 1
    Last Post: 06-06-2008, 07:55 AM

Posting Permissions

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