Join 136,465 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,579 people online right now. Registration is fast and FREE... Join Now!
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
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
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
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Please post like this:
Thank you for helping us helping you.
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
CODE
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");
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); } }
QUOTE(mysong @ 1 Jul, 2008 - 10:23 PM)
QUOTE(pbl @ 1 Jul, 2008 - 06:07 PM)
QUOTE(mysong @ 1 Jul, 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
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
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Please post like this:
Thank you for helping us helping you.
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
CODE
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");
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
/** * 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;
//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);
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.
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.
CODE
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]+""); } });