JButton to open another *.java file

Java to open another Java via a JButton

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 11152 Views - Last Post: 06 July 2008 - 06:54 AM Rate Topic: -----

#1 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

JButton to open another *.java file

Post icon  Posted 01 July 2008 - 05:45 PM

How would a person use a JButton to open another Java File.
I have the other java file ( window)
But how can a JButton open the new Java code / file
Is This A Good Question/Topic? 0
  • +

Replies To: JButton to open another *.java file

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JButton to open another *.java file

Posted 01 July 2008 - 06:07 PM

View Postmysong, on 1 Jul, 2008 - 05:45 PM, said:

How would a person use a JButton to open another Java File.
I have the other java file ( window)
But how can a JButton open the new Java code / file

The ActionListener() of the JButton() can always create a new JFrame() but this is not a good design
Better to create the new JFrame() in the constructor of the Frame of the JButton() and make it visible or not

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 01 July 2008 - 10:25 PM

View Postpbl, on 1 Jul, 2008 - 06:07 PM, said:

View Postmysong, on 1 Jul, 2008 - 05:45 PM, said:

How would a person use a JButton to open another Java File.
I have the other java file ( window)
But how can a JButton open the new Java code / file

The ActionListener() of the JButton() can always create a new JFrame() but this is not a good design
Better to create the new JFrame() in the constructor of the Frame of the JButton() and make it visible or not

[rules][/rules]


my code as is now is as follows...the next post is what I want to add to a clickable button, instead of the save or print button
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.awt.print.*;
import java.awt.Color.*;  //during research I found that this allows for colors to be used as in my color change of the save button
import javax.swing.border.*; //I was attempting to add color borders around the buttons, not added in version 12

public class Mortgage extends JApplet {
	private String[] terms={"7","15","30"};																   //3 predetermined loans for a 7, 15 and 30 year loan
	private double[] rates={0.0535,0.055,0.0575};													// the interest rate for the 7, 15, and 30 year loan.
	private JLabel amountLabel=new JLabel("Input The Amount of Loan, no commas");		  //the text showing the user what area to input the amount of loan
	private JTextField amount=new JTextField();													  //this is the area to actually input the loan amount
	private JLabel termLabel=new JLabel("Enter Years of Loan to Pay On, or use the dropdown: ");	  //this is the Text shouwing user to input years of loan one wished to obtain
				  private JTextField term=new JTextField();														   //this is where one would input the amount of years
	private JLabel rateLabel=new JLabel("Select / Input Your Interest rate, eg .055 for 5.5%: ");	   //this is the text showing the user to input or select interest rate
	private JTextField rate=new JTextField();															//this is the actual area to input the interest rate one wishes to have
	private JComboBox termList = new JComboBox(terms);								  //combo box of 3 each pre=determined terms of loans, 7, 15, and 30 year
	private JLabel payLabel=new JLabel("Your Monthly Payment Will Be: ");		//this is the text showing a user that this area will be for the monthly payment.
	private JLabel payment=new JLabel();																//this is the area that will show the user what the payment will be.
	private JButton calculate=new JButton("Calculate");										  //this button will start the calculations of the items that have been inputted
	private JButton clear=new JButton("Clear");													   //this button will clear the areas for one to start a new calculation
	private JButton exit=new JButton("Exit");															//this button will exit the item
	private JButton save=new JButton("Save");													   // added save button 23 June 08, inop at this time
	private JButton print=new JButton("Print");														 //added print button 23 June 08, inop at this time
	private JMenu loan=new JMenu("Loans");
	private JTextArea paymentSchedule=new JTextArea();								   //this is the frame to show the payment schedule information to the user
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);	 //payment schedule frame, the area to show the payment information
	private Container cp = getContentPane();
	// the menu bar
	private MenuBar mb = new MenuBar();
	// the loans menu will show the preset loans to pick from
	private Menu mLoans = new Menu("Loans");
	private MenuItem mLoan1 = new MenuItem("7 year");
	private MenuItem mLoan2 = new MenuItem("15 year");
	private MenuItem mLoan3 = new MenuItem("30 year");

	setMenuBar(mb);

// add the menus
mb.add(mLoans);
mLoans.add(mLoan1);
//mLoans.addActionListener(this);
mLoans.add(mLoan2);
//mLoans.addActionListener(this);
mLoans.add(mLoan3);
//mLoans.addActionListener(this);

//mb.add(mExit);
//mExit.add(mExit);
//mExit.addActionListener(this);

		public void init() {
																													 // Term list
		termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
																						  // combo box, source drop down box for pre determined 7, 15, and 30 year loan
				String termYear = (String)cb.getSelectedItem();
				term.setText(termYear);
				int index=0;
				switch (Integer.parseInt(termYear)) {
					case 7: index=0; break;
					case 15: index=1; break;
					case 30: index=2; break;
				}
				rate.setText(rates[index]+"");
			}
		});

																																											// The Buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
																																	// calculate the payment to be paid monthly
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double n=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+r,n)*r/(Math.pow(1+r,n)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");				//my beloved decimal format that gave me issues 
					payment.setText(df.format(monthlyPayment));
																																	// calculate the details of the loan, principal, interest, balance, etc.
					double principal=p;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<n; i++) {
						month=i+1;
						double interest=principal*r;
						double balance=principal+interest-monthlyPayment;
						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t"); 
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");
						principal=balance;
					}
					paymentSchedule.setText(buffer.toString());
				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});
//my print function	print.addActionListener(new ActionListener() {
//is not working		public void actionPerformed(ActionEvent e) {
//commented out			printPrint();
//			}
//		});

		JPanel upScreen=new JPanel();											//setting up the frame, or pane to display to the user
		upScreen.setLayout(new GridLayout(5,2));
		upScreen.add(amountLabel); upScreen.add(amount);
		upScreen.add(termLabel); upScreen.add(term);
		upScreen.add(new Label()); upScreen.add(termList);
		upScreen.add(rateLabel); upScreen.add(rate);									  
		upScreen.add(payLabel); upScreen.add(payment);
		JPanel buttons=new JPanel();															   //button for a new panel
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));	//laying out the box areas
		buttons.add(calculate); buttons.add(clear); buttons.add(print); buttons.add(save); buttons.add(exit); // the placement of the buttons and the order of appearance, including the new buttons save and print that are inop as of 24 June 08
		save.setBackground (new Color (255, 255, 255));					   //changed the save button background color to white-  24 June 08
		save.setForeground (new Color (255, 0, 0));								//changed the save buuton text to red-24 June 08
//some examples of colors I found in research
//white is 255, 255, 255
//black is 0, 0, 0
//grey is 128, 128, 128
//red is 255, 0, 0		
		JPanel up=new JPanel();
		up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));		   //laying out the box areas
		up.add(upScreen); up.add(buttons);
		cp.add(BorderLayout.NORTH, up);										   //informing where to place the borders shown inside the frame
		cp.add(BorderLayout.CENTER, schedulePane);					//showing another border, in the pane or frame of where the payment schedule will be shown.
	}
	public static void main(String[] args) {
		JApplet applet = new Mortgage();
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);													   //if I set this to false the user cannot resize the window (or popup) to fit their needs
		frame.getContentPane().add(applet);
		frame.setSize(700,500);														 //the full total size of a preset frame  or popup window that the user will be using if the user does not resize it.
		applet.init();
		applet.start();
		frame.setVisible(true);
	}
}



View Postmysong, on 1 Jul, 2008 - 10:23 PM, said:

View Postpbl, on 1 Jul, 2008 - 06:07 PM, said:

View Postmysong, on 1 Jul, 2008 - 05:45 PM, said:

How would a person use a JButton to open another Java File.
I have the other java file ( window)
But how can a JButton open the new Java code / file

The ActionListener() of the JButton() can always create a new JFrame() but this is not a good design
Better to create the new JFrame() in the constructor of the Frame of the JButton() and make it visible or not

[rules][/rules]


my code as is now is as follows...the next post is what I want to add to a clickable button, instead of the save or print button
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.awt.print.*;
import java.awt.Color.*;  //during research I found that this allows for colors to be used as in my color change of the save button
import javax.swing.border.*; //I was attempting to add color borders around the buttons, not added in version 12

public class Mortgage extends JApplet {
	private String[] terms={"7","15","30"};																   //3 predetermined loans for a 7, 15 and 30 year loan
	private double[] rates={0.0535,0.055,0.0575};													// the interest rate for the 7, 15, and 30 year loan.
	private JLabel amountLabel=new JLabel("Input The Amount of Loan, no commas");		  //the text showing the user what area to input the amount of loan
	private JTextField amount=new JTextField();													  //this is the area to actually input the loan amount
	private JLabel termLabel=new JLabel("Enter Years of Loan to Pay On, or use the dropdown: ");	  //this is the Text shouwing user to input years of loan one wished to obtain
				  private JTextField term=new JTextField();														   //this is where one would input the amount of years
	private JLabel rateLabel=new JLabel("Select / Input Your Interest rate, eg .055 for 5.5%: ");	   //this is the text showing the user to input or select interest rate
	private JTextField rate=new JTextField();															//this is the actual area to input the interest rate one wishes to have
	private JComboBox termList = new JComboBox(terms);								  //combo box of 3 each pre=determined terms of loans, 7, 15, and 30 year
	private JLabel payLabel=new JLabel("Your Monthly Payment Will Be: ");		//this is the text showing a user that this area will be for the monthly payment.
	private JLabel payment=new JLabel();																//this is the area that will show the user what the payment will be.
	private JButton calculate=new JButton("Calculate");										  //this button will start the calculations of the items that have been inputted
	private JButton clear=new JButton("Clear");													   //this button will clear the areas for one to start a new calculation
	private JButton exit=new JButton("Exit");															//this button will exit the item
	private JButton save=new JButton("Save");													   // added save button 23 June 08, inop at this time
	private JButton print=new JButton("Print");														 //added print button 23 June 08, inop at this time
	private JMenu loan=new JMenu("Loans");
	private JTextArea paymentSchedule=new JTextArea();								   //this is the frame to show the payment schedule information to the user
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);	 //payment schedule frame, the area to show the payment information
	private Container cp = getContentPane();
	// the menu bar
	private MenuBar mb = new MenuBar();
	// the loans menu will show the preset loans to pick from
	private Menu mLoans = new Menu("Loans");
	private MenuItem mLoan1 = new MenuItem("7 year");
	private MenuItem mLoan2 = new MenuItem("15 year");
	private MenuItem mLoan3 = new MenuItem("30 year");

	setMenuBar(mb);

// add the menus
mb.add(mLoans);
mLoans.add(mLoan1);
//mLoans.addActionListener(this);
mLoans.add(mLoan2);
//mLoans.addActionListener(this);
mLoans.add(mLoan3);
//mLoans.addActionListener(this);

//mb.add(mExit);
//mExit.add(mExit);
//mExit.addActionListener(this);

		public void init() {
																													 // Term list
		termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
																						  // combo box, source drop down box for pre determined 7, 15, and 30 year loan
				String termYear = (String)cb.getSelectedItem();
				term.setText(termYear);
				int index=0;
				switch (Integer.parseInt(termYear)) {
					case 7: index=0; break;
					case 15: index=1; break;
					case 30: index=2; break;
				}
				rate.setText(rates[index]+"");
			}
		});

																																											// The Buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
																																	// calculate the payment to be paid monthly
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double n=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+r,n)*r/(Math.pow(1+r,n)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");				//my beloved decimal format that gave me issues 
					payment.setText(df.format(monthlyPayment));
																																	// calculate the details of the loan, principal, interest, balance, etc.
					double principal=p;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<n; i++) {
						month=i+1;
						double interest=principal*r;
						double balance=principal+interest-monthlyPayment;
						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t"); 
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");
						principal=balance;
					}
					paymentSchedule.setText(buffer.toString());
				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});
//my print function	print.addActionListener(new ActionListener() {
//is not working		public void actionPerformed(ActionEvent e) {
//commented out			printPrint();
//			}
//		});

		JPanel upScreen=new JPanel();											//setting up the frame, or pane to display to the user
		upScreen.setLayout(new GridLayout(5,2));
		upScreen.add(amountLabel); upScreen.add(amount);
		upScreen.add(termLabel); upScreen.add(term);
		upScreen.add(new Label()); upScreen.add(termList);
		upScreen.add(rateLabel); upScreen.add(rate);									  
		upScreen.add(payLabel); upScreen.add(payment);
		JPanel buttons=new JPanel();															   //button for a new panel
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));	//laying out the box areas
		buttons.add(calculate); buttons.add(clear); buttons.add(print); buttons.add(save); buttons.add(exit); // the placement of the buttons and the order of appearance, including the new buttons save and print that are inop as of 24 June 08
		save.setBackground (new Color (255, 255, 255));					   //changed the save button background color to white-  24 June 08
		save.setForeground (new Color (255, 0, 0));								//changed the save buuton text to red-24 June 08
//some examples of colors I found in research
//white is 255, 255, 255
//black is 0, 0, 0
//grey is 128, 128, 128
//red is 255, 0, 0		
		JPanel up=new JPanel();
		up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));		   //laying out the box areas
		up.add(upScreen); up.add(buttons);
		cp.add(BorderLayout.NORTH, up);										   //informing where to place the borders shown inside the frame
		cp.add(BorderLayout.CENTER, schedulePane);					//showing another border, in the pane or frame of where the payment schedule will be shown.
	}
	public static void main(String[] args) {
		JApplet applet = new Mortgage();
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);													   //if I set this to false the user cannot resize the window (or popup) to fit their needs
		frame.getContentPane().add(applet);
		frame.setSize(700,500);														 //the full total size of a preset frame  or popup window that the user will be using if the user does not resize it.
		applet.init();
		applet.start();
		frame.setVisible(true);
	}
}



this is what I want to add to a clickable button to open in a new window
[code]


import java.util.Set;
import java.util.HashSet;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import java.text.*;

/**
* InputVerificationDemo.java is a 1.4 example that
* requires no other files.
*
* Yet another mortgage calculator.
* However, instead of using a formatted text field,
* as shown in FormattedTextFieldDemo, this example
* uses input verification to validate user input.
*/
public class InputVerificationDemo extends JPanel {
//Default values
private static double DEFAULT_AMOUNT = 100000;
private static double DEFAULT_RATE = 7.5; //7.5%
private static int DEFAULT_PERIOD = 30;

//Labels to identify the text fields
private JLabel amountLabel;
private JLabel rateLabel;
private JLabel numPeriodsLabel;
private JLabel paymentLabel;

//Strings for the labels
private static String amountString = "Loan Amount (10,000-10,000,000): ";
private static String rateString = "APR (>= 0%): ";
private static String numPeriodsString = "Years (1-40): ";
private static String paymentString = "Monthly Payment: ";

//Text fields for data entry
private JTextField amountField;
private JTextField rateField;
private JTextField numPeriodsField;
private JTextField paymentField;

//Formats to format and parse numbers
private NumberFormat moneyFormat;
private NumberFormat percentFormat;
private DecimalFormat decimalFormat;
private DecimalFormat paymentFormat;
private MyVerifier verifier = new MyVerifier();

public InputVerificationDemo() {
super(new BorderLayout());
setUpFormats();
double payment = computePayment(DEFAULT_AMOUNT,
DEFAULT_RATE,
DEFAULT_PERIOD);

//Create the labels.
amountLabel = new JLabel(amountString);
rateLabel = new JLabel(rateString);
numPeriodsLabel = new JLabel(numPeriodsString);
paymentLabel = new JLabel(paymentString);

//Create the text fields and set them up.
amountField = new JTextField(moneyFormat.format(DEFAULT_AMOUNT), 10);
amountField.setInputVerifier(verifier);

rateField = new JTextField(percentFormat.format(DEFAULT_RATE), 10);
rateField.setInputVerifier(verifier);

numPeriodsField = new JTextField(decimalFormat.format(DEFAULT_PERIOD), 10);
numPeriodsField.setInputVerifier(verifier);

paymentField = new JTextField(paymentFormat.format(payment), 10);
paymentField.setInputVerifier(verifier);
paymentField.setEditable(false);
//Remove this component from the focus cycle.
paymentField.setFocusable(false);
paymentField.setForeground(Color.red);

//Register an action listener to handle Return.
amountField.addActionListener(verifier);
rateField.addActionListener(verifier);
numPeriodsField.addActionListener(verifier);

//Tell accessibility tools about label/textfield pairs.
amountLabel.setLabelFor(amountField);
rateLabel.setLabelFor(rateField);
numPeriodsLabel.setLabelFor(numPeriodsField);
paymentLabel.setLabelFor(paymentField);

//Lay out the labels in a panel.
 &nbs
Was This Post Helpful? 0
  • +
  • -

#4 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 02 July 2008 - 05:28 PM

All I really want to do is to add to my code that works... at the top left, a drop menu that will have
7
15
30
year loans that are preset from my calculations.
And when the user clicks the one they want, it will ask them to input an amount, then auto calculate in loan schedule area.

And the menu is beating me up.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JButton to open another *.java file

Posted 02 July 2008 - 05:54 PM

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class Applet extends JApplet implements ActionListener {
	
	public void init() {
		JButton b = new JButton("Go");
		b.addActionListener(this);
		getContentPane().add(b);
	}
	
	public void actionPerformed(ActionEvent e) {
		JFrame f = new JFrame("Other Windows");
		f.setBounds(60, 40, 200, 300);
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		f.setVisible(true);
	}

}



You just have to add an instance of your InputVerificationDemo into the JFrame f

This post has been edited by pbl: 02 July 2008 - 05:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 02 July 2008 - 10:15 PM

Ok that got me lost

I am wanting to use my existing code and formulas.

I have aded 3 buttons for each preset loan,....but how can I have each 7, 15, and 30 year button to auto calculate the loan and schedule into my schedule window inside a table.

I understand all of you are profesionals at this, and I am am begging for help.
Please understand I am so very new at this trying to pass a course that the instructor has changed the syllabus midway into the course.

import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.awt.print.*;	//attempting to ad a print function, inop as of 24 June 2008 Mortgage16test
import java.awt.Color.*;  //during research I found that this allows for colors to be used as in my color change of a button
// found another way for colors so the next line is commented out
//import javax.swing.border.*; //I was attempting to add color borders around the buttons, not added in version 12

public class Mortgage20test extends JApplet {
	private String[] terms={"7","15","30"};																   //3 predetermined loans for a 7, 15 and 30 year loan
	private double[] rates={0.0535,0.055,0.0575};													// the interest rate for the 7, 15, and 30 year loan.
	private JLabel amountLabel=new JLabel("Input The Amount of Loan, no commas");		  //the text showing the user what area to input the amount of loan
	private JTextField amount=new JTextField();													  //this is the area to actually input the loan amount
	private JLabel termLabel=new JLabel("Enter Years of Loan to Pay On, or use the dropdown: ");	  //this is the Text shouwing user to input years of loan one wished to obtain
				private JTextField term=new JTextField();														   //this is where one would input the amount of years
	private JLabel rateLabel=new JLabel("Select / Input Your Interest rate, eg .055 for 5.5%: ");	   //this is the text showing the user to input or select interest rate
	private JTextField rate=new JTextField();															//this is the actual area to input the interest rate one wishes to have
	private JComboBox termList = new JComboBox(terms);								  //combo box of 3 each pre=determined terms of loans, 7, 15, and 30 year
	private JLabel payLabel=new JLabel("Your Monthly Payment Will Be: ");		//this is the text showing a user that this area will be for the monthly payment.
	private JLabel payment=new JLabel();																//this is the area that will show the user what the payment will be.
	private JButton seven=new JButton("7 years @ 5.35%");
	private JButton fifteen=new JButton("15 years @ 5.55%");
	private JButton thirty=new JButton("30 years @ 5.75%");	
	private JButton calculate=new JButton("Calculate");										  //this button will start the calculations of the items that have been inputted
	private JButton clear=new JButton("Clear");													   //this button will clear the areas for one to start a new calculation
	private JButton exit=new JButton("Exit");															//this button will exit the item
	private JButton print=new JButton("Print");														 //added print button 23 June 08, inop at this time
	private JTextArea paymentSchedule=new JTextArea();								   //this is the frame to show the payment schedule information to the user
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);	 //payment schedule frame, the area to show the payment information
	private Container cp = getContentPane();


	public void init() {
																													 // Term list
		termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
																						  // combo box, source drop down box for pre determined 7, 15, and 30 year loan
				String termYear = (String)cb.getSelectedItem();
				term.setText(termYear);
				int index=0;
				switch (Integer.parseInt(termYear)) {
					case 7: index=0; break;
					case 15: index=1; break;
					case 30: index=2; break;
				}
				rate.setText(rates[index]+"");
			}
		});

																																											// The Buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
																																	// calculate the payment to be paid monthly
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double n=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+r,n)*r/(Math.pow(1+r,n)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");				//my beloved decimal format that gave me issues
					payment.setText(df.format(monthlyPayment));
																																	// calculate the details of the loan, principal, interest, balance, etc.
					double principal=p;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<n; i++) {
						month=i+1;
						double interest=principal*r;
						double balance=principal+interest-monthlyPayment;
						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t");
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");
						principal=balance;
					}
					paymentSchedule.setText(buffer.toString());
				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});
//cannot make it work		print.addActionListener(new ActionListener() {
//cannot make it work			public void actionPerformed(ActionEvent e) {
//cannot make it work				printPrint();			}
//cannot make it work		});

		JPanel upScreen=new JPanel();											//setting up the frame, or pane to display to the user
		upScreen.setLayout(new GridLayout(5,2));
		upScreen.add(amountLabel); upScreen.add(amount);
		upScreen.add(termLabel); upScreen.add(term);
		upScreen.add(new Label()); upScreen.add(termList);
		upScreen.add(rateLabel); upScreen.add(rate);
		upScreen.add(payLabel); upScreen.add(payment);
		JPanel buttons=new JPanel();															   //button for a new panel
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));	//laying out the box areas
		buttons.add(seven); buttons.add(fifteen); buttons.add(thirty); buttons.add(calculate); buttons.add(clear); buttons.add(exit); 
		seven.setBackground (new Color (255, 255, 255));					   
		seven.setForeground (new Color (255, 0, 0));  
		fifteen.setBackground (new Color (255, 0, 0));					   
		fifteen.setForeground (new Color (255, 255, 255));	
		thirty.setBackground (new Color (0, 0, 0));					   
		thirty.setForeground (new Color (255, 0, 0));							  
//some examples of colors I found in research
//white is 255, 255, 255
//black is 0, 0, 0
//grey is 128, 128, 128
//red is 255, 0, 0
		JPanel up=new JPanel();
		up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));		   //laying out the box areas
		up.add(upScreen); up.add(buttons);
		cp.add(BorderLayout.NORTH, up);										   //informing where to place the borders shown inside the frame
		cp.add(BorderLayout.CENTER, schedulePane);					//showing another border, in the pane or frame of where the payment schedule will be shown.
	}
// PrintJob and print implementation
//cannot make it work   		static void printPrint()  {

//cannot make it work			  Printprint = window.getToolkit().getPrint(window, "JTextArea", null);

//cannot make it work			  if (print != null) {

//cannot make it work				 Graphics printgraphics = Print.getGraphics();   //get a graphics object that will draw to the next page.
//cannot make it work		 		if (printgraphics != null) {							   //if user selects cancel on the print dialog, printgraphics will be null
//cannot make it work					printgraphics.translate(100,100);					  //move the origin of the drawing area so there is a margin
//cannot make it work					JTextArea.Print(printgraphics);					   //output the text
//cannot make it work					printgraphics.dispose();								//the page is sent to the printer when dispose is called
//cannot make it work		 }
//cannot make it work		 		print.end();													  //ends the print and does any necessary cleanup
//cannot make it work	  }
//cannot make it work   }
	public static void main(String[] args) {
		JApplet applet = new Mortgage20test();
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);													   //if I set this to false the user cannot resize the window (or popup) to fit their needs
		frame.getContentPane().add(applet);
		frame.setSize(700,500);														 //the full total size of a preset frame  or popup window that the user will be using if the user does not resize it.
		applet.init();
		applet.start();
		frame.setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: JButton to open another *.java file

Posted 03 July 2008 - 04:21 AM

Quote

cp.add(BorderLayout.NORTH, up);
cp.add(BorderLayout.CENTER, schedulePane);



Shouldn't those parameters be the other way around? As in: up (a Component) as first parameter and BorderLayout.NORTH (a String) as the second?

http://java.sun.com/...va.lang.Object)

Now, I'm not sure if I understood your question correctly, but I thought the problem was that your JPanel wasn't showing in your applet? If you're adding them wrong then this should solve it. If it was something else, sorry for bothering you.

This post has been edited by JeroenFM: 03 July 2008 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

#8 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 03 July 2008 - 03:34 PM

my main code works fine.
I just want a JButton to open and run another *.java file
how can I have a JButton run another Java file when I have to start the Java file from a command prompt.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JButton to open another *.java file

Posted 03 July 2008 - 04:11 PM

View Postmysong, on 3 Jul, 2008 - 03:34 PM, said:

my main code works fine.
I just want a JButton to open and run another *.java file
how can I have a JButton run another Java file when I have to start the Java file from a command prompt.

Keep it simple
Cut & paste my example and try it
Then you'll see how you can customize it for your need
Your ActionListener has:
- to create a new JFrame
- put you JPanel into it
- set a size to the JFrame
- set its CloseOperation
- make it visible
Was This Post Helpful? 0
  • +
  • -

#10 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 04 July 2008 - 10:41 AM

The professor now tells me to stay away from JApplet.
he told me to change 3 things in my original code.

1. Change it to use JFrame
2. Add JTab, so that the results calculated look like an excel spreadsheet.
3. Add a working menu bar for each 7, 15, and 30 year calculations.

Talk about being lost now.
I am pulling out my hair.
and only one more week to fix all this.
Was This Post Helpful? 0
  • +
  • -

#11 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: JButton to open another *.java file

Posted 04 July 2008 - 05:47 PM

View Postmysong, on 4 Jul, 2008 - 10:41 AM, said:

The professor now tells me to stay away from JApplet.
he told me to change 3 things in my original code.

1. Change it to use JFrame
2. Add JTab, so that the results calculated look like an excel spreadsheet.
3. Add a working menu bar for each 7, 15, and 30 year calculations.

Talk about being lost now.
I am pulling out my hair.
and only one more week to fix all this.


You have a whole week for that? Nothing to worry then. Some people can build entire planets in such an amount of time ;-)

First of all, I think you mean JTable, which does the spreadsheet-like appearance.

Second, you probably need a working JMenuItem for each calculation, not a MenuBar.

Now, when using Swing, the API and Java tutorials are your friends:

http://java.sun.com/...1.5.0/docs/api/
http://java.sun.com/...ents/frame.html
http://java.sun.com/...ents/table.html
http://java.sun.com/...nents/menu.html
http://java.sun.com/...g/JMenuBar.html
http://java.sun.com/...wing/JMenu.html
http://java.sun.com/.../JMenuItem.html
http://java.sun.com/...ing/JFrame.html
http://java.sun.com/...ing/JTable.html
Was This Post Helpful? 0
  • +
  • -

#12 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 04 July 2008 - 11:45 PM

View PostJeroenFM, on 4 Jul, 2008 - 05:47 PM, said:

View Postmysong, on 4 Jul, 2008 - 10:41 AM, said:

The professor now tells me to stay away from JApplet.
he told me to change 3 things in my original code.

1. Change it to use JFrame
2. Add JTab, so that the results calculated look like an excel spreadsheet.
3. Add a working menu bar for each 7, 15, and 30 year calculations.

Talk about being lost now.
I am pulling out my hair.
and only one more week to fix all this.


You have a whole week for that? Nothing to worry then. Some people can build entire planets in such an amount of time ;-)

First of all, I think you mean JTable, which does the spreadsheet-like appearance.

Second, you probably need a working JMenuItem for each calculation, not a MenuBar.

Now, when using Swing, the API and Java tutorials are your friends:

http://java.sun.com/...1.5.0/docs/api/
http://java.sun.com/...ents/frame.html
http://java.sun.com/...ents/table.html
http://java.sun.com/...nents/menu.html
http://java.sun.com/...g/JMenuBar.html
http://java.sun.com/...wing/JMenu.html
http://java.sun.com/.../JMenuItem.html
http://java.sun.com/...ing/JFrame.html
http://java.sun.com/...ing/JTable.html



yes JTable
and ...ohhhh my god are you kiddidng ...easy to convert and re-write for a novice in a week.
I am begging for help ...it took me 4 weeks to make my code actually work.....now he wants JFrame instead of JApplet.
and the Jtable,...and he said if I get that working to add a working menu bar to my mortgage.
Honestly,....I feel I made the program work, but now I am expected more in one week.
I am begging for help, and I do not have much time being a working adult in college.
I thought this degree would be setting up networks and such as the college informed me,....not writting JAVA.
please help me.
That is alot of reading and re-writting my code ...thta is working ( FOR A NOVICE ) to fully re-write in less than one week.

I am begging for help.
Was This Post Helpful? 0
  • +
  • -

#13 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 05 July 2008 - 12:00 AM

here is my code that is JApplet
but he wants it JFrame.
He also wants JTable for my results to look like Excel.

and he now want a menu bar with a working
7 year loan @5.35 Int Rate
15 year @ 5.55
30 year @ 5.75 to work and auto calculate when clicked from the menu

I have it working when user input is typed in,......and when a user selects a dropdown list box

but now he wants me to change from JApplet to JFrame,...and make the other changes in one week.

I cannot accomplish this on my own in one week.
My code works as is,......but his modification requests are what I must achieve.
my working code as of now is as follows
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.awt.print.*;	//attempting to ad a print function, inop as of 24 June 2008 Mortgage16test
import java.awt.Color.*;  //during research I found that this allows for colors to be used as in my color change of a button

public class Mortgage20test extends JApplet {
	private String[] terms={"7","15","30"};																   //3 predetermined loans for a 7, 15 and 30 year loan
	private double[] rates={0.0535,0.055,0.0575};													// the interest rate for the 7, 15, and 30 year loan.
	private JLabel amountLabel=new JLabel("Input The Amount of Loan, no commas");		  //the text showing the user what area to input the amount of loan
	private JTextField amount=new JTextField();													  //this is the area to actually input the loan amount
	private JLabel termLabel=new JLabel("Enter Years of Loan to Pay On, or use the dropdown: ");	  //this is the Text shouwing user to input years of loan one wished to obtain
				private JTextField term=new JTextField();														   //this is where one would input the amount of years
	private JLabel rateLabel=new JLabel("Select / Input Your Interest rate, eg .055 for 5.5%: ");	   //this is the text showing the user to input or select interest rate
	private JTextField rate=new JTextField();															//this is the actual area to input the interest rate one wishes to have
	private JComboBox termList = new JComboBox(terms);								  //combo box of 3 each pre=determined terms of loans, 7, 15, and 30 year
	private JLabel payLabel=new JLabel("Your Monthly Payment Will Be: ");		//this is the text showing a user that this area will be for the monthly payment.
	private JLabel payment=new JLabel();																//this is the area that will show the user what the payment will be.
	private JButton seven=new JButton("7 years @ 5.35%");
	private JButton fifteen=new JButton("15 years @ 5.55%");
	private JButton thirty=new JButton("30 years @ 5.75%");	
	private JButton calculate=new JButton("Calculate");										  //this button will start the calculations of the items that have been inputted
	private JButton clear=new JButton("Clear");													   //this button will clear the areas for one to start a new calculation
	private JButton exit=new JButton("Exit");															//this button will exit the item
	private JButton print=new JButton("Print");														 //added print button 23 June 08, inop at this time
	private JTextArea paymentSchedule=new JTextArea();								   //this is the frame to show the payment schedule information to the user
	private JScrollPane schedulePane=new JScrollPane(paymentSchedule);	 //payment schedule frame, the area to show the payment information
	private Container cp = getContentPane();


	public void init() {
																													 // Term list
		termList.setSelectedIndex(0);
		termList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
																						  // combo box, source drop down box for pre determined 7, 15, and 30 year loan
				String termYear = (String)cb.getSelectedItem();
				term.setText(termYear);
				int index=0;
				switch (Integer.parseInt(termYear)) {
					case 7: index=0; break;
					case 15: index=1; break;
					case 30: index=2; break;
				}
				rate.setText(rates[index]+"");
			}
		});

																																											// The Buttons
		calculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
																																	// calculate the payment to be paid monthly
					double p=Double.parseDouble(amount.getText());
					double r=Double.parseDouble(rate.getText())/12;
					double n=Integer.parseInt(term.getText())*12;
					double monthlyPayment=p*Math.pow(1+r,n)*r/(Math.pow(1+r,n)-1);
					DecimalFormat df = new DecimalFormat("$###,###.00");				//my beloved decimal format that gave me issues
					payment.setText(df.format(monthlyPayment));
																																	// calculate the details of the loan, principal, interest, balance, etc.
					double principal=p;
					int month;
					StringBuffer buffer=new StringBuffer();
					buffer.append("Month\tAmount\tInterest\tBalance\n");
					for (int i=0; i<n; i++) {
						month=i+1;
						double interest=principal*r;
						double balance=principal+interest-monthlyPayment;
						buffer.append(month+"\t");
						buffer.append(new String(df.format(principal))+"\t");
						buffer.append(new String(df.format(interest))+"\t");
						buffer.append(new String(df.format(balance))+"\n");
						principal=balance;
					}
					paymentSchedule.setText(buffer.toString());
				} catch(Exception ex) {
					System.out.println(ex);
				}
			}
		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				amount.setText("");
				payment.setText("");
				paymentSchedule.setText("");
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(1);
			}
		});


		JPanel upScreen=new JPanel();											//setting up the frame, or pane to display to the user
		upScreen.setLayout(new GridLayout(5,2));
		upScreen.add(amountLabel); upScreen.add(amount);
		upScreen.add(termLabel); upScreen.add(term);
		upScreen.add(new Label()); upScreen.add(termList);
		upScreen.add(rateLabel); upScreen.add(rate);
		upScreen.add(payLabel); upScreen.add(payment);
		JPanel buttons=new JPanel();															   //button for a new panel
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));	//laying out the box areas
		buttons.add(seven); buttons.add(fifteen); buttons.add(thirty); buttons.add(calculate); buttons.add(clear); buttons.add(exit); 
		seven.setBackground (new Color (255, 255, 255));					   
		seven.setForeground (new Color (255, 0, 0));  
		fifteen.setBackground (new Color (255, 0, 0));					   
		fifteen.setForeground (new Color (255, 255, 255));	
		thirty.setBackground (new Color (0, 0, 0));					   
		thirty.setForeground (new Color (255, 0, 0));							  

		JPanel up=new JPanel();
		up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));		   //laying out the box areas
		up.add(upScreen); up.add(buttons);
		cp.add(BorderLayout.NORTH, up);										   //informing where to place the borders shown inside the frame
		cp.add(BorderLayout.CENTER, schedulePane);					//showing another border, in the pane or frame of where the payment schedule will be shown.
	}

	public static void main(String[] args) {
		JApplet applet = new Mortgage20test();
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);													   //if I set this to false the user cannot resize the window (or popup) to fit their needs
		frame.getContentPane().add(applet);
		frame.setSize(700,500);														 //the full total size of a preset frame  or popup window that the user will be using if the user does not resize it.
		applet.init();
		applet.start();
		frame.setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

#14 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: JButton to open another *.java file

Posted 05 July 2008 - 03:24 AM

Seriously, I am not kidding. I had to do assignments like yours in 4 hours and I finished them in 3 - I believe it was in the 9th week of the semester.

So, to keep things simple:

Create a class that extends JFrame for every dialog you need. The following is a skeleton that should explain a few basic principles - you should add the proper package statements:

// Add package name

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class MyMortgageFrame extends JFrame implements ActionListener {
  private JFrame dialog1;
  // Depending on how many you need, add more

  private JMenuBar jmbMainMenu;
  private JMenu jmCalculations;
  private JMenuItem jmiDoCalculation1;

  private JTable jtResults;

  public MyMainWindow() {
    super("Mortgage calculator");

    initialize();
    dialog1 = new NameOfYourDialogClass(jtResults);
  }

  protected void initialize() {
    jmbMainMenu = new JMenuBar();
    jmCalculations = new JMenu("Calculations");
    jmiDoCalculation1 = new JMenuItem("Perform calculation type 1");

    jmiDoCalculation1.addActionListener(this);

    jmbMainMenu.add(jmCalculations);
    jmCalculations.add(jmiDoCalculation1);

    setJMenuBar(jmMainMenu);

    jtResults = new JTable();
    getContentPane().add(jtResults);

    setSize(800, 600);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == jmiDoCalculation1) {
      dialog1.setVisible(true);
    }
  }

  public static void main(String[] args) {
    new MyMortgageFrame();
  }
}




For each dialog:

// Add package name

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class NameOfYourDialogClass extends JFrame implements ActionListener {
  private JTable ref;
  private JButton jbCalculate;
  
  // Add textfields and other things needed for calculation

  public NameOfYourDialogClass(JTable ref) {
    super("Calculate mortgage type X");
    this.ref = ref;

    initialize();
  }

  protected void initialize() {
    JButton jbCalculate = new JButton("Calculate!");
    jbCalculate.addActionListener(this);

    // Add your other components, don't forget to add them to the contentpane
  }

  public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == jbCalculate) {
      ref.setModel(
        new DefaultTableModel(
          new Object[0][0], // REPLACE WITH VALUES FOR YOUR TABLE
          new Object[0] // REPLACE WITH COLUMN TITLES
        );
       ref.updateUI();
     }
    }
  }
}



Disclaimer: code is untested

This post has been edited by JeroenFM: 05 July 2008 - 03:25 AM

Was This Post Helpful? 0
  • +
  • -

#15 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: JButton to open another *.java file

Posted 05 July 2008 - 11:14 AM

Could't I find a way to keep my existing code that uses JApplet, and change it to use JFrame for the full code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2