I'm having issues passing data from a GUI to other classes. Everything is initialized in the GUI and then from there the data is passed to another class where additional changes may take place: (simplified and missing parts for compilation)
class GUI extends JFrame {
final Configuration conf = new Configuration();
GUI() {
initComponents();
}
private void initComponents() {
//Setup default values
conf.expectFires(false);
conf.expectRain(false);
conf.expectDisaster(true);
JComboBox<String> seasonTypes = new JComboBox<>(new String[]{"Winter", "Spring", "Summer", "Fall"});
seasonTypes.setSelectedIndex(0);
conf.setSeason(seasonTypes.getItemAt(seasonTypes.getSelectedIndex()));
//Action listener for dynamic changes to whatever is selected
seasonTypes.addActionListener(e -> {
String season = seasonTypes.getSelectedItem().toString();
if (!season.equals(""))
conf.setSeason(season);
});
pack();
setLocationRelativeTo(getOwner());
setVisible(true);
}
}
The data is supposed to be saved to my configuration class which is a getter/setter class. I'm unable to retrieve any data unless I set it within the same class:
Main class:
public class Main {
public static void main(String[] args) {
Configuration conf = new Configuration();
//code for GUI not included but pretend the GUI is called here
//data does not come out it's basically unset.
System.out.println("Data from GUI: [season]"+ conf.getSeason()+ " Season expectations: " + conf.expectations());
//yet this works just fine.
conf.setSeason("Summer");
System.out.println("What's the last season set?: " + conf.getSeason());
}
}