0

I am working on a small programm that should display recipies in a JTable. All seems to work fine, the only problem I've got is that I can't initialize the object that should hold the data properly. Here's the class that gives me headache:

class RecipeTableModel extends AbstractTableModel {

    private String[] columnNames = {"Number", "Name", "Difficulty",
        "Preparation", "PreparationTime", "Quantity", "Incredients"};
    Recipe recipe1 = new Recipe("1", "SecretRecipe", "easy", "microwave", "20min", "100gr", "flour");
    Recipe recipe2 = new Recipe("2", "SuperRecipe", "medium", "microwave", "30min", "100gr", "Salt");
    **RecipeDB recipeDB = new RecipeDB();
    recipeDB.addRecipe (Recipe recipe1);**


    @Override
    public int getColumnCount() {
        return 7;
    }

    public int getRowCount() {
        //return recipeDB.getRecipeDBSize();
        return 15;
    }

    @Override
    public Object getValueAt(int row, int col) {

    /*  Recipe recipe = recipeDB.getRecipe(row);
        switch (col){
        case 0:
            return recipe.getNumber();
        case 1:
            return recipe.getName();
        case 2:
            return recipe.getPrep();
        case 3:
            return recipe.getPrepTime();
        case 4:
            return recipe.getDifficulty();
        case 5:
            return recipe.getIngredients(); 
        }
        */
        return null;
    }

    public String getColumnName(int column) {
        return columnNames[column];
    }
}

I get a syntax error when I try to add recipies to recipeDB; and when I debug, I see that recipeDB is initialized to a null value. How is this possible?

Here's my other class:

import java.util.ArrayList;

public class RecipeDB {

    public ArrayList<Recipe> recipeArraylist;

    public RecipeDB() {
        recipeArraylist = new ArrayList<Recipe>();
    }

    public void addRecipe(Recipe recipe) {
        recipeArraylist.add(recipe);
    }

    public int getRecipeDBSize() {
        return recipeArraylist.size();
    }

    public Recipe getRecipe(int i) {
        return recipeArraylist.get(i);
    }

    public ArrayList getDBArrayList() {
        return recipeArraylist;
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
ALMA
  • 1
  • 1

1 Answers1

1

The syntax error comes from attempting to invoke an instance method on recipeDB in the class body declaration section of RecipeTableModel.

RecipeDB recipeDB = new RecipeDB();  // legal declaration and initialization
recipeDB.addRecipe (Recipe recipe1); // illegal method invocation

The method call would be legal in an instance initializer or the constructor. In the example below, neither is required; instances of a Recipe stub are added in a loop using the addRecipe() method.

image

import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

class RecipeTableModel extends AbstractTableModel {

    private final String[] columnNames = {"Number", "Name", "Difficulty",
        "Preparation", "Prep Time", "Quantity", "Ingredients"};
    private final RecipeDB recipeDB = new RecipeDB();

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return recipeDB.getRecipeDBSize();
    }

    @Override
    public Object getValueAt(int row, int col) {
        return "(" + row + ", " + col + ")";
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    private static class RecipeDB {

        public ArrayList<Recipe> recipeArraylist = new ArrayList<Recipe>();

        public void addRecipe(Recipe recipe) {
            recipeArraylist.add(recipe);
        }

        public int getRecipeDBSize() {
            return recipeArraylist.size();
        }

        public Recipe getRecipe(int i) {
            return recipeArraylist.get(i);
        }

        public ArrayList getDBArrayList() {
            return recipeArraylist;
        }
    }

    private static class Recipe {}

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            RecipeTableModel model = new RecipeTableModel();
            for (int i = 0; i < 42; i++) {
                model.recipeDB.addRecipe(new Recipe());
            }
            JFrame f = new JFrame("Test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JScrollPane(new JTable(model)));
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045