First off, I am a Java Noob coding in latest version of NetBeans. I am trying to create a program the will allow the user to enter two decimal values: time and earnings in a swing GUI.
Basic Flow
- The user will enter the values in two different text fields
- user will hit enter button and jTextField1 and jTextField2 will be parsed (double) and validation occurs - This is where my errors start. code compiles but generates errors on the button action
- if values are in range - they are stored in a two-dimensional array
- a Run Report button can be used to do some calculations and output is sent to a jTextArea
- Quit button exits the program
How I'm trying to code the program
I created an object> Ledger
/*
* Ledger -
* simulates a Ledger with a 2-d Array "page" to hold Doubles
* Allows you to add entries and run reports on the data
*/
package com.kenneth.test1;
/**
*
* @author Ken
*/
public class Ledger {
// set # of Columns in array - need only 2 -woud break program if changed
final private int COLUMN_MAX = 2;
// @MAX_SESSION_TIME sets the maximum amout of work-time for user
// could be changed without breaking program
final private int MAX_SESSION_TIME = 240;
// @ROW_MAX sets max size of array, could be increased without breaking program
final private int ROW_MAX = 10;
// @currentRow keeps track of how many items have been added to array
// also used for item total to calculate averages
private int currentRow;
// @ledger initialize array
private double[][] ledgerPage;
// stores numerical data entered into the earnings field
private double sessionEarnings;
// stores numerical data entered into the time field
private double sessionTime;
// string to hold output messages
private String verify;
// **Constructor**\\
public Ledger() {
double[][] ledgerPage1 = new double[ROW_MAX][COLUMN_MAX];
// @currentRow keeps track of how many items have been added to array
// also used for item total to calculate averages
// stores numerical data entered into the time field
sessionTime = 0.0;
// stores numerical data entered into the earnings field
sessionEarnings = 0.0;
// string to hold output messages
verify = " ";
}
/**
* @return the ROW_MAX
*/
public int getROW_MAX() {
return ROW_MAX;
}
/**
* @return the COLUMN_MAX
*/
public int getCOLUMN_MAX() {
return COLUMN_MAX;
}
/**
* @return the ledger
*/
public double[][] getLedgerPage() {
return ledgerPage;
}
/**
* @param ledger the ledger to set
*/
public void setLedgerPage(double[][] x) {
this.ledgerPage = x;
}
/**
* @return the currentRow
*/
public int getCurrentRow() {
return currentRow;
}
/**
* @param currentRow the currentRow to set
*/
public void setCurrentRow(int currentRow) {
this.currentRow = currentRow;
}
/**
* @return the sessionTime
*/
public double getSessionTime() {
return sessionTime;
}
/**
* @param sessionTime the sessionTime to set
*/
public void setSessionTime(double sessionTime) {
this.sessionTime = sessionTime;
}
/**
* @return the MAX_SESSION_TIME
*/
public int getMAX_SESSION_TIME() {
return MAX_SESSION_TIME;
}
/**
* @return the sessionEarnings
*/
public double getSessionEarnings() {
return sessionEarnings;
}
/**
* @param sessionEarnings the sessionEarnings to set
*/
public void setSessionEarnings(double sessionEarnings) {
this.sessionEarnings = sessionEarnings;
}
/**
* @return the verify
*/
public String getVerify() {
return verify;
}
/**
* @param verify the verify to set
*/
public void setVerify(String verify) {
this.verify = verify;
}
public void recordEntry(double[][] ledgerPage, int index1, int index2, double value1) {
this.ledgerPage[index1][index2] = value1;
}
public double readEntry(double[][] ledgerPage, int[][] index) {
return sessionTime;
}
}
I created the Gui via NetBeans visual editor and dragged my Ledger.java object on top of it to link them as stated on Oracle's website.
/*
* Ken
* TutorTracker.java
* program that take time and earnings data and print reports
* Created on Sep 28, 2011, 1:27:19 PM
*/
package com.kenneth.test1;
public class TutorTrackerMain extends javax.swing.JFrame {
/** Creates new form TutorTrackerMain */
public TutorTrackerMain() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ledger1 = new weigand.kenneth.task1.Ledger();
ledger2 = new weigand.kenneth.task1.Ledger();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("TutorTracker");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add Entry", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 10))); // NOI18N
jLabel1.setText("Time:");
jLabel2.setText("Earnings:");
jButton1.setText("Enter");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Reports", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 10))); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
.addContainerGap())
);
jButton2.setText("Run Report");
jButton3.setText("Quit");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(214, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addGap(6, 6, 6))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton2, jButton3});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton2)))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// try-catch to throw exception on non-number data
try{
// parse data to store in array
ledger1.setSessionTime(Double.parseDouble(jTextField1.getText()));
ledger1.setSessionEarnings(Double.parseDouble(jTextField2.getText()));
//while there is room in the array - accept input
if(ledger1.getCurrentRow() < ledger1.getLedgerPage().length){
// validate data MAX_SESSION_TIME
if ((ledger1.getSessionTime() > 0 & ledger1.getSessionTime() <= ledger1.getMAX_SESSION_TIME()) & (ledger1.getSessionEarnings() > 0)){
// if values are valid, then add to them to the array
ledger1.recordEntry(ledger1.getLedgerPage(), ledger1.getCurrentRow(), 0, ledger1.getSessionTime());
ledger1.recordEntry(ledger1.getLedgerPage(), ledger1.getCurrentRow(), 1, ledger1.getSessionEarnings());
//incr row
ledger1.setCurrentRow((ledger1.getCurrentRow()) + 1);
ledger1.setVerify(String.format(" Time: %1.0f Eanings: $%1.2f stored.\n",ledger1.getSessionTime(), ledger1.getSessionEarnings()));
}
else{
//else display message to user to correct data
ledger1.setVerify("Please enter positve decimal numbers."
+ "\nSession Time in minutes must be between 1-240 \n ");
}
}
else{
//else display message to user to correct data
ledger1.setVerify("Array is full"
+ "\nRun a report or quit.\n ");
}
// send varifcation message to user
jTextArea1.append(ledger1.getVerify());
}
catch (NumberFormatException e){
jTextArea1.append("***Invalid Entry***\nPlease enter positve decimal numbers."
+ "\nSession Time in minutes must be between 1-240 \n ");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TutorTrackerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TutorTrackerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TutorTrackerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TutorTrackerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TutorTrackerMain().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private weigand.kenneth.task1.Ledger ledger1;
private weigand.kenneth.task1.Ledger ledger2;
// End of variables declaration
}
The red portion is my algorithm. The code compiles, but errors out when the Enter button is pushed. I have commented out the nested loops to see if it was my validation loop only but the errors are coming up on all loops.
That makes me suspect that I am not passing values correctly.
The GUI has created a new Ledger object, I am able to pull up all Ledger methods in my Gui Object.
I can't see the 2-d Array from the object though.
I hope that there is something simple that I'm missing and would be very thankful if some kind soul could give me direction.
Thanks!