9 Replies - 729 Views - Last Post: 09 August 2014 - 05:07 PM Rate Topic: -----

#1 speedy8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 14

2D Array in NetBeans GUI

Posted 09 August 2014 - 02:50 PM

I am trying to create a program in NetBeans GUI builder and I am having trouble creating the 2D array. Here is what I have come up with so far:


private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        double time = 0;
        try{
            time = Double.parseDouble(timeText.getText());
            if (time <= 0 || time > 240){
                throw new Exception();
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid Input. Minutes must be a number greater than 0 and no more than 240.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        double payment = 0;
        try{
            payment = Double.parseDouble(paymentText.getText());
            if (payment <= 0) {
                throw new Exception();
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid Input. Payment must be a number greater than 0.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
    }
        {
            tutorArray[i][0] = time;
            tutorArray[i][1] = payment;
            for (int j = 0; i < payment; j++) {
                String line = String.format("%s\n", tutorArray[i][j]);
                textArea.append(line);
            }
        }
        {
            timeText.setText("");
            paymentText.setText("");
        }
        
    } 

..................right now it is printing what i need but I need to separate columns, not rows. Any help would be appreciated.

This post has been edited by andrewsw: 09 August 2014 - 02:56 PM
Reason for edit:: Attempt to use CODE tags


Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array in NetBeans GUI

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 02:56 PM

Moved to Java forum; Java and Javascript are two completely different languages.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10777
  • View blog
  • Posts: 40,134
  • Joined: 27-December 08

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 03:08 PM

You are aware the \n escape sequence says to go to a new line, right? If you want columns instead of rows, I'd start by removing the \n escape sequence. The \t escape sequence will insert a tab. It might be closer to what you want. Or alternatively, use a JTable, which was designed for this.
Was This Post Helpful? 1
  • +
  • -

#4 speedy8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 14

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 03:44 PM

thanks! The worst part is I read that earlier but it didn't register.

For every entry it should be putting my entries into a new row but mine are doing new columns. I've declared the 2D array and now I have the space needed. Now if I can get it to store the entries correctly so I can start on a new button(report button)
Was This Post Helpful? 0
  • +
  • -

#5 speedy8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 14

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 04:20 PM


            tutorArray[i][0] = time;
            tutorArray[i][1] = payment;
            for (int j = 0; i < payment; j++) {
                String line = String.format("%s\t", tutorArray[i][j]);
                textArea.append(line);
                timeText.setText("");
            paymentText.setText("");



This is the code for the 2D array. It posts into the array and clears the text from the text fields. I want it to start a new row after it posts the code but if i use the \n after the \t it overrides the \t. That is the only place I know I would be able to declare the new line method.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10777
  • View blog
  • Posts: 40,134
  • Joined: 27-December 08

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 04:23 PM

You could append line + "\n" to start a new row.

Also- just a heads up. Using a 2D array to manage parallel arrays is a bad idea. Design a Tutor class encapsulating the fields time and payment. Then store a single Tutor[]. More on OOP here.
Was This Post Helpful? 0
  • +
  • -

#7 speedy8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 14

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 04:54 PM

unfortunately i am stuck to a rubric for school, so I am required to use GUI 2D array. I used \n on the append statement and it overrides my previous statement for \t
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10777
  • View blog
  • Posts: 40,134
  • Joined: 27-December 08

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 04:57 PM

What is the exact output it is giving you? What is the desired output?
Was This Post Helpful? 0
  • +
  • -

#9 speedy8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-August 14

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 05:04 PM

example output of what i'm getting with 3 entries:
20.0 30.0 35.0 30.0 25.0 20.0

what I'm wanting:
20.0 30.0
35.0 30.0
25.0 20.0


if that makes sense.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10777
  • View blog
  • Posts: 40,134
  • Joined: 27-December 08

Re: 2D Array in NetBeans GUI

Posted 09 August 2014 - 05:07 PM

Then simply:
String line = String.format("%s\t %s\n", tutorArray[i][0], tutorArray[i][1]);



That should do the trick.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1