Results 1 to 2 of 2
  1. #1
    Jononomous is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Unhappy replacing array values

    Alright So....I'm trying to replace the array values when I hit my 'add' button, and then when i hit the summary button it displays the initial array values, the ones i preloaded in, and the new, updated ones. Any help here is much appreciated, been workin on it for days :(



    Questions being:

    1. How do I achieve the proper effects with my add and summary buttons, right now it will display the initial values but not the ones I add in using my add button.

    2. How do I make it so that when it appends null to my textArea to actually make it append "N/A" instead, current method isnt working properly

    Code:

    Java Code:
    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 newArray[];
    
        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[i].getPW() + WHITESPACE
                                    + partsList[i].getBrandA() + WHITESPACE
                                    + partsList[i].getBrandB() + WHITESPACE
                                    + partsList[i].getBrandX() + "\n");
    
                            indexInteger+=1;
                        }
    
                        if (indexInteger == partsList.length) {
                            foundBoolean = false;
                        }
                    }
    
                }
    
            }
        }
    }
    =====================
    MY OTHER CLASS!!
    =====================

    Java Code:
    public class partsSummary {
    
        private String plainWrapString;
        private String brandAString;
        private String brandBString;
        private String brandXString;
    
        
    
        public void setValues(String plainWrap, String brandA, String brandB,
                String brandX) {
            if (plainWrap != null) {
                plainWrapString = plainWrap;
    
            } else {
                plainWrapString = "N/A";
            }
    
            if (brandA != null) {
                brandAString = brandA;
            }
    
            else {
                brandAString = "N/A";
            }
    
            if (brandB != null) {
                brandBString = brandB;
            } else {
                brandBString = "N/A";
            }
    
            if (brandX != null) {
                brandXString = brandX;
            } else {
                brandXString = "N/A";
            }
    
        }
    
        public String getPW() {
    
            return plainWrapString;
        }
    
        public String getBrandA() {
            return brandAString;
        }
    
        public String getBrandB() {
            return brandBString;
        }
    
        public String getBrandX() {
            return brandXString;
        }
    
    }
    Thanks again,

    - Jononomous
    Last edited by Jononomous; 05-19-2008 at 06:36 AM.

  2. #2
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default

    What I discovered is a null pointer exception when I hit the add button. YOu are not assigning values to your array until the summary button is clicked.
    Java Code:
     if (clickedButton == summaryButton) {
    
                    
                    int indexInteger = 0;
                    boolean foundBoolean = true;
                    // This seems to be a key element?
                    assignParts();
    You probably want to initialize your array prior to this.

Similar Threads

  1. 3d array filled with garbage values
    By jon80 in forum New To Java
    Replies: 5
    Last Post: 01-01-2009, 10:12 PM
  2. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  3. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM
  4. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 06:33 AM
  5. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 03:08 PM

Posting Permissions

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