0

I have read quite a few pages of stackoverflow but I wasn't able to get my ArrayList to get copied unto another class. Here's the scenario, I'm building a quick book saver app, similar to what you would have in a library but simpler (for school).

I have my main library class (with the main) that has the swing set up for the main menu/options.

I have the book class with the constructor for new books as follows:

public class Livre {

private String titre;
private String soustitre;
private String auteur;
private String editeur;
private String collection;
private String isbn;
private long cup;
private double prixDeVenteSuggere;
private double prixVente;
private int nbPages;
private boolean disponible;

public Livre(String titre, String soustitre, String auteur, String editeur, String collection, String isbn, long cup, double prixDeVenteSuggere, double prixVente, int nbPages, boolean disponible){
    this.titre = titre;
    this.soustitre = soustitre;
    this.auteur = auteur;
    this.editeur = editeur;
    this.collection = collection;
    this.isbn = isbn;
    this.cup = cup;
    this.prixDeVenteSuggere = prixDeVenteSuggere;
    this.prixVente = prixVente;
    this.nbPages = nbPages;
    disponible = true;
}

public Livre() {

}

public String getTitre() {
    return titre;
}

public void setTitre(String titre) {
    this.titre = titre;
}

public String getSoustitre() {
    return soustitre;
}

public void setSoustitre(String soustitre) {
    this.soustitre = soustitre;
}

public String getAuteur() {
    return auteur;
}

public void setAuteur(String auteur) {
    this.auteur = auteur;
}

public String getEditeur() {
    return editeur;
}

public void setEditeur(String editeur) {
    this.editeur = editeur;
}

public String getCollection() {
    return collection;
}

public void setCollection(String collection) {
    this.collection = collection;
}

public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

public long getCup() {
    return cup;
}

public void setCup(long cup) {
    this.cup = cup;
}

public double getPrixDeVenteSuggere() {
    return prixDeVenteSuggere;
}

public void setPrixDeVenteSuggere(double prixDeVenteSuggere) {
    this.prixDeVenteSuggere = prixDeVenteSuggere;
}

public double getPrixVente() {
    return prixVente;
}

public void setPrixVente(double prixVente) {
    this.prixVente = prixVente;
}

public int getNbPages() {
    return nbPages;
}

public void setNbPages(int nbPages) {
    this.nbPages = nbPages;
}

public boolean isDisponible() {
    return disponible;
}

public void setDisponible(boolean disponible) {
    this.disponible = disponible;
}

}

Option #1 on the Library class (built with WindowBuilder) has the "New" button which opens a second JFrame to input all the info in regards to the book.

in this JFrame class, I've added an actionListener on the confirm button to confirm the input on the JTextFields to be added as an object as follows:

public void confirmerLivre(){
    l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(), 
                    txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(), 
                    Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()), 
                    Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);

    confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList
}

What I can't wrap my head around is being able to take the ArrayList confirmerLivre from the 2nd JFrame class and push it unto my main JFrame class to be manipulated further with other options.

Any help will be greatly appreciated. Thank you

JFreeman
  • 974
  • 1
  • 10
  • 26

3 Answers3

0

Probably the quickest fix is to create/expose these methods in your main JFrame class:

  • getBookList()
  • setBookList()

When you create your popup JFrame, you need to pass an instance of your main JFrame class to it in its constructor:

public PopupFrame extends JFrame {
  private MainFrame main;
  public PopupFrame(MainFrame main) {
    this.main = main;
  }
}

Now that you have access to your main JFrame class from your popup, you can just go main.getBookList() to get the list (I'd recommend reading this question also)

Community
  • 1
  • 1
Catchwa
  • 5,845
  • 4
  • 31
  • 57
  • Thank you very much for this, there's an answer below with access to the ArrayList between classes thanks to its global scope but with your getter/setter method is the one the teacher needs to see. Thanks once again – Oleg Tchernov May 19 '17 at 13:55
0

If you create your ArrayList as a public variable in the second JFrame class (outside any of the methods) then it can be used in the first class such as:

SecondJFramesName.confirmerLivre()

In this code SecondJFramesName is the name of your second JFrame class. Now that your ArrayList is a public variable it can be accessed outside the class.

Note: your second JFrame's name is the one you use to create it in a way such as this:

JFrame SecondJFramesName = new JFrame("My title");

If you need any more specific details please comment!

Hopefully this helps!

JFreeman
  • 974
  • 1
  • 10
  • 26
  • Thank you 100 times, this works. The getter/setter method is the one I need to go with due to teacher's constraints. – Oleg Tchernov May 19 '17 at 13:55
  • Great! I'm glad it helped. Also if you think it is a good answer upvote it (click the up arrow next to the number on the left) so other people who view the question know! – JFreeman May 19 '17 at 22:39
  • I did, but since my reputation points are below 15 it apparently doesn't publicly show. I'll be back once it hits 15 – Oleg Tchernov May 21 '17 at 02:50
0

Maybe observer pattern could help you:

public interface ConfirmerLivreMonitor{
    void onConfirmerLivreChange(List<...> confirmerLivre);
}

then

//...
private ConfirmerLivreMonitor confirmerLivreMonitor;
public void setConfirmerLivreMonitor(ConfirmerLivreMonitor confirmerLivreMonitor ){
    this.confirmerLivreMonitor = confirmerLivreMonitor 
}
//....
public void confirmerLivre(){
    l = new Livre(txtTitre.getText(), txtSousTitre.getText(), txtAuteur.getText(), 
                    txtEditeur.getText(), txtCollection.getText(), txtISBN.getText(), 
                    Long.parseLong(txtCodebar.getText()), Double.parseDouble(txtPrixMSRP.getText()), 
                    Double.parseDouble(txtPrix.getText()), Integer.parseInt(txtPages.getText()), true);

    confirmerLivre.add(l); /// confirmerLivre is defined as an ArrayList

    if(confirmerLivreMonitor != null){   //notify confirmerLivre change
       confirmerLivreMonitor.onConfirmerLivreChange(confirmerLivre);
    }
}

make the Main JFrame implemnents ConfirmerLivreMonitor,so you can:

sencondJFrame.setConfirmerLivreMonitor(this);

or just pass a anonymous class:

sencondJFrame.setConfirmerLivreMonitor(new ConfirmerLivreMonitor(){

        public void onConfirmerLivreChange(List<...> confirmerLivre){
            //display in Main JFrame,maybe
        }
});

once the confirmerLivre change, the main frame can display(or something else) the first time,very cool

rayen
  • 89
  • 7