How to get textbox value from other class? - java

I have a problem regarding the JTextField, I always get a NullPointerException whenever i want to get the textfield value from my class Student and pass it to my other class which is class myAction what i want to happen is that after i clicked the button or Jbutton all the data in the textfield will be save to the array, and i already have an actionlistener to my button and create a class myAction.
Here are my code:
Student class(Main class)
/*
package Student;
import java.util.Scanner;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student{
private static Scanner input = new Scanner(System.in);
public String[] name = {"Lara", "Jerome", "Ferdie", "Jeffrey", "Eduard", "King"};
public int[] grade = new int[6];
JFrame frame;
JLabel label1, label2, label3, label4, label5, label6;
JPanel panel;
public JTextField text1, text2, text3, text4, text5, text6;
JButton button;
public static void main(String[] args)
{
Student myStudent = new Student();
myStudent.Layout();
//myStudent.
}
public void GetValue()
{
grade[0] = Integer.parseInt(text1.getText());
grade[1] = Integer.parseInt(text2.getText());
grade[2] = Integer.parseInt(text3.getText());
grade[3] = Integer.parseInt(text4.getText());
grade[4] = Integer.parseInt(text5.getText());
grade[5] = Integer.parseInt(text6.getText());
for(int i = 0; i < grade.length; i++)
{
System.out.println(grade[i]);
}
}
public void Layout()
{
//Design Layout
panel = new JPanel();
panel.setLayout(null);
frame = new JFrame("Student Grades");
button = new JButton("Get Grade");
button.setBounds(50, 260, 200, 30);
label1 = new JLabel("Your Grade: " + name[0]);
label1.setBounds(10, 10, 150, 30);
text1 = new JTextField(5);
text1.setBounds(140, 15, 150, 20);
label2 = new JLabel("Your Grade: " + name[1]);
label2.setBounds(10, 50, 150, 30);
text2 = new JTextField(5);
text2.setBounds(140, 55, 150, 20);
label3 = new JLabel("Your Grade: " + name[2]);
label3.setBounds(10, 90, 150, 30);
text3 = new JTextField(5);
text3.setBounds(140, 95, 150, 20);
label4 = new JLabel("Your Grade: " + name[3]);
label4.setBounds(10, 130, 150, 30);
text4 = new JTextField(5);
text4.setBounds(140, 135, 150, 20);
label5 = new JLabel("Your Grade: " + name[4]);
label5.setBounds(10, 170, 150, 30);
text5 = new JTextField(5);
text5.setBounds(140, 175, 150, 20);
label6 = new JLabel("Your Grade: " + name[5]);
label6.setBounds(10,210, 150, 30);
text6 = new JTextField(5);
text6.setBounds(140, 215, 150, 20);
frame.setSize(350, 350);
frame.setVisible(true);
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(label6);
panel.add(text6);
panel.add(button);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyEvent myAction = new MyEvent();
button.addActionListener(myAction);
}
}
and my class myAction(or actionlistener) class
class MyEvent implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Get Grade"))
{
Student EventStudent = new Student();
EventStudent.GetValue();
}
}
}
*/
here are all my code hope you can help me.

The problem in your code is you created a Student object in main.So your text fields are associated with that object.Now in your event listener you are creating another object of the student that doesn't belong with any of the component you need.you have to retrieve data from the object in the main method.
Do like below.Hopefully it will help.
import java.util.Scanner;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student{
static Student myStudent ;
private static Scanner input = new Scanner(System.in);
public String[] name = {"Lara", "Jerome", "Ferdie", "Jeffrey", "Eduard", "King"};
public int[] grade = new int[6];
JFrame frame;
JLabel label1, label2, label3, label4, label5, label6;
JPanel panel;
public JTextField text1, text2, text3, text4, text5, text6;
JButton button;
public static void main(String[] args)
{
myStudent = new Student();
myStudent.Layout();
//myStudent.
}
public void GetValue()
{
grade[0] = Integer.parseInt(text1.getText());
grade[1] = Integer.parseInt(text2.getText());
grade[2] = Integer.parseInt(text3.getText());
grade[3] = Integer.parseInt(text4.getText());
grade[4] = Integer.parseInt(text5.getText());
grade[5] = Integer.parseInt(text6.getText());
for(int i = 0; i < grade.length; i++)
{
System.out.println(grade[i]);
}
}
public void Layout()
{
//Design Layout
panel = new JPanel();
panel.setLayout(null);
frame = new JFrame("Student Grades");
button = new JButton("Get Grade");
button.setBounds(50, 260, 200, 30);
label1 = new JLabel("Your Grade: " + name[0]);
label1.setBounds(10, 10, 150, 30);
text1 = new JTextField(5);
text1.setBounds(140, 15, 150, 20);
label2 = new JLabel("Your Grade: " + name[1]);
label2.setBounds(10, 50, 150, 30);
text2 = new JTextField(5);
text2.setBounds(140, 55, 150, 20);
label3 = new JLabel("Your Grade: " + name[2]);
label3.setBounds(10, 90, 150, 30);
text3 = new JTextField(5);
text3.setBounds(140, 95, 150, 20);
label4 = new JLabel("Your Grade: " + name[3]);
label4.setBounds(10, 130, 150, 30);
text4 = new JTextField(5);
text4.setBounds(140, 135, 150, 20);
label5 = new JLabel("Your Grade: " + name[4]);
label5.setBounds(10, 170, 150, 30);
text5 = new JTextField(5);
text5.setBounds(140, 175, 150, 20);
label6 = new JLabel("Your Grade: " + name[5]);
label6.setBounds(10,210, 150, 30);
text6 = new JTextField(5);
text6.setBounds(140, 215, 150, 20);
frame.setSize(350, 350);
frame.setVisible(true);
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(label6);
panel.add(text6);
panel.add(button);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyEvent myAction = new MyEvent(myStudent);
button.addActionListener(myAction);
}
}
class MyEvent implements ActionListener
{
Student myStudent;
public MyEvent(Student myStudent) {
this.myStudent=myStudent;
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Get Grade"))
{
myStudent.GetValue();
}
}
}

Related

How to load Text from TextField, checkbox, RadioButton, ComboBox to one Big Text Field

I'm writing a simple program for recruiting employees.
I have few questions with JtextField, few checkboxes with text, a RadioButton with text and a Combobox.
I need the data entered in the text windows, checkbox, drop-down list to be thrown into the large window on the right.
At the moment there are only data from the text windows, but not from the top just in the middle of the field.
package appka;
import javax.swing.*;
import java.awt.Checkbox;
import java.awt.event.*;
public class CzwartaApp {
public static void main(String[] args) {
// Klasy obiektow
JFrame frame = new JFrame("Czwarta Aplikacja");
JButton btnSubmit = new JButton("Wypisz");
JButton btnExit = new JButton("Wyjdz");
JLabel lbl1 = new JLabel("imię");
JLabel lbl2 = new JLabel("nazwisko");
JLabel lbl3 = new JLabel("stanowisko");
JLabel lbl4 = new JLabel("E-mail");
JLabel lbl5 = new JLabel("");
JLabel lbl6 = new JLabel("Jakie znasz języki programowania?");
JLabel lbl7 = new JLabel("Wybierz poziom języka angielskiego?");
JLabel lbl8 = new JLabel("Wybierz kurs programowania?");
JLabel lbl9 = new JLabel("Dane Kontaktowe");
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
JTextField tf5 = new JTextField();
Checkbox checkbox1 = new Checkbox("Java");
Checkbox checkbox2 = new Checkbox("Python");
Checkbox checkbox3 = new Checkbox("Inne");
JRadioButton btn1 = new JRadioButton();
btn1.setText("podstawowy");
JRadioButton btn2 = new JRadioButton();
btn2.setText("średnio-zaawansowany");
JRadioButton btn3 = new JRadioButton();
btn3.setText("zaawansowany");
ButtonGroup group = new ButtonGroup ();
group.add(btn1); group.add(btn2); group.add(btn3);
JComboBox kurs = new JComboBox();
kurs.addItem("FrontEnd Developer");
kurs.addItem("BackEnd Developer");
// Pozycjonowanie
lbl1.setBounds(20, 20, 100, 20);
tf1.setBounds(20, 70, 100, 20);
lbl2.setBounds(20, 120, 100, 20);
tf2.setBounds(20, 170, 100, 20);
lbl3.setBounds(20, 220, 100, 20);
tf3.setBounds(20, 270, 100, 20);
lbl4.setBounds(20, 320, 100, 20);
tf4.setBounds(20, 370, 100, 20);
tf5.setBounds(500, 20, 250, 300);
lbl5.setBounds(20, 420, 300, 20);
lbl6.setBounds(200, 20, 250, 20);
lbl7.setBounds(200, 220, 250, 20);
lbl8.setBounds(200, 350, 250, 20);
lbl9.setBounds(500, 50, 200, 20);
btnSubmit.setBounds(20, 470, 100, 20);
btnExit.setBounds(220, 470, 100, 20);
checkbox1.setBounds(200, 70, 250, 20);
checkbox2.setBounds(200, 120, 250, 20);
checkbox3.setBounds(200, 170, 250, 20);
btn1.setBounds(200, 250, 250, 20);
btn2.setBounds(200, 280, 250, 20);
btn3.setBounds(200, 310, 250, 20);
kurs.setBounds(200, 380, 250, 20);
// Obsluga zdarzen
btnSubmit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String imie = tf1.getText();
String nazwisko = tf2.getText();
String stanowisko = tf3.getText();
String email = tf4.getText();
tf5.setText(imie+" "+nazwisko+" "+stanowisko+" "+email);
}
});
btnExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// Dodanie do okna
frame.add(btnSubmit); frame.add(btnExit);
frame.add(tf1); frame.add(tf2); frame.add(tf3);frame.add(tf4);frame.add(tf5);
frame.add(lbl1); frame.add(lbl2); frame.add(lbl3); frame.add(lbl4);frame.add(lbl5);
frame.add(lbl6);frame.add(lbl7);frame.add(lbl8);frame.add(lbl9);
frame.add(checkbox1); frame.add(checkbox2); frame.add(checkbox3);
frame.add(btn1); frame.add(btn2); frame.add(btn3);
frame.getContentPane().add(kurs);
frame.setSize(800, 600);
frame.setLayout(null);
frame.setVisible(true);
}
}
Here is the corrected code.
import java.awt.Checkbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CzwartaApp {
public static void main(String[] args) {
// Klasy obiektow
JFrame frame = new JFrame("Czwarta Aplikacja");
JButton btnSubmit = new JButton("Wypisz");
JButton btnExit = new JButton("Wyjdz");
JLabel lbl1 = new JLabel("imie");
JLabel lbl2 = new JLabel("nazwisko");
JLabel lbl3 = new JLabel("stanowisko");
JLabel lbl4 = new JLabel("E-mail");
JLabel lbl5 = new JLabel("");
JLabel lbl6 = new JLabel("Jakie znasz jezyki programowania?");
JLabel lbl7 = new JLabel("Wybierz poziom jezyka angielskiego?");
JLabel lbl8 = new JLabel("Wybierz kurs programowania?");
JLabel lbl9 = new JLabel("Dane Kontaktowe");
final JTextField tf1 = new JTextField();
final JTextField tf2 = new JTextField();
final JTextField tf3 = new JTextField();
final JTextField tf4 = new JTextField();
final JTextArea tf5 = new JTextArea();
final Checkbox checkbox1 = new Checkbox("Java");
final Checkbox checkbox2 = new Checkbox("Python");
final Checkbox checkbox3 = new Checkbox("Inne");
JRadioButton btn1 = new JRadioButton();
btn1.setText("podstawowy");
JRadioButton btn2 = new JRadioButton();
btn2.setText("srednio-zaawansowany");
JRadioButton btn3 = new JRadioButton();
btn3.setText("zaawansowany");
ButtonGroup group = new ButtonGroup();
group.add(btn1);
group.add(btn2);
group.add(btn3);
final JComboBox kurs = new JComboBox();
kurs.addItem("FrontEnd Developer");
kurs.addItem("BackEnd Developer");
// Pozycjonowanie
lbl1.setBounds(20, 20, 100, 20);
tf1.setBounds(20, 70, 100, 20);
lbl2.setBounds(20, 120, 100, 20);
tf2.setBounds(20, 170, 100, 20);
lbl3.setBounds(20, 220, 100, 20);
tf3.setBounds(20, 270, 100, 20);
lbl4.setBounds(20, 320, 100, 20);
tf4.setBounds(20, 370, 100, 20);
tf5.setBounds(500, 20, 250, 300);
lbl5.setBounds(20, 420, 300, 20);
lbl6.setBounds(200, 20, 250, 20);
lbl7.setBounds(200, 220, 250, 20);
lbl8.setBounds(200, 350, 250, 20);
lbl9.setBounds(500, 50, 200, 20);
btnSubmit.setBounds(20, 470, 100, 20);
btnExit.setBounds(220, 470, 100, 20);
checkbox1.setBounds(200, 70, 250, 20);
checkbox2.setBounds(200, 120, 250, 20);
checkbox3.setBounds(200, 170, 250, 20);
btn1.setBounds(200, 250, 250, 20);
btn2.setBounds(200, 280, 250, 20);
btn3.setBounds(200, 310, 250, 20);
kurs.setBounds(200, 380, 250, 20);
// Obsluga zdarzen
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String imie = tf1.getText();
String nazwisko = tf2.getText();
String stanowisko = tf3.getText();
String email = tf4.getText();
StringBuffer buffer = new StringBuffer();
buffer.append( "imie : ").append( imie ).append("\n");
buffer.append( "nazwisko : ").append( nazwisko ).append("\n");
buffer.append( "stanowisko : ").append( stanowisko ).append("\n");
buffer.append( "E-mail : ").append( email ).append("\n");
buffer.append( "Jakie znasz jezyki programowania? : ");
if( checkbox1.getState() ) {
buffer.append( checkbox1.getLabel() ).append(",");
}
if( checkbox2.getState() ) {
buffer.append( checkbox2.getLabel() ).append(",");
}
if( checkbox3.getState() ) {
buffer.append( checkbox3.getLabel() );
}
buffer.append("\n");
buffer.append( "Wybierz kurs programowania? ").append( kurs.getSelectedItem().toString() ).append("\n");
tf5.setText(buffer.toString());
}
});
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Dodanie do okna
frame.add(btnSubmit);
frame.add(btnExit);
frame.add(tf1);
frame.add(tf2);
frame.add(tf3);
frame.add(tf4);
frame.add(tf5);
frame.add(lbl1);
frame.add(lbl2);
frame.add(lbl3);
frame.add(lbl4);
frame.add(lbl5);
frame.add(lbl6);
frame.add(lbl7);
frame.add(lbl8);
frame.add(lbl9);
frame.add(checkbox1);
frame.add(checkbox2);
frame.add(checkbox3);
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.getContentPane().add(kurs);
frame.setSize(800, 600);
frame.setLayout(null);
frame.setVisible(true);
}
}

Java multiple menu item with an event listener?

I have two menu items here ,one is for the student information and the another one is for the teacher information.i have added the menu for student and teacher both.
I am struck in the event listener how to add the action to the menu and menu1,suppose when the user click on menu 1 then student information should be displayed and for the menu2 the teacher information action should be performed.i am confused where to add the action listenr and perform the operation for the student and teacher.
I am new to the the event and action listner in java.kindly suggest the solution
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Searchdb extends JFrame implements ActionListener {
//Initializing Components
JLabel lb,lbd,lb1, lb2, lb3, lb5;
JTextField tf1, tf2,tf3,tf5,tfd;
JButton btn;
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Student");
JMenu menu1 = new JMenu("Teacher");
//Creating Constructor for initializing JFrame components
Searchdb() {
//Providing Title
super("Fetching Roll Information");
setJMenuBar(menuBar);
menuBar.add(menu);
menuBar.add(menu1);
lb5 = new JLabel("Roll Number:");
lb5.setBounds(20, 20, 100, 20);
tf5 = new JTextField(20);
tf5.setBounds(130, 20, 200, 20);
lbd = new JLabel("Date:");
lbd.setBounds(20, 50, 100, 20);
tfd = new JTextField(20);
tfd.setBounds(130, 50, 200, 20);
btn = new JButton("Submit");
btn.setBounds(50, 50, 100, 20);
btn.addActionListener(this);
lb = new JLabel("Fetching Student Information From Database");
lb.setBounds(30, 80, 450, 30);
lb.setForeground(Color.black);
lb.setFont(new Font("Serif", Font.PLAIN, 12));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
lb1 = new JLabel("Name:");
lb1.setBounds(20, 120, 100, 20);
tf1 = new JTextField(50);
tf1.setBounds(130, 120, 200, 20);
lb2 = new JLabel("Fathername:");
lb2.setBounds(20, 150, 100, 20);
tf2 = new JTextField(100);
tf2.setBounds(130, 150, 200, 20);
lb3 = new JLabel("State:");
lb3.setBounds(20, 180, 100, 20);
tf3 = new JTextField(50);
tf3.setBounds(130, 180, 200, 20);
setLayout(null);
//Add components to the JFrame
add(lb5);
add(tf5);
add(lbd);
add(tfd);
add(btn);
add(lb);
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
//Set TextField Editable False
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
}
public void actionPerformed(ActionEvent e) {
//Create DataBase Coonection and Fetching Records
try {
String str = tf5.getText();
Datestri = tfd.getText();//Getting the unable to convert String to Date error
System.out.println(str);
System.out.println(stri);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#//host:port/servicename","username","password");
PreparedStatement st = con.prepareStatement("select Name,Fathername,State from student_db where roll_number=? and medium=?");
System.out.println(st);
st.setString(1, str);
st.setDate(2, stri);
//Excuting Query
ResultSet rs = st.executeQuery();
System.out.println(rs);
if (rs.next()) {
String s = rs.getString(1);
String s1 = rs.getString(2);
String s2 = rs.getString(3);
//Sets Records in TextFields.
tf1.setText(s);
tf2.setText(s1);
tf3.setText(s2);
} else {
JOptionPane.showMessageDialog(null, "Student not Found");
}
//Create Exception Handler
} catch (Exception ex) {
System.out.println(ex);
}
}
public void actionPerformed(ActionEvent e) {
//Create DataBase Coonection and Fetching Records
//Teacher information should be retrieved from the db
}
//Running Constructor
public static void main(String args[]) {
new Searchdb();
}
}
Thanks for any help in advance.
Hope, This will help
For JMenu you can use MenuListener
class MenuListenerAdapter implements MenuListener {
#Override
public void menuSelected(MenuEvent e) {
System.out.println("Menu Selected");
}
#Override
public void menuDeselected(MenuEvent e) {
System.out.println("Menu Deselected");
}
#Override
public void menuCanceled(MenuEvent e) {
System.out.println("Menu Canceled");
}
}
Then add MenuListener on menu
menu.addMenuListener(new MenuListenerAdapter());
You can use MouseListener,
Find your solution below, check out console when you click on Student and Teacher Menu
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
public class Searchdb extends JFrame implements ActionListener {
//Initializing Components
JLabel lb, lbd, lb1, lb2, lb3, lb5;
JTextField tf1, tf2, tf3, tf5, tfd;
JButton btn;
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Student");
JMenu menu1 = new JMenu("Teacher");
//Creating Constructor for initializing JFrame components
Searchdb() {
//Providing Title
super("Fetching Roll Information");
setJMenuBar(menuBar);
menuBar.add(menu);
menuBar.add(menu1);
menu.addMouseListener(new MouseListenerForStudentAndTeacher());
menu1.addMouseListener(new MouseListenerForStudentAndTeacher());
lb5 = new JLabel("Roll Number:");
lb5.setBounds(20, 20, 100, 20);
tf5 = new JTextField(20);
tf5.setBounds(130, 20, 200, 20);
lbd = new JLabel("Date:");
lbd.setBounds(20, 50, 100, 20);
tfd = new JTextField(20);
tfd.setBounds(130, 50, 200, 20);
btn = new JButton("Submit");
btn.setBounds(50, 50, 100, 20);
btn.addActionListener(this);
lb = new JLabel("Fetching Student Information From Database");
lb.setBounds(30, 80, 450, 30);
lb.setForeground(Color.black);
lb.setFont(new Font("Serif", Font.PLAIN, 12));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
lb1 = new JLabel("Name:");
lb1.setBounds(20, 120, 100, 20);
tf1 = new JTextField(50);
tf1.setBounds(130, 120, 200, 20);
lb2 = new JLabel("Fathername:");
lb2.setBounds(20, 150, 100, 20);
tf2 = new JTextField(100);
tf2.setBounds(130, 150, 200, 20);
lb3 = new JLabel("State:");
lb3.setBounds(20, 180, 100, 20);
tf3 = new JTextField(50);
tf3.setBounds(130, 180, 200, 20);
setLayout(null);
//Add components to the JFrame
add(lb5);
add(tf5);
add(lbd);
add(tfd);
add(btn);
add(lb);
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
//Set TextField Editable False
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
}
public void actionPerformed(ActionEvent e) {
// removed code, you can add your code later on
}
public static void main(String args[]) {
new Searchdb();
}
private class MouseListenerForStudentAndTeacher extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == menu) {
System.out.println("Student Menu Clicked");
}
if (e.getSource() == menu1) {
System.out.println("Teacher Menu Clicked");
}
}
}
}

Java GUI Calculator JButton convert setText (Double)

I am trying to make an Infix to Postfix GUI calculator. I need to add normal calculator buttons to the calculator and i want there to be "PI" button that holds the value of pi. When the action is called i would like the value of "Pi" to appear in the input.setText field. However, when i attempt to do so, i run into a problem with converting the double into a type that the textField accepts.
import acm.program.*;
import acm.graphics.*; // GCanvas and G-friends
import javax.swing.*; // JButton and J-friends
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;
public class Calc extends Program
{
JTextField input;
JTextField output;
JTextField result;
public Calc()
{
start();
setSize(500, 500);
}
public void init()
{
GCanvas canvas = new GCanvas();
add(canvas);
canvas.setBackground(Color.lightGray);
JButton conversionButton = new JButton(" DEG ");
canvas.add(conversionButton, 85, 60);
JButton conversionButton2 = new JButton("RAD");
canvas.add(conversionButton2, 170, 60);
conversionButton2.setSize(conversionButton.getSize());
JButton goButton = new JButton("Go!");
canvas.add(goButton, 280, 350);
goButton.setBackground(Color.green);
goButton.setSize(conversionButton.getSize());
JButton clearButton = new JButton("Clear");
canvas.add(clearButton, 380, 350);
clearButton.setBackground(Color.red);
clearButton.setSize(conversionButton.getSize());
JButton graphButton = new JButton("Graph");
canvas.add(graphButton, 0, 120);
graphButton.setSize(conversionButton.getSize());
JButton factorialButton = new JButton("!");
canvas.add(factorialButton, 85, 120);
factorialButton.setSize(conversionButton.getSize());
JButton ANSButton = new JButton("ANS");
canvas.add(ANSButton, 0, 90);
ANSButton.setSize(conversionButton.getSize());
JButton LnButton = new JButton("Ln");
canvas.add(LnButton, 85, 90);
LnButton.setSize(conversionButton.getSize());
JButton AbsButton = new JButton("| |");
canvas.add(AbsButton, 170, 90);
AbsButton.setSize(conversionButton.getSize());
JButton PIButton = new JButton("PI");
canvas.add(PIButton, 170, 120);
PIButton.setSize(conversionButton.getSize());
JLabel infixLabel = new JLabel("Infix");
canvas.add(infixLabel, 275, 40);
JLabel postfixLabel = new JLabel("Postfix");
canvas.add(postfixLabel, 275, 160);
JLabel resultLabel = new JLabel("Result");
canvas.add(resultLabel, 275, 280);
JLabel messageLabel = new JLabel("Note: this calculator
can only graph functions of the 3rd degree or lower");
canvas.add(messageLabel, 25, 10);
input = new JTextField();
canvas.add(input, 275, 60);
input.setSize(200, 30);
output = new JTextField();
canvas.add(output, 275, 180);
output.setSize(200, 30);
result = new JTextField();
canvas.add(result, 275, 300);
result.setSize(200, 30);
addActionListeners();
}
public void actionPerformed(ActionEvent ae)
{
String action = ae.getActionCommand();
if (action.equals("Clear"))
{
input.setText("");
output.setText("");
result.setText("");
}
else if (action.equals("Go!"))
{
// Get input from user
String c = input.getText();
result.setText(input.getText());
}
if (action.equals("PI"))
{
double Pi = 3.145926;
Double.toString(Pi);
input.setText(String.Pi);
}
}
}

Making a save button work in java

I need to build a GUI that will allow users to enter basic details about themselves for a new patient form and have that information saved to a .txt file. This data should then be viewable at a later time, but I can't get the save button to work.
//Import packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
//Main class
public class PatientDetailsWindow{
//Declare variables
public JFrame frame1;
public JPanel panel;
public JButton btnSave, btnExit;
public JLabel lblFirstName, lblSurname, lblGender, lblDOB, lblSmoker, lblMedHistory, lblFSlash1, lblFSlash2, lblImage;
public JTextField txtFirstName, txtSurname, txtGender, txtSmoker, txtMedHistory, txtDOB1, txtDOB2, txtDOB3;
public Insets insets;
public JTextArea textArea = new JTextArea();
public static void main (String args[]){
new PatientDetailsWindow();
}
public PatientDetailsWindow(){
createFrame();
createLabels();
createTextFields();
createButtons();
}
//Create the frame
public void createFrame(){
frame1 = new JFrame ("Personal details");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize (600,600);
panel = new JPanel();
panel.setBackground(Color.gray);
panel.setBounds(0, 0, 600, 600);
panel.setLayout(null);
frame1.add(panel);
frame1.setVisible(true);
}
//Creating Labels
public void createLabels(){
lblFirstName = new JLabel ("First Name: ");
lblFirstName.setBounds(50, 50, 500, 20);
lblFirstName.setForeground(Color.blue);
panel.add (lblFirstName);
lblSurname = new JLabel ("Surname: ");
lblSurname.setBounds(50, 100, 500, 20);
lblSurname.setForeground(Color.blue);
panel.add (lblSurname);
lblGender = new JLabel ("Gender: ");
lblGender.setBounds(50, 150, 500, 20);
lblGender.setForeground(Color.blue);
panel.add (lblGender);
lblDOB = new JLabel ("Date of Birth: ");
lblDOB.setBounds(50, 200, 500, 20);
lblDOB.setForeground(Color.blue);
panel.add (lblDOB);
lblFSlash1 = new JLabel ("/");
lblFSlash1.setBounds(440, 200, 20, 20);
lblFSlash1.setForeground(Color.blue);
panel.add (lblFSlash1);
lblFSlash2 = new JLabel ("/");
lblFSlash2.setBounds(490, 200, 40, 20);
lblFSlash2.setForeground(Color.blue);
panel.add (lblFSlash2);
lblSmoker = new JLabel ("Are you a smoker? ");
lblSmoker.setBounds(50, 250, 500, 20);
lblSmoker.setForeground(Color.blue);
panel.add (lblSmoker);
lblMedHistory = new JLabel ("Any other previous medical history? ");
lblMedHistory.setBounds(50, 300, 500, 20);
lblMedHistory.setForeground(Color.blue);
panel.add (lblMedHistory);
/*ImageIcon image= new ImageIcon("heartandstethoscope.jpg");
JLabel lblImage = new JLabel(image);
panel.add(lblImage);
*/
}
//Creating Text Fields
public void createTextFields(){
txtFirstName = new JTextField (10);
txtFirstName.setBounds(400, 50, 100, 20);
txtFirstName.setForeground(Color.blue);
panel.add (txtFirstName);
txtSurname = new JTextField (10);
txtSurname.setBounds(400, 100, 100, 20);
txtSurname.setForeground(Color.blue);
panel.add (txtSurname);
txtGender = new JTextField (10);
txtGender.setBounds(400, 150, 100, 20);
txtGender.setForeground(Color.blue);
panel.add (txtGender);
txtDOB1 = new JTextField (2);
txtDOB1.setBounds(400, 200, 40, 20);
txtDOB1.setForeground(Color.blue);
panel.add (txtDOB1);
txtDOB2 = new JTextField (2);
txtDOB2.setBounds(450, 200, 40, 20);
txtDOB2.setForeground(Color.blue);
panel.add (txtDOB2);
txtDOB3 = new JTextField (4);
txtDOB3.setBounds(500, 200, 80, 20);
txtDOB3.setForeground(Color.blue);
panel.add (txtDOB3);
txtSmoker = new JTextField (3);
txtSmoker.setBounds(400, 250, 100, 20);
txtSmoker.setForeground(Color.blue);
panel.add (txtSmoker);
txtMedHistory = new JTextField (300);
txtMedHistory.setBounds(400, 300, 100, 60);
txtMedHistory.setForeground(Color.blue);
panel.add (txtMedHistory);
JScrollPane areaScrollPane = new JScrollPane(txtMedHistory);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setBounds(400, 300, 100, 80);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
textArea.setLineWrap(true);
textArea.setEditable(true);
textArea.setVisible(true);
panel.add(textArea);
}
//Creating buttons
public void createButtons(){
btnSave = new JButton ("Save");
btnSave.setBounds(130, 350, 100, 20);
btnSave.setForeground(Color.blue);
btnSave.addActionListener(new SaveHandler());
panel.add (btnSave);
btnSave.setVisible(true);
btnExit = new JButton ("Exit");
btnExit.setBounds(240, 350, 100, 20);
btnExit.setForeground(Color.blue);
btnExit.addActionListener(new ExitHandler());
panel.add (btnExit);
btnExit.setVisible(true);
}
class ExitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame1,
"Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
System.out.println("EXIT SUCCESSFUL");
}
}
}
class SaveHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
try( PrintWriter out = new PrintWriter( "DetailsofPatient.txt" )){
out.println(txtFirstName.getText());
out.println(txtGender.getText());
out.println(txtDOB1.getText());
out.println(txtDOB2.getText());
out.println(txtDOB3.getText());
out.println(txtSmoker.getText());
out.println(txtMedHistory.getText());
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
System.out.println("Save successful");
}
}
}
Alright for starters, you didn't add an ActionListener to your save button, so you need to add it like this.
btnSave = new JButton ("Save");
btnSave.setBounds(130, 350, 100, 20);
btnSave.setForeground(Color.blue);
btnSave.addActionListener(new SaveHandler());
panel.add (btnSave);
btnSave.setVisible(true);
Also, in your SaveHandler, your PrintWriter isn't actually printing anything.
Your Code: out.println( ); would not write anything.
Let's say you wanted to write the contents of txtFirstName, you would need to do
out.println(txtFirstName.getText());
Edit: You also need another closing braces at the end of your ExitHandler class
class ExitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame1,
"Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
System.out.println("EXIT SUCCESSFUL");
}
}
}
You will also then need to remove a closing brace at the end of the program.

plMortgage Calculator GUI Issue with A Lot

I am having a lot of issues with this application. I have been at this all day and cannot get this figured out. I have a Java application that is for a class. The issue that I am having is trying to get the JRadioButtons assigned to variables in the array then passing them into the formula. If someone could help I would appreciate it a lot.
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JRadioButton;
public class MortgageCalculatorGUI8 extends JFrame {
JPanel panel1 = new JPanel();
double Principal;
double [] Interest = {5.35, 5.5, 5.75};
double temp_Interest;
int [] Length = {7, 15, 30};
int temp_Length;
boolean ok = false;
public MortgageCalculatorGUI8(){
getContentPane ().setLayout (null);
setSize (400,400);
panel1.setLayout (null);
panel1.setBounds (0, 0, 2000, 800);
add (panel1);
JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
mortgageLabel.setBounds (15, 15, 150, 30);
panel1.add (mortgageLabel);
final JTextField mortgageText = new JTextField(10);
mortgageText.setBounds (130, 15, 150, 30);
panel1.add (mortgageText);
JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
termLabel.setBounds (340, 40, 150, 30);
panel1.add (termLabel);
JTextField termText = new JTextField(3);
termText.setBounds (340, 70, 150, 30);
panel1.add (termText);
JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
intRateLabel.setBounds (340, 94, 150, 30);
panel1.add (intRateLabel);
JTextField intRateText = new JTextField(5);
intRateText.setBounds (340, 120, 150, 30);
panel1.add (intRateText);
JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
mPaymentLabel.setBounds (550, 40, 150, 30);
panel1.add (mPaymentLabel);
JTextField mPaymentText = new JTextField(10);
mPaymentText.setBounds (550, 70, 150, 30);
panel1.add (mPaymentText);
// JLabel paymentLabel = new JLabel ("Payment #");
// JLabel balLabel = new JLabel (" Balance");
// JLabel ytdPrincLabel = new JLabel (" Principal");
// JLabel ytdIntLabel = new JLabel (" Interest");
JTextArea textArea = new JTextArea(10, 31);
textArea.setBounds (550, 100, 150, 30);
panel1.add (textArea);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setBounds (450, 200, 300, 150);
panel1.add (scroll);
public void rbuttons(){
JLabel tYears = new JLabel("Years of Loan Amount");
tYears.setBounds (30, 35, 150, 30);
panel1.add (tYears);
JRadioButton b7Yr = new JRadioButton("7 Years",false);
b7Yr.setBounds (30, 58, 150, 30);
panel1.add (b7Yr);
JRadioButton b15Yr = new JRadioButton("15 Years",false);
b15Yr.setBounds (30, 80, 150, 30);
panel1.add (b15Yr);
JRadioButton b30Yr = new JRadioButton("30 Years",false);
b30Yr.setBounds (30, 101, 150, 30);
panel1.add (b30Yr);
JLabel tInterest = new JLabel("Interest Rate Of the Loan");
tInterest.setBounds (175, 35, 150, 30);
panel1.add(tInterest);
JRadioButton b535Int = new JRadioButton("5.35% Interest",false);
b535Int.setBounds (178, 58, 150, 30);
panel1.add (b535Int);
JRadioButton b55Int = new JRadioButton("5.5% Interest",false);
b55Int.setBounds (178, 80, 150, 30);
panel1.add (b55Int);
JRadioButton b575Int = new JRadioButton("5.75% Interest",false);
b575Int.setBounds (178, 101, 150, 30);
panel1.add (b575Int);
}
JButton calculateButton = new JButton("CALCULATE");
calculateButton.setBounds (30, 400, 120, 30);
panel1.add (calculateButton);
calculateButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(e.getSource () == b7Yr){
b7Yr = Length[0];
}
if(e.getSource () == b15Yr){
b15Yr = Length[1];
}
if(e.getSource () == b30Yr){
b30Yr = Length[2];
}
if(e.getSource () == b535Int){
b535Int = Interest[0];
}
if(e.getSource () == b55Int){
b55Int = Interest[1];
}
if(e.getSource () == b575Int){
b575Int = Interest[2];
}
double Principal;
// double [] Interest;
// int [] Length;
double M_Interest = Interest /(12*100);
double Months = Length * 12;
Principal = Double.parseDouble (mortgageText.getText());
double M_Payment = Principal * ( M_Interest / (1 - (Math.pow((1 + M_Interest), - Months))));
NumberFormat Money = NumberFormat.getCurrencyInstance();
}
});
JButton clearButton = new JButton("CLEAR");
clearButton.setBounds (160, 400, 120, 30);
panel1.add (clearButton);
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
mortgageText.setText (null);
b7Yr.setSelected (false);
b15Yr.setSelected (false);
b30Yr.setSelected (false);
b535Int.setSelected (false);
b55Int.setSelected (false);
b575Int.setSelected (false);
}
});
JButton exitButton = new JButton("EXIT");
exitButton.setBounds (290, 400, 120, 30);
panel1.add (exitButton);
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args) {
MortgageCalculatorGUI8 frame = new MortgageCalculatorGUI8();
frame.setBounds (400, 200, 800, 800);
frame.setTitle ("Mortgage Calculator 1.0.4");
frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
frame.setVisible (true);
}
}
Shoot, I'll show you in an example of what I meant in my last comment:
Myself, I'd create an array of JRadioButtons as a class field for each group that I used as well as a ButtonGroup object for each cluster of JRadioButtons. Then in my calculate JButton's ActionListener, I'd get the selected radiobutton by either looping through the radio button array or from the ButtonGroups getSelection method (note though that this returns a ButtonModel object or null if nothing is selected).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InfoFromRadioBtns extends JPanel {
private static final long serialVersionUID = 1L;
private int[] foobars = {1, 2, 5, 10, 20};
private JRadioButton[] foobarRButtons = new JRadioButton[foobars.length];
private ButtonGroup foobarBtnGroup = new ButtonGroup();
public InfoFromRadioBtns() {
// jpanel to hold one set of radio buttons
JPanel radioBtnPanel = new JPanel(new GridLayout(0, 1));
radioBtnPanel.setBorder(BorderFactory
.createTitledBorder("Choose a Foobar"));
// iterate through the radio button array creating buttons
// and adding them to the radioBtnPanel and the
// foobarBtnGroup ButtonGroup
for (int i = 0; i < foobarRButtons.length; i++) {
// string for radiobutton to dislay -- just the number
String buttonText = String.valueOf(foobars[i]);
JRadioButton radiobtn = new JRadioButton("foobar " + buttonText);
radiobtn.setActionCommand(buttonText); // one way to find out which
// button is selected
radioBtnPanel.add(radiobtn); // add radiobutton to its panel
foobarBtnGroup.add(radiobtn); // add radiobutton to its button group
// add to array
foobarRButtons[i] = radiobtn;
}
// one way to get the selected JRadioButton
JButton getRadioChoice1 = new JButton("Get Radio Choice 1");
getRadioChoice1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonModel seletedModel = foobarBtnGroup.getSelection();
if (seletedModel != null) {
String actionCommand = seletedModel.getActionCommand();
System.out.println("selected foobar: " + actionCommand);
} else {
System.out.println("No foobar selected");
}
}
});
// another way to get the selected JRadioButton
JButton getRadioChoice2 = new JButton("Get Radio Choice 2");
getRadioChoice2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String actionCommand = "";
for (JRadioButton foobarRButton : foobarRButtons) {
if (foobarRButton.isSelected()) {
actionCommand = foobarRButton.getActionCommand();
}
}
if (actionCommand.isEmpty()) {
System.out.println("No foobar selected");
} else {
System.out.println("selected foobar: " + actionCommand);
}
}
});
JPanel jBtnPanel = new JPanel();
jBtnPanel.add(getRadioChoice1);
jBtnPanel.add(getRadioChoice2);
// make main GUI use a BordeLayout
setLayout(new BorderLayout());
add(radioBtnPanel, BorderLayout.CENTER);
add(jBtnPanel, BorderLayout.PAGE_END);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("InfoFromRadioBtns");
frame.getContentPane().add(new InfoFromRadioBtns());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
Whatever you do, don't try to copy and paste any of this code into your program, because it simply isn't going to work that way (on purpose). It was posted only to illustrate the concepts that I've discussed above.
I would extend JRadioButton to create a class capable of holding the variables you want. You can do this as an inner class to keep things simple.
private double pctinterest;
private int numyears; // within scope of your containing class
private class RadioButtonWithYears extends JRadioButton {
final private int years;
private int getYears() { return years; }
public RadioButtonWithYears(int years) {
super(years + " years",false);
this.years = years;
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numyears = getYears();
}
});
}
}
// elsewhere
RadioButtonWithYears b7Yr = new RadioButtonWithYears(7);
RadioButtonWithYears b15Yr = new RadioButtonWithYears(15);
RadioButtonWithYears b30Yr = new RadioButtonWithYears(30);
// then later
double M_Interest = java.lang.Math.pow((pctinternet / 100)+1, numyears);
Update: It isn't too far gone to salvage. I have incorporated the ButtonGroup as per Eels suggestion, and made the GUI part of it work (although you'll have to fix the layout) and marked where you need to sort out the calculation.
package stack.swing;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JRadioButton;
public class MortgageCalculatorGUI8 extends JFrame {
JPanel panel1 = new JPanel();
Integer Principal;
boolean ok = false;
private BigDecimal temp_Interest;
private int temp_Length; // within scope of your containing class
private class JRadioButtonWithYears extends JRadioButton {
final private int years;
private int getYears() { return years; }
public JRadioButtonWithYears(int years) {
super(years + " years",false);
this.years = years;
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
temp_Length = getYears();
}
});
}
}
private class JRadioButtonWithPct extends JRadioButton {
final private BigDecimal pct;
private BigDecimal getPct() { return pct; }
public JRadioButtonWithPct(String pct) {
super(pct + "%",false);
this.pct = new BigDecimal(pct);
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
temp_Interest = getPct();
}
});
}
}
public MortgageCalculatorGUI8() {
getContentPane ().setLayout (null);
setSize (400,400);
panel1.setLayout (null);
panel1.setBounds (0, 0, 2000, 800);
add (panel1);
JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
mortgageLabel.setBounds (15, 15, 150, 30);
panel1.add (mortgageLabel);
final JTextField mortgageText = new JTextField(10);
mortgageText.setBounds (130, 15, 150, 30);
panel1.add (mortgageText);
JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
termLabel.setBounds (340, 40, 150, 30);
panel1.add (termLabel);
JTextField termText = new JTextField(3);
termText.setBounds (340, 70, 150, 30);
panel1.add (termText);
JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
intRateLabel.setBounds (340, 94, 150, 30);
panel1.add (intRateLabel);
JTextField intRateText = new JTextField(5);
intRateText.setBounds (340, 120, 150, 30);
panel1.add (intRateText);
JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
mPaymentLabel.setBounds (550, 40, 150, 30);
panel1.add (mPaymentLabel);
JTextField mPaymentText = new JTextField(10);
mPaymentText.setBounds (550, 70, 150, 30);
panel1.add (mPaymentText);
// JLabel paymentLabel = new JLabel ("Payment #");
// JLabel balLabel = new JLabel (" Balance");
// JLabel ytdPrincLabel = new JLabel (" Principal");
// JLabel ytdIntLabel = new JLabel (" Interest");
JTextArea textArea = new JTextArea(10, 31);
textArea.setBounds (550, 100, 150, 30);
panel1.add (textArea);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setBounds (450, 200, 300, 150);
panel1.add (scroll);
// jpanel to hold one set of radio buttons
JPanel yearsPanel = new JPanel(new GridLayout(0, 1));
yearsPanel.setBorder(BorderFactory
.createTitledBorder("Years of Loan Amount"));
yearsPanel.setBounds(30, 55, 150, 150);
panel1.add (yearsPanel);
final ButtonGroup yearsGroup = new ButtonGroup();
int years[] = { 7, 15, 30 };
for (int i = 0; i < years.length; i++) {
JRadioButtonWithYears radiobtn = new JRadioButtonWithYears(years[i]);
yearsPanel.add(radiobtn); // add radiobutton to its panel
yearsGroup.add(radiobtn); // add radiobutton to its button group
}
// jpanel to hold one set of radio buttons
JPanel pctPanel = new JPanel(new GridLayout(0, 1));
pctPanel.setBorder(BorderFactory
.createTitledBorder("Interest Rate Of the Loan"));
pctPanel.setBounds(175, 55, 180, 150);
panel1.add (pctPanel);
final ButtonGroup pctGroup = new ButtonGroup();
String pct[] = { "5.35", "5.5", "5.75" };
for (int i = 0; i < pct.length; i++) {
JRadioButtonWithPct radiobtn = new JRadioButtonWithPct(pct[i]);
pctPanel.add(radiobtn); // add radiobutton to its panel
pctGroup.add(radiobtn); // add radiobutton to its button group
}
final JButton calculateButton = new JButton("CALCULATE");
calculateButton.setBounds (30, 400, 120, 30);
panel1.add (calculateButton);
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double M_Interest = temp_Interest.doubleValue() /(12*100);
double Months = temp_Length * 12;
Principal = Integer.parseInt(mortgageText.getText());
double M_Payment = Principal * ( M_Interest / (1 - (Math.pow((1 + M_Interest), - Months))));
NumberFormat Money = NumberFormat.getCurrencyInstance();
/** MORE STUFF TO HAPPEN HERE */
}
});
JButton clearButton = new JButton("CLEAR");
clearButton.setBounds (160, 400, 120, 30);
panel1.add (clearButton);
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mortgageText.setText (null);
yearsGroup.clearSelection();
pctGroup.clearSelection();
}
});
JButton exitButton = new JButton("EXIT");
exitButton.setBounds (290, 400, 120, 30);
panel1.add (exitButton);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
MortgageCalculatorGUI8 frame = new MortgageCalculatorGUI8();
frame.setBounds (400, 200, 800, 800);
frame.setTitle ("Mortgage Calculator 1.0.4");
frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
frame.setVisible (true);
}
}

Categories