0

I'm fairly inexperienced in programming, so this might be a very stupid mistake. I'm trying to reference objects I've instantiated in the class "DateSelectionSpinner" in my "speiseplanDesign" class. The variables in question are "daySelector", "monthSelector" and "yearSelector". Both classes are in the same package and I'm using Eclipse for coding. I'm thankful for any help I can get :)

This is the class I want to use the variables in:

package foodTracker_View;
import java.awt.*;
import java.time.LocalDate;
import javax.swing.*;

/**
 * 
 * @author 
 * @version 09.06.23
 *
 */
public class speiseplanDesign extends JPanel {

    private JLabel beschriftungSpeiseplan;
    private LocalDate date = LocalDate.now();
    private JFormattedTextField eingabefeldDatum;
    private JPanel datumsauswahl;
    
    public speiseplanDesign(int width, int height) {
        this.setSize(width, height);
        
        this.setBackground(Color.black);            
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.setBorder(BorderFactory.createLineBorder(Color.black, 20, true));
        this.setOpaque(true);
        ScrollPane speisplanScroll = new ScrollPane();

        beschriftungSpeiseplan = new JLabel("Speiseplan vom " + date, JLabel.CENTER);
        this.add(beschriftungSpeiseplan);
        beschriftungSpeiseplan.setOpaque(true);
        
        datumsauswahl = new JPanel();
        datumsauswahl.add(daySelector);    //Error: daySelector cannot be resolved to a           
                                                            variable
        datumsauswahl.add(monthSelector);   //same issue as above
        datumsauswahl.add(yearSelector);    //same issue as above
        
    }           
}

This is the class I've defined them in:

package foodTracker_View;
import javax.swing.*;
import java.time.*;
import javax.swing.SpinnerNumberModel;


/**
 * 
 * Diese Klasse beinhaltet die Vorgaben für alle graphischen Komponenten, 
 * die im Speiseplan für die Auswahl des Datums benötigt werden
 * 
 * @author 
 * @version 09.06.23
 *
 */

public class DateSelectionSpinner {

    private int currentYear = Year.now().getValue();
        SpinnerNumberModel yearSelector = new SpinnerNumberModel(currentYear, currentYear, currentYear + 5, 1 );
    
    String[] months = { "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" };
        private SpinnerListModel allowedMonths = new SpinnerListModel(months);
        JSpinner monthSelection = new JSpinner(allowedMonths);
    
    private LocalDate currentDate = LocalDate.now();
    private int currentDay = currentDate.getDayOfMonth();
    int maxDay = 30;
        SpinnerNumberModel daySelector = new SpinnerNumberModel(currentDay, currentDay, maxDay, 1);

    
}

I've tried specifically importing the second class to the first one, but that didn't do anything.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 2
    If they are in different classes, they are different variables who just happen to have the same name. – Scott Hunter Jun 09 '23 at 13:48
  • 1
    Does this answer your question? [What is 'scope' in Java?](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) – OH GOD SPIDERS Jun 09 '23 at 13:52
  • Define the variable access using "public" is probably the problem, no access modifier among variables that do have other access modifiers it may be being "default" (package private) . If the classes are in the same package folder you may be able to use "protected" if it's used nowhere outside, but you will need to use a protected method to call it even with the internal passed class reference if it is a global. – Samuel Marchant Jun 09 '23 at 15:42

3 Answers3

1

The variables you menction: daySelector, monthSelector and yearSelector, are all atributes that belong to the class DateSelectionSpinner.

As you might know, atributes can be private or public. I asume you want the menctioned atributes to be public. Hence, to begin, I would recommend to add public keyword when you declare those atributes, for more clarity.

Now, to access those atributes, you first need to create an instance of DateSelectionSpinner.

Your code should look something like this:

Class speiseplanDesign

public class speiseplanDesign extends JPanel {

    private JLabel beschriftungSpeiseplan;
    private LocalDate date = LocalDate.now();
    private JFormattedTextField eingabefeldDatum;
    private JPanel datumsauswahl;
    
    public speiseplanDesign(int width, int height) {
        this.setSize(width, height);
        
        this.setBackground(Color.black);            
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.setBorder(BorderFactory.createLineBorder(Color.black, 20, true));
        this.setOpaque(true);
        ScrollPane speisplanScroll = new ScrollPane();

        beschriftungSpeiseplan = new JLabel("Speiseplan vom " + date, JLabel.CENTER);
        this.add(beschriftungSpeiseplan);
        beschriftungSpeiseplan.setOpaque(true);
        
        datumsauswahl = new JPanel();
        DateSelectionSpinner dss = new DateSelectionSpinner();
        datumsauswahl.add(dss.daySelector);
        datumsauswahl.add(dss.monthSelector);
        datumsauswahl.add(dss.yearSelector);
        
    }           
}

Class DateSelectionSpinner

public class DateSelectionSpinner {

    private int currentYear = Year.now().getValue();
    public SpinnerNumberModel yearSelector = new SpinnerNumberModel(currentYear, currentYear, currentYear + 5, 1 );
    
    String[] months = { "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" };
    private SpinnerListModel allowedMonths = new SpinnerListModel(months);
    public JSpinner monthSelection = new JSpinner(allowedMonths);
    
    private LocalDate currentDate = LocalDate.now();
    private int currentDay = currentDate.getDayOfMonth();
    int maxDay = 30;
    public SpinnerNumberModel daySelector = new SpinnerNumberModel(currentDay, currentDay, maxDay, 1);
}

Some additional comments

  • It is generally not recommended to have public atributes. You should probably use setters and getters rather than public atributes. You can read about it in Java Encapsulation
  • Class names should always start with capital letters. Java Naming Conventions
  • Most of the concepts I mention (atributes, classes, public, private, etc) are Object Oriented Programming concepts. Since most of what you do in Java is Object Oriented, I would recommend reading Java OOP if don't yet feel completely familiar with these concepts.
  • I think you meant one class in one file. As one of the answers stated, "Creating a new package for every public class would be extremely cumbersome and provide no overview at all." – Gilbert Le Blanc Jun 09 '23 at 14:20
  • @GilbertLeBlanc you are right. Thanks for the heads up – Juan Esteban Arboleda Restrepo Jun 09 '23 at 14:22
  • _"As you might know, atributes can be private or public."_ -> actually, the OP has correctly used a third access type: package private (which is the default access when you don't specify public or private). – k314159 Jun 09 '23 at 14:45
0

Your program, as it stands now, has structural problems. It would be better to have DateSelectionSpinner as a class that encapsules the whole UI for date selection, instead of providing just pieces for other classes to use.

But to keep things easy and not change too much, let's stick with what we have and make it work...

First, rename the second class to a more fitting name, and split the code into small methods for each part of the date selection it provides:

package foodTracker_View;
import javax.swing.*;
import java.time.*;
import javax.swing.SpinnerNumberModel;


/**
 * 
 * Diese Klasse beinhaltet die Vorgaben für alle graphischen Komponenten, 
 * die im Speiseplan für die Auswahl des Datums benötigt werden
 * 
 * @author 
 * @version 09.06.23
 *
 */

public class DateSelectionSpinnerFactory {
    private int currentYear = Year.now().getValue();
    private SpinnerNumberModel yearModel = new SpinnerNumberModel(currentYear, currentYear, currentYear + 5, 1 );

    public JSpinner createYearSelector() {
        return new JSpinner(yearModel);
    }

    private static String[] months = { "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" };
    private SpinnerListModel allowedMonths = new SpinnerListModel(months);

    public JSpinner createMonthSelector {
        return new JSpinner(allowedMonths);
    }

    private LocalDate currentDate = LocalDate.now();
    private int currentDay = currentDate.getDayOfMonth();
    private int maxDay = 30;
    private SpinnerNumberModel dayModel = new SpinnerNumberModel(currentDay, currentDay, maxDay, 1);

    public JSpinner createDaySelector {
        return new JSpinner(dayModel);
    }        
}

and now let the first class use it:

package foodTracker_View;
import java.awt.*;
import java.time.LocalDate;
import javax.swing.*;

/**
 * 
 * @author 
 * @version 09.06.23
 *
 */
public class speiseplanDesign extends JPanel {

    private JLabel beschriftungSpeiseplan;
    private LocalDate date = LocalDate.now();
    private JFormattedTextField eingabefeldDatum;
    private JPanel datumsauswahl;
    
    public speiseplanDesign(int width, int height) {
        this.setSize(width, height);
        
        this.setBackground(Color.black);            
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.setBorder(BorderFactory.createLineBorder(Color.black, 20, true));
        this.setOpaque(true);
        ScrollPane speisplanScroll = new ScrollPane();

        beschriftungSpeiseplan = new JLabel("Speiseplan vom " + date, JLabel.CENTER);
        this.add(beschriftungSpeiseplan);
        beschriftungSpeiseplan.setOpaque(true);
        
        DateSelectionSpinnerFactory dssf = new DateSelectionSpinnerFactory();

        datumsauswahl = new JPanel();
        datumsauswahl.add(dssf.createDaySelector());        
        datumsauswahl.add(dssf.createMonthSelector());
        datumsauswahl.add(dssf.createYearSelector());        
    }           
}
Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
0

To access the DateSelectionSpinner variables from the speiseplanDesign class, you'll need to instantiate the object.
Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects).

DateSelectionSpinner dateSelectionSpinner = new DateSelectionSpinner();

Then, access the dateSelectionSpinner variables using dot-notation.

dateSelectionSpinner.daySelector
datumsauswahl = new JPanel();
DateSelectionSpinner dateSelectionSpinner = new DateSelectionSpinner();
datumsauswahl.add(dateSelectionSpinner.daySelector);    //Error: daySelector cannot be resolved to a
variable
datumsauswahl.add(dateSelectionSpinner.monthSelector);   //same issue as above
datumsauswahl.add(dateSelectionSpinner.yearSelector);    //same issue as above
Reilas
  • 3,297
  • 2
  • 4
  • 17