Hello All,

I am student just starting Java. I do need help with swing - GUI. I am creating 4 textfield that accepts input from 3 user and will display as a 2D Array in the text area. As well as calculate the average of one column from the input.

I have done some coding with the add button. Need help with the remaining part.

float breakFast= Float.parseFloat(jTextField1.getText());
float lunch = Float.parseFloat(jTextField2.getText());
float dinner = Float.parseFloat(jTextField3.getText());
float dessert = Float.parseFloat(jTextField4.getText());

jTextArea1.setText(String.format("Amount spent on breakfast: %3.2f\n",breakFast));
jTextArea1.append(String.format("Amount spent on lunch : %3.2f\n",lunch));
jTextArea1.append(String.format("Amount spent on dinner: %3.2f\n",dinner));
jTextArea1.append(String.format("Amount spent on dessert: %3.2f\n",dessert));

Can you describe what your problems are?
If you are getting errors, please copy the full text and paste here.

public class MealRep1 extends javax.swing.JFrame {

    /** Creates new form MealRep1 */
    public MealRep1() {
        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() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        jLabel1.setText("Breakfast");

        jLabel2.setText("Lunch");

        jLabel3.setText("Dinner");

        jLabel4.setText("Dessert");

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

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

        jButton2.setText("Close");

        jButton3.setText("Result");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1)
                        .addComponent(jLabel4)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                            .addComponent(jTextField2)
                            .addComponent(jTextField3)
                            .addComponent(jTextField4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 74, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.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))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jButton3))
                        .addGap(13, 13, 13))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                        .addGap(49, 49, 49)))
                .addGap(56, 56, 56))
        );

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

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

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

float breakFast= Float.parseFloat(jTextField1.getText());
float lunch = Float.parseFloat(jTextField2.getText());
float dinner = Float.parseFloat(jTextField3.getText());
float dessert = Float.parseFloat(jTextField4.getText());

jTextArea1.setText(String.format("Amount spent on breakfast: %3.2f\n",breakFast));
jTextArea1.append(String.format("Amount spent on lunch : %3.2f\n",lunch));
jTextArea1.append(String.format("Amount spent on dinner: %3.2f\n",dinner));
jTextArea1.append(String.format("Amount spent on dessert: %3.2f\n",dessert));
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MealRep1().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.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration

}

I am trying to create a 2D array to display in the textarea. The array will have input from 3 users by every click on the add button. Then I will be able to calculate the average amount spent on lunch by the 3 user by the Result button

Can you show what it is you want the textarea to contain?

Please use code tags when posting code. Use the icon above to right of text input.

This should display as 2D array in the textarea using the add button. Using whatever amount user input into the textfield

Breakfast = 5.00 Lunch = 7.50 Dinner = 8.90 Dessert = 6.50
Breakfast = 9.00 Lunch = 9.50 Dinner = 9.50 Dessert = 9.50
Breakfast = 13.00 Lunch = 5.50 Dinner = 6.90 Dessert = 4.50


(when the result button is clicked it should display this just below the array)

The average amount spent on Lunch = 7.50

What does your program put in the text area?

You need to add a newline character(\n) at the end of every line of data to put the next data on the next line

The add button on click displays a 2D array of users input into the textarea

Sample of the what is going to the displayed in the textarea is above

Thanks alot.

Have you solved your problem now?

I have not. Perhaps I should explain again.

I am creating a GUI swing with Netbean 6.9.1
The GUI has 4 label(Breakfast, Lunch, Dinner, Dessert), 4 textfield(that accepts user input- amount of the meal), 3 button(ADD, SUMMARY, and EXIT), and a textarea( that will display a 2D array of 3 user input with the click of an "ADD" button). The click of the summary button will then display the average amount spent on lunch by the 3 users just below the displayed 2D array which is already in the textarea.

I will really appreciate if someone can help asap.

Thank you all.

Can you post the code you are working with. Hard to comment on code you can't see.

My ADD button have this so far

float breakFast= Float.parseFloat(jTextField1.getText());
float lunch = Float.parseFloat(jTextField2.getText());
float dinner = Float.parseFloat(jTextField3.getText());
float dessert = Float.parseFloat(jTextField4.getText());

jTextArea1.setText(String.format("Amount spent on breakfast: %3.2f\n",breakFast));
jTextArea1.append(String.format("Amount spent on lunch : %3.2f\n",lunch));
jTextArea1.append(String.format("Amount spent on dinner: %3.2f\n",dinner));
jTextArea1.append(String.format("Amount spent on dessert: %3.2f\n",dessert));


just do not know how to code it to display a 2D array

Your posted code doesn't show a 2 dim array.
Can you show where you want each element of the 2 dim array to be shown?
For example given all these elements where you do want them to go:
array[0][0] and array[0][1] and array[0][2] ..... array[0][maxindex]
....
array[maxIndex][0] ................. array[maxIndex][maxindex]

I have been able to figure out a little of the problem. But now my array is displaying my first input only. I want to be able to enter input 3 times while the update is being added to the array. Also I want the amount entered to display in currency format (25.00). Help fix please.

Thank you.

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

/*
 * MealRep1.java
 *
 * Created on Sep 1, 2010, 10:53:40 PM
 */

package mealRep;

/**
 *
 * @author tracey
 */
public class MealRep1 extends javax.swing.JFrame {
public double[][] myArray = new double[3][3];

/** Creates new form MealRep1 */

     public MealRep1() {
        initComponents();
    }


     */
    @SuppressWarnings("unchecked")

    private void initComponents() {

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        for (int j = 0; j < myArray.length; j++) {
            for(int k = 0; k < myArray.length; k++){
        double breakfast= 0;
        double lunch=0;
        double dinner=0;

        breakfast = Double.parseDouble(jTextField1.getText());
        lunch = Double.parseDouble(jTextField2.getText());
        dinner = Double.parseDouble(jTextField3.getText());

   // Fill ArrayList stepping through it and setting all values


       myArray[j][0] = breakfast;

       myArray[j][1] = lunch;

       myArray[j][2] = dinner;

       System.out.println(myArray[j][0]);

       System.out.println(myArray[j][1]);

       System.out.println(myArray[j][2]);

       System.out.println();

        jTextArea1.append("Cost of breakfast = " + breakfast + "" + "  Cost of lunch = " + lunch + "" + "  Cost of dinner = " + dinner + "\n");
        j++;

            }

    }
    }                                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MealRep1().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.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration


}

Please surround your code with code tags. Use upper right icon.

But now my array is displaying my first input only.

Please show what the code is outputing.

What is the following statement for?

j++;
MealRep1.java
*
* Created on Sep 1, 2010, 10:53:40 PM
*/

package mealRep;

/**
*
* @author tracey
*/
public class MealRep1 extends javax.swing.JFrame {
public double[][] myArray = new double[3][3];

/** Creates new form MealRep1 */

public MealRep1() {
initComponents();
}


*/
@SuppressWarnings("unchecked")

private void initComponents() {

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

for (int j = 0; j < myArray.length; j++) {
for(int k = 0; k < myArray.length; k++){
double breakfast= 0;
double lunch=0;
double dinner=0;

breakfast = Double.parseDouble(jTextField1.getText());
lunch = Double.parseDouble(jTextField2.getText());
dinner = Double.parseDouble(jTextField3.getText());

// Fill ArrayList stepping through it and setting all values


myArray[j][0] = breakfast;

myArray[j][1] = lunch;

myArray[j][2] = dinner;

System.out.println(myArray[j][0]);

System.out.println(myArray[j][1]);

System.out.println(myArray[j][2]);

System.out.println();

jTextArea1.append("Cost of breakfast = " + breakfast + "" + " Cost of lunch = " + lunch + "" + " Cost of dinner = " + dinner + "\n");
j++;

}

}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MealRep1().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.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration

Below is the 2D display I can see in my textarea

Cost of breakfast=5.50 Cost of lunch= 9.50 Cost of dinner=5.25
Cost of breakfast=5.50 Cost of lunch= 9.50 Cost of dinner=5.25
Cost of breakfast=5.50 Cost of lunch= 9.50 Cost of dinner=5.25


I need the array to display 3 different entries from 3 users. So it has to clear the textfield in other for each user to input their prices, which then keep adding this entry to the array. I hope you understand now.

Thank you

Below is the 2D display I can see in my textarea

Ok, now show what you want to see displayed in the textarea.

So it has to clear the textfield

Read the API doc for a method that will set the TextField value to ""

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.