13 Replies Latest reply: May 19, 2008 10:16 AM by 3004 RSS

    adding elements to arrays

    807601
      Alright I can't seem to get this array and button event to work properly, i want to add the element to the array that is already preset, and when the summary button is pressed to display the updated array information. Problems being: Summary button doesnt display 'added parts' after you add the parts to the array, and I want to change null to "N/A" but nothing seems to work very well.

      Any help is much appreciated.

      Presentation Class:
       
       
      
      import javax.swing.*;
      import java.awt.Color;
      import java.awt.Container;
      import java.awt.FlowLayout;
      import java.awt.event.*;
      
      public class partsPresentation extends JFrame implements ActionListener {
      
           private static final int FRAME_WIDTH = 375;
           private static final int FRAME_HEIGHT = 600;
           private static final int FRAME_X_ORIGIN = 150;
           private static final int FRAME_Y_ORIGIN = 250;
           private static final int BUTTON_WIDTH = 80;
           private static final int BUTTON_HEIGHT = 30;
           JLabel partAddLabel;
           JButton summaryButton;
           JTextField pwrapField;
           JTextField brandAField;
           JTextField brandBField;
           JTextField brandXField;
           JLabel columnsLabel;
           JButton addButton;
           private JTextArea textArea;
           private JScrollPane textBoxScroll;
           private String WHITESPACE = "        ";
      
           partsSummary partsList[] = new partsSummary[9];
           partsSummary partsSum;
      
           public static void main(String[] args) {
      
                partsPresentation frame = new partsPresentation();
                frame.setVisible(true);
      
           }
      
           public partsPresentation() {
      
                Container contentPane;
      
                setSize(FRAME_WIDTH, FRAME_HEIGHT);
                setResizable(false);
                setTitle("Ch10 Project");
                setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
      
                contentPane = getContentPane();
                contentPane.setLayout(new FlowLayout());
      
                columnsLabel = new JLabel();
                columnsLabel
                          .setText("                PWrap - Brand A - Brand B - Brand X                       ");
                columnsLabel.setSize(150, 25);
                contentPane.add(columnsLabel);
      
                partAddLabel = new JLabel();
                partAddLabel.setText("Add Part: ");
                partAddLabel.setSize(150, 25);
                contentPane.add(partAddLabel);
      
                pwrapField = new JTextField();
                pwrapField.setColumns(6);
                contentPane.add(pwrapField);
      
                brandAField = new JTextField();
                brandAField.setColumns(4);
                contentPane.add(brandAField);
      
                brandBField = new JTextField();
                brandBField.setColumns(4);
                contentPane.add(brandBField);
      
                brandXField = new JTextField();
                brandXField.setColumns(4);
                contentPane.add(brandXField);
      
                summaryButton = new JButton("Summary");
                summaryButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
                contentPane.add(summaryButton);
      
                addButton = new JButton("Add Part");
                addButton.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
                contentPane.add(addButton);
      
                textArea = new JTextArea();
                textArea.setColumns(30);
                textArea.setRows(20);
                textArea.setLineWrap(true);
                textBoxScroll = new JScrollPane(textArea);
                textBoxScroll
                          .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                textArea.setBorder(BorderFactory.createLineBorder(Color.RED));
      
                // Set to non edit
                textArea.setEditable(false);
                contentPane.add(textBoxScroll);
      
                pwrapField.addActionListener(this);
                brandAField.addActionListener(this);
                brandBField.addActionListener(this);
                brandXField.addActionListener(this);
                summaryButton.addActionListener(this);
                addButton.addActionListener(this);
      
                setDefaultCloseOperation(EXIT_ON_CLOSE);
      
           }
      
           public void assignParts() {
                
                for (int i = 0; i < partsList.length; i++) {
                     partsList[i] = new partsSummary();
                }
      
                
                partsList[0].setValues("PR001", "A001", "B001", "X001");
                partsList[1].setValues("PR002", "A002", "B002", "X002");
                partsList[2].setValues("PR003", "A003", "B003", "X003");
      
                
           }
      
           public void actionPerformed(ActionEvent event) {
                
      
      
                String PWrapStr = pwrapField.getText();
                String brandAStr = brandAField.getText();
                String brandBStr = brandBField.getText();
                String brandXStr = brandXField.getText();
                
                //start on the 3rd index of the array
                int partsListIndex = 3;
      
                if (event.getSource() instanceof JButton) {
                     JButton clickedButton = (JButton) event.getSource();
      
                     if (clickedButton == addButton) {
                     
                          partsList[partsListIndex].setValues(PWrapStr, brandAStr,
                                    brandBStr, brandXStr);
                          partsListIndex += 1;
                          
      
                          JOptionPane.showMessageDialog(null, "Part Added!");
      
                     }
      
                     if (clickedButton == summaryButton) {
      
                          
                          int indexInteger = 0;
                          boolean foundBoolean = true;
      
                          // This seems to be a key element?
                          assignParts();
                          
                          //With it, I can click summary and it will output the
                          //preset array values. However, when i remove it and hit the
                          //summary button, runtime error ect. BUT! if I 'add' a part
                          //using the add button, and THEN click summary, it works 
                          //like it should, but it doesnt have the preset array values.
      
                          while (indexInteger < partsList.length && foundBoolean != false) {
      
                               for (int i = 0; i < partsList.length; i++) {
      
                                    textArea.append(partsList.getPW() + WHITESPACE
                                              + partsList[i].getBrandA() + WHITESPACE
                                              + partsList[i].getBrandB() + WHITESPACE
                                              + partsList[i].getBrandX() + "\n");

                                    indexInteger++;
                               }

                               if (indexInteger == partsList.length) {
                                    foundBoolean = false;
                               }
                          }

                     }

                }
           }
      }
      2nd Class:
      public class partsSummary {

           private String plainWrapString;
           private String brandAString;
           private String brandBString;
           private String brandXString;
           
           partsSummary partsList[];

           public void setValues(String plainWrap, String brandA, String brandB, String brandX)
           {
                if (plainWrap != null) {
                     plainWrapString = plainWrap;
                     
                }
                else
                {
                     plainWrapString = "N/A";
                }
                
                if (brandA.equals(null)) {
                     brandAString = "N/A";
                }
                else
                if (brandB.equals(null)) {
                     brandBString = "N/A";
                }
                else
                if (brandX.equals(null)) {
                     brandXString = "N/A";
                }
                
                
                brandAString = brandA;
                brandBString = brandB;
                brandXString = brandX;
                
           
                
                
           }
           
           public void getInitialArray() {
                          
                partsList[0].setValues("PR001", "A001", "B001", "X001");
                partsList[1].setValues("PR002", "A002", "B002", "X002");
                partsList[2].setValues("PR003", "A003", "B003", "X003");
                
           }
           
           
           
           public String getPW() {
                
                
                return plainWrapString;
           }
           public String getBrandA() {
                return brandAString;
           }
           public String getBrandB() {
                return brandBString;
           }
           public String getBrandX() {
                return brandXString;
           }
           
      }
      Thanks,
      
      - GoOsE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: adding elements to arrays
          807601
          Questions Being:

          1.How do I update my array so that when I press add, then summary...it will display the new information along with the preset array elements?

          2.How do I get the null values to translate into "N/A" when listed in the TextArea?

          - GoOsE
          • 2. Re: adding elements to arrays
            3004
            Jononomous wrote:
            Questions Being:

            1.How do I update my array so that when I press add, then summary...it will display the new information along with the preset array elements?
            You need to provide an SSCCE. If your question is about arrays, then you should just provide array code and no GUI stuff. If your question is about how to get a button press to update data somewhere, then you should just provide just enough GUI code to show where your "respond to button press" logic lies and not worry about whether the data is going to an array or elsewhere.

            I have no idea what you mean by "display the new information along with the preset array elements."
            2.How do I get the null values to translate into "N/A" when listed in the TextArea?
            At some point--when setting or getting or displaying something, whichever is appropriate for your specific case, you'd do something like:
            if (s1 != null) {
              s2 = s1;
            }
            else {
              s2 = "N/A";
            }
            
            
            // OR
            
            s2 = (s1 != null) ? s1 : "N/A";
            • 3. Re: adding elements to arrays
              807601
              alright basically stated - I'm trying to add elements to an existing array. When the 'Summary' button is clicked, display this array. The problem I've run into is that sure I can preset the array values, and display the summary of them just fine, however when I add a value, and click summary again, the output doesn't change, IE: no new array elements appear in the output.

              Array:
              partsSummary partsList[] = new partsSummary[9];
              
              public void assignParts() {
                        
                        for (int i = 0; i < partsList.length; i++) {
                             partsList[i] = new partsSummary();
                        }
               
                        
                        partsList[0].setValues("PR001", "A001", "B001", "X001");
                        partsList[1].setValues("PR002", "A002", "B002", "X002");
                        partsList[2].setValues("PR003", "A003", "B003", "X003");
               
                        
                   }
              Here is the add and summarize button events.
               
                   public void actionPerformed(ActionEvent event) {
                        
               
               
                        String PWrapStr = pwrapField.getText();
                        String brandAStr = brandAField.getText();
                        String brandBStr = brandBField.getText();
                        String brandXStr = brandXField.getText();
                        
                        //start on the 3rd index of the array
                        int partsListIndex = 3;
               
                        if (event.getSource() instanceof JButton) {
                             JButton clickedButton = (JButton) event.getSource();
               
                             if (clickedButton == addButton) {
                             
                                  partsList[partsListIndex].setValues(PWrapStr, brandAStr,
                                            brandBStr, brandXStr);
                                  partsListIndex += 1;
                                  
               
                                  JOptionPane.showMessageDialog(null, "Part Added!");
               
                             }
               
                             if (clickedButton == summaryButton) {
               
                                  
                                  int indexInteger = 0;
                                  boolean foundBoolean = true;
               
                                  // This seems to be a key element?
                                  assignParts();
                                  
                                  //With it, I can click summary and it will output the
                                  //preset array values. However, when i remove it and hit the
                                  //summary button, runtime error ect. BUT! if I 'add' a part
                                  //using the add button, and THEN click summary, it works 
                                  //like it should, but it doesnt have the preset array values.
               
                                  while (indexInteger < partsList.length && foundBoolean != false) {
               
                                       for (int i = 0; i < partsList.length; i++) {
               
                                            textArea.append(partsList.getPW() + WHITESPACE
                                                      + partsList[i].getBrandA() + WHITESPACE
                                                      + partsList[i].getBrandB() + WHITESPACE
                                                      + partsList[i].getBrandX() + "\n");

                                            indexInteger++;
                                       }

                                       if (indexInteger == partsList.length) {
                                            foundBoolean = false;
                                       }
                                  }

                             }

                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
              • 4. Re: adding elements to arrays
                797060
                Are you trying at add an element, or replace an element? You cannot add an element to an array, they are predefined in length. You can create a new array larger and assign all the old values to it and the extra one, but then you might as well use arrayLists, which are much better.

                Also, you are trying to display the array when you click the summaryButton yes?
                if (clickedButton == summaryButton) {
                 
                                    
                                    int indexInteger = 0;
                                    boolean foundBoolean = true;
                 
                                    // This seems to be a key element?
                                    assignParts();
                                    
                see here you are calling the assignParts() method, which just makes the parts:
                          partsList[0].setValues("PR001", "A001", "B001", "X001");
                          partsList[1].setValues("PR002", "A002", "B002", "X002");
                          partsList[2].setValues("PR003", "A003", "B003", "X003");
                so whatever you do to it, you are setting it to these presets when you want to display it? i take it you dont want to do this.... might be your problem?
                • 5. Re: adding elements to arrays
                  797060
                  i see your problem i think. You need to assign parts in your innitialisation, only once, and then change it using addparts, otherwise your array will be null and you will get an error, unless you add something using addParts()
                  • 6. Re: adding elements to arrays
                    3004
                    Jononomous wrote:
                    alright basically stated - I'm trying to add elements to an existing array.
                    You can't add elements. An array's size is fixed at creation. Perhaps you want to set values for elements that still have their default initial values (0/false/null), or perhaps you want to create a new array that's bigger than the old, and copy over the values from the old array. If the latter, you probably want to use ArrayList, not an array.

                    When the 'Summary' button is clicked, display this array. The problem I've run into is that sure I can preset the array values, and display the summary of them just fine, however when I add a value, and click summary again, the output doesn't change, IE: no new array elements appear in the output.
                    • 7. Re: adding elements to arrays
                      807601
                      Ah I understand now, I've been asking the wrong question it would appear, so now it would come down to, how do i replace array values then. And then when summary is clicked, display those initial, and replaced values?

                      My apologies for the confusion!

                      - GoOsE
                      • 8. Re: adding elements to arrays
                        807601
                        you said...
                        display those initial, and replaced values

                        you need two arrays to be that!

                        When you replace a value in an array the computer doesn't just hold the old value in some magic location where it can be called again, it disappears unless you allocate it to some other array or variable.
                        • 9. Re: adding elements to arrays
                          3004
                          Jononomous wrote:
                          Ah I understand now, I've been asking the wrong question it would appear, so now it would come down to, how do i replace array values then.
                          arr[index] = newValue;
                          And then when summary is clicked, display those initial, and replaced values?
                          You'll need a separate array where you explicitly save off the old value before overwriting it.
                          • 10. Re: adding elements to arrays
                            807601
                            Super Confused

                            But if I cant add elements to an array how would I put the old values into a new array!?

                            :(


                            Thanks for the help btw...lol
                            • 11. Re: adding elements to arrays
                              797060
                              well, you know how to cycle through arrays, you know how to make an new array, you know how to assign a value from one array to another right? put these together, post what you think you should do and why you think it doesnt work
                              • 12. Re: adding elements to arrays
                                797060
                                if "put the old values into a new array" is your problem, i advise you read the [tutorials on arrays|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html] and this will tell you.

                                basically

                                array[index]=value;

                                done, but read the tutorial anyway
                                • 13. Re: adding elements to arrays
                                  3004
                                  Jononomous wrote:
                                  Super Confused

                                  But if I cant add elements to an array how would I put the old values into a new array!?
                                  The same way you put them into the first array--by changing the values of the existing elements.

                                  "Can't add elements" means you can't change an array's size. You can't go from
                                  ["abc", "def"]
                                  to
                                  ["abc", "def", "ghi", "jlk"]
                                  without creatin a new array.

                                  However, if, at the time you create your original array, you also create one of the same size to hold the previous values, then, before you change the value in the "current" array, you copy the current value into the "previous" array.