Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Printing Output from an array in a gui form.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Printing Output from an array in a gui form.

    I'm using Netbeans to creat a Java Swing Gui form. I'm stuck creating output in an array that will print out in a text area box.

     
           double tutorArray [] [] = new double [4][2];
           int counter;
           int i  = 0;
           int j = 0;

    The user inputs two values at a time and then presses the enter button:

     
     double salary = Double.parseDouble (salaryTextField.getText ());
           double week = Double.parseDouble (wagesTextField.getText ());
     
           try
         {
             {if (minutes <= 0 || minutes > 4500|| wages <= 4) 
              throw new IllegalArgumentException ();
              }
        }                                           
          catch (IllegalArgumentException exception)
          {
           dataTextArea.setText("Invalid input.  Please try again.");
           return;
          }
     
            dataTextArea.setText("");
            tutorArray [counter] [0] = salary;
            tutorArray [counter] [1] = weeks;
            counter++;
             for (int i = 0; i <= 4 ; i++){
              for (int j = 0; j <= 2; j++) {
                 String line = String.format ("%2d", tutorArray[i][j]);
                 dataTextArea.append (line);
              counter++;     
              }
           }

    3. Once the user finishes entering his information, a 2d Array should be generated when he presses the "run report" button:

     private void runReportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
            // TODO add your handling code here:
        }

    I have tried multiple times to write the code for a 2d array that has 4 rows and two columns containing the information entered by the users - minutes and wages.

    Any pointers?
    Last edited by Norm; December 28th, 2012 at 07:47. Reason: Removed = Java from code tags


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Printing Output from an array in a gui form.

    What is in the array after the code assigns values to it?
    Use the Arrays class's deepToString() method to format the array for printing:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));

    Can you explain what the code does and what it is not doing that you want it to do?
    Obviously this needs to be replaced with some code:
    // TODO add your handling code here:

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing Output from an array in a gui form.

    The purpose of the code is to receive input from a user who enters a series of two values. Value 1 is the minutes spent tutoring. Value 2 is the wages earned. For example:
    Please enter the minutes spent in a tutoring session: 50
    Please enter the wages in dollars and cents earned for the session: 17.90

    The user does this with the following exceptions: Minutes spent tutoring can't exceed a daily limit of 240 minutes and Wages can't be less than 0.

    Since this is a Gui form, I can't use the System.out.println method. The output is being generated to a text area in the form of array when the report button is pressed AFTER the user finish entering in the minutes and wages for each session. The array should look like this:

    Minutes Wages
    70 12.95
    50 15.00
    120 30.00

    I'm having problems doing the code for an enhanced for loop to use with the array and then code the output so it will print as above in a text area field for a Swing Java GUI form.

    [=Java]


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package my.moneymade;

    /**
    *
    * @author alan
    */
    public class MoneyMadeUI extends javax.swing.JFrame {

    /**
    * Creates new form MoneyMadeUI
    */
    double tutorArray [] [] = new double [4][2];
    int counter;
    int i = 0;
    int j = 0;
    public MoneyMadeUI() {
    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() {

    jPanel1 = new javax.swing.JPanel();
    tutoringWagesLabel = new javax.swing.JLabel();
    tutoringMinutesLabel = new javax.swing.JLabel();
    paymentLabel = new javax.swing.JLabel();
    enterButton = new javax.swing.JButton();
    minutesTextField = new javax.swing.JTextField();
    wagesTextField = new javax.swing.JTextField();
    runReportButton = new javax.swing.JButton();
    quitButton = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    dataTextArea = new javax.swing.JTextArea();
    clearButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    tutoringWagesLabel.setText("Tutoring Wages Report");

    tutoringMinutesLabel.setText("Tutoring Time in Minutes:");

    paymentLabel.setText("Payment for sessions in dollars and cents:");

    enterButton.setText("Enter");
    enterButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    enterButtonActionPerformed(evt);
    }
    });

    minutesTextField.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    minutesTextFieldActionPerformed(evt);
    }
    });

    wagesTextField.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    wagesTextFieldActionPerformed(evt);
    }
    });

    runReportButton.setText("Run Report");
    runReportButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    runReportButtonActionPerformed(evt);
    }
    });

    quitButton.setText("Quit");
    quitButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    quitButtonActionPerformed(evt);
    }
    });

    dataTextArea.setColumns(20);
    dataTextArea.setRows(5);
    jScrollPane1.setViewportView(dataTextArea);

    clearButton.setText("Clear");
    clearButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    clearButtonActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addComponent(tutoringWagesLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addGap(63, 63, 63))
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(paymentLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(tutoringMinutesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(34, 34, 34)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(minutesTextField)
    .addComponent(wagesTextField)))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(26, 26, 26)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(quitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(runReportButton, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
    .addComponent(enterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
    .addGap(160, 160, 160))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addComponent(tutoringWagesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(tutoringMinutesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(minutesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(wagesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(paymentLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(3, 3, 3)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(26, 26, 26)
    .addComponent(enterButton)
    .addGap(12, 12, 12)
    .addComponent(clearButton)
    .addGap(13, 13, 13)
    .addComponent(runReportButton)
    .addGap(18, 18, 18)
    .addComponent(quitButton))
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(0, 64, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

    private void minutesTextFieldActionPerformed(java.awt.event.Act ionEvent evt) {
    // TODO add your handling code here:
    }

    private void wagesTextFieldActionPerformed(java.awt.event.Actio nEvent evt) {
    // TODO add your handling code here:
    }

    private void enterButtonActionPerformed(java.awt.event.ActionEv ent evt) {
    // TODO add your handling code here:
    double minutes = Double.parseDouble (minutesTextField.getText ());
    double wages = Double.parseDouble (wagesTextField.getText ());

    try
    {
    {if (minutes <= 0 || minutes > 240 || wages <= 0)
    throw new IllegalArgumentException ();
    }
    }
    catch (IllegalArgumentException exception)
    {
    dataTextArea.setText("Invalid input. Please try again.");
    return;
    }

    dataTextArea.setText("");
    tutorArray [counter] [0] = minutes;
    tutorArray [counter] [1] = wages;
    counter++;
    for (int i = 0; i <= 4 ; i++){
    for (int j = 0; j <= 2; j++) {
    String line = String.format ("%2d", tutorArray[i][j]);
    dataTextArea.append (line);
    counter++;
    }
    }


    }

    private void clearButtonActionPerformed(java.awt.event.ActionEv ent evt) {
    // TODO add your handling code here:
    minutesTextField.setText("");
    wagesTextField.setText ("");
    //dataTextArea.setText ("");
    }

    private void runReportButtonActionPerformed(java.awt.event.Acti onEvent evt) {
    // TODO add your handling code here:
    }

    private void quitButtonActionPerformed(java.awt.event.ActionEve nt evt) {
    // TODO add your handling code here:
    }

    /**
    * @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 How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(MoneyMadeUI.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(MoneyMadeUI.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(MoneyMadeUI.cla ss.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(MoneyMadeUI.cla ss.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 MoneyMadeUI().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton clearButton;
    private javax.swing.JTextArea dataTextArea;
    private javax.swing.JButton enterButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField minutesTextField;
    private javax.swing.JLabel paymentLabel;
    private javax.swing.JButton quitButton;
    private javax.swing.JButton runReportButton;
    private javax.swing.JLabel tutoringMinutesLabel;
    private javax.swing.JLabel tutoringWagesLabel;
    private javax.swing.JTextField wagesTextField;
    // End of variables declaration
    }

    [/Java]

Similar Threads

  1. help with printing output in columns
    By jblankinship in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2012, 13:03
  2. Problem with running a GUI Form
    By Pane87 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 4th, 2012, 12:48
  3. Problem with running a GUI form
    By Pane87 in forum AWT / Java Swing
    Replies: 0
    Last Post: August 4th, 2012, 06:03
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41