Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JList Update and Clear + Other Annoying Crap... Up for a challenge? Lol (Help)

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation JList Update and Clear... Up for a challenge? Lol (Help)

    [Java]annoying - Pastebin.com

    -

    I have no idea.. whats wrong with it.. It loads all records from text file into the jList1... with the ListModel.. but then when it comes to updating problems appeared.. the first one: when it adds to the ArrayList.. it goes to the first line? have no idea why even though I pass it an index - and the jList won't update or clear at all.

    Can someone please see what the hell is wrong with this code I just did? lol
    Last edited by JuLiAnc; January 13th, 2012 at 01:59 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JList Update and Clear + Other Annoying Crap... Up for a challenge? Lol (Help)

    Please watch your language JuLiAnc, and edit your post to clean it up.
    In terms of your problem, I'm having a hard time following what your issue is, so could you please post the exact error messages you receive (if any) along with the relevant code posted here.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JList Update and Clear + Other Annoying Crap... Up for a challenge? Lol (Help)

    edited ... Iam having no errors.. just the jList is not getting updated.. sorry about the words.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JList Update and Clear + Other Annoying Crap... Up for a challenge? Lol (Help)

    package GUI;
     
    import javax.swing.*;
    import java.awt.*;
    import java.net.*;
    import java.io.*;
    import java.util.*;
     
    public class GuiMaintain extends javax.swing.JFrame
    {
     
        public DefaultListModel ListModel = new DefaultListModel();
        public ArrayList editRecord = new ArrayList();
     
        public GuiMaintain()
        {
            initComponents();
            //maintainLoad();
            jList1.setModel(ListModel);
        }
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            btnEdit = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jList1 = new javax.swing.JList();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Maintain Documents");
     
            btnEdit.setText("Edit");
            btnEdit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnEditActionPerformed(evt);
                }
            });
     
            jScrollPane1.setViewportView(jList1);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(719, Short.MAX_VALUE)
                    .addComponent(btnEdit)
                    .addContainerGap())
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnEdit)
                    .addGap(6, 6, 6))
            );
     
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-796)/2, (screenSize.height-493)/2, 796, 493);
        }// </editor-fold>
     
    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if (jList1.getSelectedIndex() != -1)
        {
            Functionality.Documents documents = new Functionality.Documents();
            documents.setIndex(jList1.getSelectedIndex());
     
            GuiEdit editForm = new GuiEdit();
            editForm.setVisible(true);
     
            String splittedRecord[] = editRecord.get(documents.getIndex()).toString().split("\\|");
            editForm.txtTitle.setText(splittedRecord[0]);
            editForm.txtSummary.setText(splittedRecord[1]);
            editForm.txtPath.setText(splittedRecord[3]);
            editForm.txtRefNo.setText(splittedRecord[2]);
        }
    }                                       
     
        public void maintainLoad()
        {
            try
            {
                Scanner getRecords;
                URL url = getClass().getResource("/Information/records.txt");
                getRecords = new Scanner(new File(url.getPath()));
     
                for (int a = 0; getRecords.hasNextLine(); a++)
                {
                    String line = getRecords.nextLine();
                    String splittedText[] = line.split("\\|");
                    if (splittedText.length == 4)
                    {
                        editRecord.add(a, line);
                        ListModel.add(ListModel.size(), "Title: " + splittedText[0] + " - " + "Summary: " + splittedText[1]
                                + " - " + "Ref no: " + splittedText[2] + " - " + "Path: " + splittedText[3]);
                    }
                }
                getRecords.close();
            } catch (IOException e)
            {
                System.out.println("Error");
            }
        }
     
        public void editArrayUpdate(String title, String summary, String refNo, String path)
        {
            try
            {
                URL url = getClass().getResource("/Information/records.txt");
                Functionality.Documents documents = new Functionality.Documents();
                //re-load empty readUpdate arraylist with records and add the one that needed to be implemented.
                Scanner readUpdate;
                readUpdate = new Scanner(new File(url.getPath()));
                for (int i = 0; readUpdate.hasNextLine(); i++)
                {
                    if(i == documents.getIndex())
                    {
                    editRecord.add(documents.getIndex(), title + "|" + summary + "|" + refNo + "|" + path);
                    System.out.println(documents.getIndex());
                    }
                    else 
                    { 
                       editRecord.add(readUpdate.nextLine()); 
                    }
                }
                readUpdate.close();
     
                //update text file .....
                Formatter writeNew = new Formatter(url.getPath());
                for (int i = 0; i < editRecord.size(); i++)
                {
                    writeNew.format(editRecord.get(i).toString() + "%n");
                }
                writeNew.close();
     
                //update list...
                Scanner Update;
                Update = new Scanner(new File(url.getPath()));
                for (int i = 0; Update.hasNextLine(); i++)
                {
                    String line = Update.nextLine();
                    String splittedText[] = line.split("\\|");
                    ListModel.add(i, "Title: " + splittedText[0] + " - " + "Summary: " + splittedText[1] + " - " + "Ref no: " + splittedText[2] + " - " + "Path: " + splittedText[3]);
                }
                Update.close();
            } catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
     
     
     
        }
        // Variables declaration - do not modify
        private javax.swing.JButton btnEdit;
        public javax.swing.JList jList1;
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration
    }

Similar Threads

  1. Challenge: I need an animation loop outside of the paint method.
    By Spidey1980 in forum Java Theory & Questions
    Replies: 28
    Last Post: August 20th, 2011, 09:20 AM
  2. Can not clear the contents of JList Java
    By nfs3250 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 10th, 2011, 01:13 AM
  3. CHALLENGE - find the shortest path
    By ice in forum Algorithms & Recursion
    Replies: 13
    Last Post: January 20th, 2011, 12:30 PM
  4. A lot of annoying cast errors.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 10:54 PM
  5. annoying error.
    By gajamaflake in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2010, 04:29 PM