1

I'm a beginner at Java code and have recently come to an issue regarding my Edit GUI JDialog. I'm doing an assignment which collects the information on DVD's and adds it to the main GUI's JList. Whilst everything thus far appears to be working, the only issue is the Edit GUI. The user is meant to select an object in the main GUI's JList and click the edit button to open a JDialog for the user to then edit the information residing in the Object. This isn't working and keeps saying that the user must select an object in a JList to edit, even if the user has or hasn't selected an object.

I have provided all my code below:

MAIN GUI:

public class DVDGUI extends javax.swing.JFrame {

    private DVDCollection dvdcollection = new DVDCollection();
    public DVDGUI() {
        initComponents();
        loadData();
    }

    private void loadData()
    {
        try
        {
            ObjectInputStream infile = new ObjectInputStream(new FileInputStream("heatherdunne.txt"));
            dvdcollection = (DVDCollection)infile.readObject();
            infile.close();
            updateDVDList();
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(this,"Error found - " + e);
        }
    }

    public void saveData()
    {
        try
        {            
            ObjectOutputStream outfile = new ObjectOutputStream(new FileOutputStream("heatherdunne.txt"));
            outfile.writeObject(dvdcollection);
            outfile.close();
            System.exit(0);
        }
        catch (Exception e)
        {
               JOptionPane.showMessageDialog(this,"Error writing file - " + e);        
        }
    }

    @SuppressWarnings("unchecked")
    private void lstDVDsValueChanged(javax.swing.event.ListSelectionEvent evt) {                                     
        if (lstDVDs.getSelectedIndex() >= 0)
            txtDetails.setText(((DVD)lstDVDs.getSelectedValue()).getDetails());
        else
            txtDetails.setText(" ");                            

        int selectedIndex = lstDVDs.getSelectedIndex();
        ListModel lModel= lstDVDs.getModel();
        DVD item = (DVD)lModel.getElementAt(selectedIndex);
        System.out.println("Title= "+ item.getTitle());  
        boolean favourite = false;
        favourite = true;
        favourite = item.favourite;
        if (item.favourite == true)
        uebbtnFav.setSelected(true);
        else
        uebbtnFav.setSelected(false);
    }
    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if (lstDVDs.getSelectedIndex() >= 0)
        {
          JOptionPane.showMessageDialog(this,"You need to select a DVD to be edited");
        }
        else
        {
            DVD dvvvd = (DVD)lstDVDs.getSelectedValue();
            EditDVD idk = new EditDVD(this, dvvvd);  
        }
    }
    private void loadDVDsfromfile(){
        DVD dvd = null;
        DVD dvd2 = null;        
        DVD dvd3 = null;        
        try
        {    
            FileInputStream file = new FileInputStream("C:\\Users\\User\\Desktop\\code\\Assign2\\heatherdunne.txt"); 
            ObjectInputStream in = new ObjectInputStream(file); 

            dvd = (DVD)in.readObject(); 
            dvd2 = (DVD)in.readObject();             
            dvd3 = (DVD)in.readObject();             

            dvdcollection.addDVD(dvd);
            dvdcollection.addDVD(dvd2);
            dvdcollection.addDVD(dvd3);

            in.close(); 
            file.close(); 

            System.out.println("Object has been deserialized "); 
            System.out.println(dvd);
            System.out.println(dvd2);
            System.out.println(dvd3);      

        } 

        catch(IOException ex) 
        { 
            System.out.println("IOException is caught"); 
        } 

        catch(ClassNotFoundException ex) 
        { 
            System.out.println("ClassNotFoundException is caught"); 
        }                 
    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        loadDVDsfromfile();  
        DefaultListModel lstmdl = new DefaultListModel();
        lstmdl.addElement(dvdcollection.getDVDs().get(0)); 
        lstDVDs.setModel(lstmdl);
    }                                 
    private void crtFileActionPerformed(java.awt.event.ActionEvent evt) {                                        
          DVD dvd = new DVD("Avengers: Endgame", 2019, Boolean.TRUE);
          DVD dvd2 = new DVD("Shrek 2", 2004, Boolean.TRUE);
          DVD dvd3 = new DVD("IT", 2017, Boolean.TRUE);

        try
        {    
            FileOutputStream file = new FileOutputStream("C:\\Users\\User\\Desktop\\code\\Assign2\\heatherdunne.txt"); 
            ObjectOutputStream out = new ObjectOutputStream(file); 

            out.writeObject(dvd); 
            out.writeObject(dvd2); 
            out.writeObject(dvd3);



            out.close(); 
            file.close(); 

            System.out.println("Object has been serialized"); 
        } 

        catch(IOException ex) 
        { 
            System.out.println("IOException is caught"); 
        }    

    }                                       
    public void updateDVDList()
    {       
        if (rbtTitle.isSelected())
            sortByTitle();
        else
                sortByID();
        lstDVDs.setListData(dvdcollection.getDVDs().toArray());
    }                                

EDIT DVD GUI

private DVD selectedDVD;
    public EditDVD(DVDGUI inParent, DVD inDVD)
    {
        super(inParent, true);          
        initComponents();
        parent = inParent;
        Title.setText(selectedDVD.getTitle());        
        Year.setText(selectedDVD.getYear() + "");
        setVisible(true);        
    }

@SuppressWarnings("unchecked")
private void EditActionPerformed(java.awt.event.ActionEvent evt) {                                     
    String title = Title.getText();
    String year = Year.getText();

    String msgTitle = DVD.checkTitle(title);
    String msgYear = DVD.checkYear(year);        

    if (msgTitle.length()>0)
        JOptionPane.showMessageDialog(this, msgTitle);
    else
    if (msgYear.length()>0)
        JOptionPane.showMessageDialog(this, msgYear);
    else    
    {
        //validation was ok
            selectedDVD.setTitle(title);
            selectedDVD.setYear(Integer.parseInt(year));
            parent.updateDVDList();
            JOptionPane.showMessageDialog(this, "Update sucessful");
            dispose();
    }
}    

DVD CLASS:

public class DVD implements Serializable
{
    private String id;     
    private String title;                                                                     
    private int year;                                                          
    boolean favourite;  
    private int nextID=1;

    public DVD(String inID, String inTitle, int inYear, boolean inFavourite)
    {
        id = inID;
        title = inTitle;
        year = inYear;
        favourite = inFavourite;
    }
    public DVD (String inTitle, int inYear, boolean inFavourite)
    {  
        id = "0";
        nextID++; 
        title = inTitle;
        year = inYear;
        favourite = inFavourite;        
    }


    public String toString()
    {
        return title;
    } 

    public String getID()
    {
        return id;
    }

    public void setID(String inID)
    {
        id = inID;
    }

    public String getDetails()
    {
        return "id(" + id + ") " + title + " <" + year + "> ";
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String inTitle)
    {
        title = inTitle;
    }

    public String getYear()
    {
        if (year==0)
            return " ";
        else
            return year + "";
    } 

    public void setYear(int inYear)
    {
        year = inYear;
    }

    public boolean isFavourite()
    {
        return favourite;
    }    

    public void setFavourite(boolean inFavourite)
    {
        favourite = inFavourite;
    }  

    public static String checkTitle(String inTitle)
    {
        if (inTitle.trim().length()>0)
            return "";
        else
            return "Title must have at least 1 non-blank character";
    } 

    public static String checkYear(String inYear)
    {
        Calendar now = Calendar.getInstance();  
        int currYear = now.get(Calendar.YEAR);         
        try
        {
            int year = Integer.parseInt(inYear);
            if ((year >= 1997)&&(year <= currYear))
                return ""; 
            else
                return "Year must be between 1997 and current year";
        }
        catch (Exception ex)
        {
            return "Year must be a 4 digit integer";
        }
    }

    public static String checkID(String inID)
    {
        if (inID.trim().length()>0)
            return "";
        else
            return "ID must have at least 1 non-blank character";
    } 

    public static boolean checkFavourite(boolean inFav)
    {
        boolean Favs = false;
        int total = 0;
        if ( !Favs ) 
        {
        total = total + 1;
        }
        return false;
    }
    public int compareTo(DVD d2)
    {
//        if (ID<c2.getID())
//      return -1;
//  else 
//         if (ID>c2.getID())
//          return 1;
//       else return 0;        
        return id.compareTo(d2.getID());
    }        
}

DVDCollection Class:

public class DVDCollection implements Serializable
{
    private ArrayList<DVD> dvds = new ArrayList<DVD>();
    private int nextID=1;
    public boolean addDVD(DVD inDVD)
    {   
        return 
        dvds.add(inDVD);
    }

    public DVDCollection()
    {
    }

    public ArrayList<DVD> getDVDs()
    {
        return dvds;
    }

    public int getNumDVDs()
    {
        return dvds.size();
    }

    public boolean deleteDVD(DVD inDVD)
    {
       return dvds.remove(inDVD);
    }

    public int CountFavourites()
    {
        int total = 0;

        if (DVD.checkFavourite(true))
        {
            boolean fav = true;   
            int val = fav? 1 : 0;
            total = 1 + total;
        }
        else
        {
            boolean fav = false;   
            int val = fav? 1 : 0;
        }


        return total;
    }
}

I’ve followed the steps for an Edit GUI provided in my class but this issue still appears. Another thing I tried but took out was converting all the variables to String and also took out the edit function of the favourite jcheckbox in case that was the issue. I’ve done loads more but all have resulted in failure and the same issue appearing nonetheless.

All help would be really appreciated

Update: Issue has been resolved

  • If you need Add, please ask as It exceeds the letter amount on my question – beginnercoderanon Oct 06 '19 at 10:05
  • Hello, a good way to get better help sooner is to show us what you have tried and exactly where you are stuck. Have a look at [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), often known as a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) or [SSCCE](http://sscce.org/). I appreciate you have added your code but there is a lot to go through there. Try creating a small example that uses only one or two files – Dan Oct 06 '19 at 11:55
  • Thank you for this input, I'll fix the size of the given code right away – beginnercoderanon Oct 06 '19 at 13:30

1 Answers1

1

I have resolved my codes issue