0

Hi all, I have a class CAR.java

public class CAR {

    private int ID;
    private String model;

    public CAR(int ID, String model) {
        this.ID = ID;
        this.model = model;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public int getmodel() {
        return model;
    }

     public void setmodel(int model) {
        this.model = model;
    }

/* ********************* */

and the file CARInitialData.java

public class CARInitialData {

    private List<CAR> list;
    private static final int carCounter = 1;

    public CARInitialData() {
        list = new ArrayList<CAR>();
        list.add(new CAR(carCounter,"Audi A3"));
        list.add(new CAR(carCounter+1,"Audi A5"));
   }

   public List<CAR> getCARList() {
       return list;
   }

}

/* ********************* */

I am using netbeans and I am trying to make a menu page, a display page form and edit rows form.
in mainmenu.java file

public class mainmenu extends javax.swing.JFrame {

    public CARInitialData initialCARData;
    private Vector row;
    public  List<CAR> CARList; 

    public mainmenu() {
        initComponents();

        initialCARData = new CARInitialData();
        CARList = initialCARData.getCARList();
    }
}

but how can I access these list of CARs from display page or edit page.

Could you help me please with an example because I am confused.

Thanks in advance

2
Contributors
4
Replies
32
Views
2 Months
Discussion Span
Last Post by peter20
1

I understand that you have multiple GUI classes that all need to access the same list of cars?

If so, a common approach is to create one list of Cars and pass it to each of the GUI classes via their constructors, eg (abbreviated code)

p s v  main(...) {

   List<Car> carList = new CarInitialData().getCarList();

   new MainMenu(carList);  
   ...
   new EditPage(carList);  
   etc
}

public class MainMenu extends javax.swing.JFrame {
   private List<Car> carList;

   public MainMenu(List<Car> carList) {
        this.carList = carList; 
        // now I have a reference in this class to the master car list
         initComponents();
        ...

public class EditPage extends javax.swing.JFrame {
   private List<Car> carList;

   public EditPage(List<Car> carList) {
        this.carList = carList; 
        // now I have a reference in this class to the master car list
         initComponents();
        ...

If it's the MainMenu that calls the new EditPage then it can pass in the carList in exactly the same way.

Notes:

This is a common pattern called model-view-controiller (MVC). The Model is the data etc, the View is the form and its fields etc, the Controller is the code that ties them together (in Java you often combine the view and controller in a single class). So when you create a View you pass it the Model that it;s going to display, and that's how it works.

I suggested names for classes and variables that fit normal Java coding standards.

I see you have not compiled the CAR class. YOu should compile small secions of code as early as possible, so you only have to deal with one or two errors at a time. Waiting until you have all the code before compiling or testing will guarantee you are submerged in a mass of confusing errors all at once.

Edited by JamesCherrill

0

I am following your advice but I get the error in the main function of the form
non-static variable variable carList cannot be referenced from a static context

public class Display extends javax.swing.JFrame {

    private List<CAR> courseList;

    public Display(List<CAR> carList) {
        this.courseList = carList; 
        initComponents();
    }

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Display(carList).setVisible(true);   //<-- error here
            }
        });
    }
}
1

OK, you have most of the right pieces, but in the wrong place and some missing!

Let's look at the overall class structure.
Here's a template that will work for all kinds of GUI apps:

A class that starts up the whole application, and has the main method
Some classes that represent the "model" or data (Car etc)
Some classes that provide the GUI (MainMenu etc)
Other lower-level utility classes as required

What you have doe is the put main inside a GUI class and missed out the bit in main where the carList is created. Hence your problems

So the structure should be:

class MyCarApp { // this is the main (startup) class of the app

    // probably no need to code a constructor for this class
    // because you won't create any instances (just static)

    p s v main(...) {
        // initialise everything (eg lines 12-27 in your code above)
        // create a car list
        // create the first window (MainMenu) of the GUI,
        //    passing it the car list (eg lines 30-34 above)

  }

  // other classes as per our previous posts

I know at this stage it looks like the "application" class looks a bit silly, with just one method, but as you do more complex applications you will have all kinds of initialisation (logons, loading configurations, defaults, user defaults, opening databases, holding public constants that are needed throughout the app etc etc) and that first class gets plenty big and complicated. So it's the right approach to learn now and use forever.

Edited by JamesCherrill

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.