Results 1 to 2 of 2

Thread: Formatting 2d array in a textarea.

  1. #1
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Formatting 2d array in a textarea.

    This is a project for class, I have to do it their particular way, which requires taking a couple textfields for input, adding the entries to a 2d array, and displaying the results in a textArea for each submitted record. Then perform various calculations on the array data and display the results in the textarea.

    My problem is getting it to display cleanly in the textArea. Since I'm just concatenating spaces in between two variables on the same line, when the first variable has a different number of characters from other rows it looks terrible. I need to have like a column offset for the second column of data rather than my hacked up appending " " spaces.

    Code:
     
    public class myClass extends javax.swing.JFrame {
     double tutorMinutes;
            double tutorPay;
            double [][] tutorArray = new double[1][2];  
            double defaultAmt = 0.00;
    
     public myClassStart() {
            initComponents();
            
            //Initialize the Text Area headings
            jTextArea2.append("Minutes: ");
            jTextArea2.append("         ");
            jTextArea2.append("Payment: ");
            jTextArea2.append("\n");
        }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            //ENTER BUTTON PRESSED
    
            for(int i = 0; i < tutorArray.length; i++){  
                    for(int j = 0; j < tutorArray[i].length; j++){  
    
                     //Assign input from the text fields to variables
                     tutorMinutes = Double.parseDouble(jTextField1.getText());  
                     tutorPay= Double.parseDouble(jTextField2.getText());
                          
                        //Assign the variables to indexes in the array
                        tutorArray[j][0] = tutorMinutes;  
                        tutorArray[j][1] = tutorPay;   
    
                        //Reset text entry fields
                        jTextField1.setText(String.valueOf(defaultAmt));  
                        jTextField2.setText(String.valueOf(defaultAmt)); 
                        
                        //Append the new entry to the textArea 
                        jTextArea2.append(String.valueOf(tutorArray[j][0]));              
                        jTextArea2.append("               "); //spacing I want
                        jTextArea2.append(String.valueOf(tutorArray[j][1]));  
                        jTextArea2.append("\n");
                    }                                         
            }       
        }
    }
    Any ideas? Thanks.

  2. #2
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    In this loop :
    for (int i =0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
    matrix_tf.setText(String.valueOf(matrix[i][j]));

    you set the text of your field on each iteration (setting the text overwrites the prvioues one). Try concatenating your texts :
    for (int i =0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
    matrix_tf.setText(matrix_tf.getText() + " " + String.valueOf(matrix[i][j]));

    If you are using a text area (and not a text field) use append as @Sujay suggested

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •