Question: So i know how to do this using Swing but how to i convert swing code to JavaFX? Here is a sample code so far
So i know how to do this using Swing but how to i convert swing code to JavaFX? Here is a sample code so far that i must convert to JavaFx.
Executable Code
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import java.text.NumberFormat;
@SuppressWarnings("serial")
public class InterestTableGUI13 extends JPanel
{
private static final int NOYRSMIN = 1;
private static final int NOYRSMAX = 30;
private static final int NOYRSINIT = 5;
private JTextArea mydisplay;
private JScrollPane myscrollPane;
private JPanel mycentralPanel;
private JTextField principalText;
private JTextField percentageText;
private JSlider myslider;
private JPanel mybPanel;
private JButton sIButton;
private JTextField mymonthlyPay;
public InterestTableGUI13(int a, int b)
{
setPreferredSize(new Dimension(a, b));
setLayout(new BorderLayout());
mydisplay = new JTextArea(10, 10);
mydisplay.setLineWrap(true);
mydisplay.setEditable(false);
myscrollPane = new JScrollPane(mydisplay);
add(myscrollPane, BorderLayout.NORTH);
mycentralPanel = new JPanel();
mycentralPanel.add(new JLabel("Principal: "));
principalText = new JTextField(12);
mycentralPanel.add(principalText);
mycentralPanel.add(new JLabel("Monthly Payment: "));
mymonthlyPay = new JTextField(12);
mycentralPanel.add(mymonthlyPay);
mycentralPanel.add(new JLabel("Rate(Percentage): "));
percentageText = new JTextField(5);
mycentralPanel.add(percentageText);
JLabel mysliderLabel = new JLabel("Number of Years: ", JLabel.CENTER);
mysliderLabel.setAlignmentX(CENTER_ALIGNMENT);
myslider = new JSlider(JSlider.HORIZONTAL, NOYRSMIN, NOYRSMAX, NOYRSINIT);
myslider.setMajorTickSpacing(4);
myslider.setMinorTickSpacing(1);
myslider.setPaintTicks(true);
myslider.setPaintLabels(true);
mycentralPanel.add(mysliderLabel);
mycentralPanel.add(myslider);
add(mycentralPanel, BorderLayout.CENTER);
mybPanel = new JPanel();
sIButton = new JButton("SimpleInterest");
JButton cIButton = new JButton("CompoundInterest");
JButton bothButton = new JButton("BothInterests");
mybPanel.add(sIButton);
mybPanel.add(cIButton);
mybPanel.add(bothButton);
add(mybPanel, BorderLayout.SOUTH);
sIButton.addActionListener(new ButtonListener());
cIButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
double myP = getP();
double myR = getR();
int myY = getYrs();
mydisplay.setText(cITable(myP,myR, myY));
}
});
bothButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
double myP = getP();
double myR = getR();
int myY = getYrs();
mydisplay.setText(bothtable(myP, myR, myY));
}
});
}
private double getP()
{
try
{
return Double.parseDouble(principalText.getText());
}
catch(NumberFormatException e)
{
mydisplay.setText("Principal amounts are numbers.");
return 0;
}
}
private double getmyMonthly()
{
try
{
return Double.parseDouble(mymonthlyPay.getText());
}
catch(NumberFormatException ee)
{
mydisplay.setText("Put number!");
return 0;
}
}
private double getR()
{
try
{
return Double.parseDouble(percentageText.getText());
}
catch(NumberFormatException ef)
{
mydisplay.setText("Put number without %");
return 0;
}
}
private int getYrs()
{
return myslider.getValue();
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double myP = getP();
double myR = getR();
int myY = getYrs();
mydisplay.setText(sITable(myP, myR,myY));
}
}
public static void createDisplayGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame myframe = new JFrame("Interest Table Calculator");
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setLocation(100, 100);
InterestTableGUI13 mytable = new InterestTableGUI13(675, 300);
myframe.setContentPane(mytable);
myframe.pack();
myframe.setVisible(true);
}
public static double sI(double myP, double rP, double myY)
{
return (myP + myP*(rP/100)*myY);
}
public static String formattedSI(double myP, double rP, double myY)
{
return formattedMoney(sI(myP, rP, myY));
}
public static double cI(double myP, double rP, double myY)
{
return (myP*Math.pow(1+rP/100, myY));
}
public static String formattedCI(double myP, double rP, double myY)
{
return formattedMoney(cI(myP, rP, myY));
}
private static String formattedMoney(double value)
{
NumberFormat myft = NumberFormat.getCurrencyInstance();
return myft.format(value);
}
public static String sITable(double myP, double rP, int myY)
{
String toReturn = "Principal: "+ formattedMoney(myP)+ ", Rate: "+rP;
toReturn+= " Year, Simple Interest Amount";
for(int myrows=1; myrows<= myY; myrows++)
{
toReturn = toReturn +" "+myrows+"-->"+formattedSI(myP, rP, myrows);
}
return toReturn;
}
public static String cITable(double myP, double rP, int myY)
{
String toReturn = "Principal: "+ formattedMoney(myP)+ ", Rate: "+rP;
toReturn+= " Year, Compound Interest Amount";
for(int myrows=1; myrows<= myY; myrows++)
{
toReturn = toReturn +" "+myrows+"-->"+formattedCI(myP, rP, myrows);
}
return toReturn;
}
public static String bothtable(double myP, double rP, int myY)
{
String toReturn = "Principal: "+ formattedMoney(myP)+ ", Rate: "+rP;
toReturn+= " Year, Simple Interest Amount, Compound Interest Amount";
for(int myrows=1; myrows<= myY; myrows++)
{
toReturn += " "+myrows+"-->"+
formattedSI(myP, rP, myrows)+
"-->"+formattedCI(myP, rP, myrows);
}
return toReturn;
}
public static void main(String[] args)
{
createDisplayGUI();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
