javaswingjspinner

Getting data from JSpinner and store it into an Array


I am currently having another problem, this time with JSpinners. I want to add dynamically JSpinners on the screen by right click, via the add button. After I add them, I want them to be inserted in an array of JSpinners and then the data from the JSpinners to be stored into a Date ArrayList. So far I am facing this problem:


I am really lost here. Appreciate any help here. Thanks and best regards

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.event.*;
import java.text.SimpleDateFormat;

public class TestingGround
{
    Toolkit toolkit;
    JFrame frame;
    JPopupMenu menu;

    Calendar calendar1, calendar2;
    Date startDate, saveThisDate;
    String stringStoredInitialRequestDate;
    ArrayList <Date> dateArray; // array to store the dates from the request date
    JSpinner spinner;
    ArrayList <JSpinner> spinnerArray;
    SpinnerModel model1;


    int countAddClicks;
    int val1;

    public TestingGround()
    {
        frame = new JFrame("Testing ground area");

        centerToScreen();

        menu = new JPopupMenu();
        JMenuItem addRow = new JMenuItem("Add ComboBox");
        JMenuItem removeRow = new JMenuItem("Remove ComboBox");
        JPanel panel = new JPanel();
        JPanel mainGridPanel = new JPanel(); 

        mainGridPanel.setLayout(new GridLayout(0,2));
        mainGridPanel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.add(mainGridPanel);
        // -------------------------------------------

        dateArray = new <Date> ArrayList(); // array used to store the initial request dates
        spinnerArray = new <JSpinner> ArrayList();

        JButton saveDataButton = new JButton("save state");
        countAddClicks =0;
        val1 = -1;
        panel.add(saveDataButton);

        // ACTION LISTENERS
        addRow.addActionListener(new ActionListener(){ // Right click to add JComboBoxes to the screen
                public void actionPerformed(ActionEvent event) {
                    System.out.println("Starting click is: " + countAddClicks);

                    // JSPINNER 1
                    calendar1 = Calendar.getInstance();
                    Date now = calendar1.getTime();
                    calendar1.add(Calendar.YEAR, -10);

                    Date startDate = calendar1.getTime();
                    calendar1.add(Calendar.YEAR, 20);

                    Date endDate = calendar1.getTime();

                    model1  = new SpinnerDateModel(now, startDate, endDate, Calendar.YEAR);

                    spinner  = new JSpinner(model1); // creating Visual Spinner 
                    String format = "dd MMM yy"; // formatting Visual Spinner 1

                    JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, format);
                    spinner.setEditor(editor); // applying the formatting to the visual spinner  

                    spinnerArray.add(countAddClicks,spinner); // adds the spinner to the array of spinners

                    /*
                    model1.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent e) {
                    changedDate = ((SpinnerDateModel) e.getSource()).getDate();

                    }
                    });
                     */
                    saveThisDate = now;
                    dateArray.add(countAddClicks, saveThisDate); // add to the DATE array the new date (in the correct slot, going side by side with the other array);   
                    countAddClicks++;

                    System.out.println("After click is: " + countAddClicks);         

                    mainGridPanel.add(spinner); // add the JTextField to the JPanel
                    mainGridPanel.repaint();
                    mainGridPanel.revalidate();

                }
            });

        removeRow.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent event) {
                    countAddClicks--;  

                    if (countAddClicks <0) {
                        countAddClicks = 0;
                    }

                    if (spinnerArray.size() > 0 & dateArray.size() >0 ) {

                        spinner = spinnerArray.remove(spinnerArray.size()-1);
                        mainGridPanel.remove(spinner);
                    }

                    System.out.println("After removal click is: " + countAddClicks);
                    mainGridPanel.revalidate();      
                    mainGridPanel.repaint();

                }
            });

        saveDataButton.addActionListener (new ActionListener() {
                public void actionPerformed(ActionEvent e) {


                   dateArray.clear();  

                    for (int i=0; i< spinnerArray.size(); i++) {

                        JSpinner tempSpinner = spinnerArray.get(i); // creating a temporary spinner

                        calendar2 = Calendar.getInstance(); // creating a temporary calendar

                        Date now2 = calendar2.getTime(); 
                        calendar1.add(Calendar.YEAR, -50);

                        Date startDate2 = calendar2.getTime();
                        calendar2.add(Calendar.YEAR, 50);

                        Date endDate2 = calendar2.getTime();

                        SpinnerModel model2  = new SpinnerDateModel(now2, startDate2, endDate2, Calendar.YEAR);

                        tempSpinner.setModel(model2); // setting up a temporary spinnerModel and adding it to this instance of JSpinner


                        model2.addChangeListener(new ChangeListener() { // adding a listener to this model2
                                public void stateChanged(ChangeEvent e) {
                                    saveThisDate = ((SpinnerDateModel) e.getSource()).getDate(); // checking for user input
                                    System.out.println(saveThisDate); // for testing purpose it is showing that it detects the change
                                }

                            });    




                          dateArray.add(saveThisDate); // add to the DATE array the new date (in the correct slot, going side by side with the other array);   

                        }



                    // Only for checking purpose if the arrays are the correct sizes
                    System.out.println();
                    System.out.println("The size of the JSpinner Array is: " + spinnerArray.size() ); // showing correct
                    System.out.println("The content of the Date Array is: " + dateArray ); // checking the Date array. This is where data is not added, it is not working!
                    System.out.println("The size of the Date Array is: " + dateArray.size()); // showing correct
                    System.out.println();
                }

            });
        // Cand dau click din butonul cel mai din dreapta (3) se deschide menium popup 
        frame.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent event) {
                    if (event.getButton() == event.BUTTON3) {
                        menu.show(event.getComponent(), event.getX(),event.getY());
                    }
                } 
            }); 

        menu.add(addRow);
        menu.add(removeRow);

        frame.add(panel);

        frame.setVisible(true);
    }

    public void centerToScreen()
    {
        frame.setSize(700,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("A Popup Menu");

        toolkit = frame.getToolkit();
        Dimension size = toolkit.getScreenSize();
        frame.setLocation((size.width-frame.getWidth())/2, (size.height-frame.getHeight())/2);

    }

    public static void main(String[]args){
        new TestingGround();
    }
}

Solution

  • The source of the nulls in one of the ArrayLists: You're adding null values to the dateArray ArrayList

        Date changedDate; // null
    
    
        addRow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                // .....
                dateArray.add(countAddClicks, changedDate);  // changedDate is null
    

    and you never change these values. Yes you assign a new Date instance to the changedDate variable, but the ArrayList is not holding variables, it's holding objects.

    Note that here:

        saveDataButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dateArray.clear();
                for (int i = 0; i < spinnerArray.size(); i++) {
                    //.....
                    model2.addChangeListener(new ChangeListener() {
                        public void stateChanged(ChangeEvent e) {
                            changedDate = ((SpinnerDateModel) e.getSource()).getDate();  // **** (A) ****
                            System.out.println(changedDate);
                        }
                    });
                    // ...
                }
            }
        });
    

    again you change the Date instance held by the dateArray variable, but again, this has no effect on the nulls held by the ArrayList.

    I suggest that you get rid of the dateArray ArrayList and instead extract the data from the JSpinners when needed (in a listener).