从 JSpinner 获取数据并将其存储到数组中
Getting data from JSpinner and store it into an Array
我目前遇到了另一个问题,这次是 JSpinners。我想通过右键单击添加按钮在屏幕上动态添加 JSpinners。添加它们之后,我希望将它们插入到 JSpinners 数组中,然后将来自 JSpinners 的数据存储到 Date ArrayList 中。到目前为止我面临这个问题:
- 如果我添加一个新的 JSpinner,Date 数组不会自动从第二个 JSpinner 或第三个等处获取日期。它只从第一个 JSpinner 获取数据。
我真的迷路了。感谢这里的任何帮助。谢谢并致以最诚挚的问候
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();
}
}
其中一个 ArrayList 中空值的来源:您正在向 dateArray
ArrayList
添加空值
Date changedDate; // null
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// .....
dateArray.add(countAddClicks, changedDate); // changedDate is null
而且您永远不会更改这些值。是的,您将 new Date 实例分配给 changedDate 变量,但 ArrayList 不包含变量,它包含 objects。
注意这里:
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);
}
});
// ...
}
}
});
您再次更改了 dateArray 变量持有的 Date 实例,但这同样对 ArrayList 持有的空值没有影响。
我建议您摆脱 dateArray ArrayList,而是在需要时(在侦听器中)从 JSpinners 中提取数据。
@Hovercraft Full Of Eels 我已经解决了这个问题!!我很自豪,现在一切正常。因此它动态添加 JSpinners 并将日期存储在各个数组槽中。如果有人会搜索类似的东西,我会粘贴代码,以便他们可以将其用作参考。再次感谢
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;
ArrayList<Date> dateArray; // array to store the dates from the request date
JSpinner spinner;
ArrayList<JSpinner> spinnerArray;
SpinnerModel model1;
JPanel mainGridPanel;
int countAddClicks;
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();
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;
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
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);
dateArray.remove(dateArray.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 = new JSpinner(); // creating a
// temporary spinner
calendar2 = Calendar.getInstance(); // creating a temporary
// calendar
Date now2 = calendar2.getTime();
calendar2.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
}
});
saveThisDate = (Date) spinnerArray.get(i).getValue();
System.out.println("Content of the Spinner Array is: " + spinnerArray.get(i).getValue());
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();
}
}
我目前遇到了另一个问题,这次是 JSpinners。我想通过右键单击添加按钮在屏幕上动态添加 JSpinners。添加它们之后,我希望将它们插入到 JSpinners 数组中,然后将来自 JSpinners 的数据存储到 Date ArrayList 中。到目前为止我面临这个问题:
- 如果我添加一个新的 JSpinner,Date 数组不会自动从第二个 JSpinner 或第三个等处获取日期。它只从第一个 JSpinner 获取数据。
我真的迷路了。感谢这里的任何帮助。谢谢并致以最诚挚的问候
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();
}
}
其中一个 ArrayList 中空值的来源:您正在向 dateArray
ArrayList
Date changedDate; // null
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// .....
dateArray.add(countAddClicks, changedDate); // changedDate is null
而且您永远不会更改这些值。是的,您将 new Date 实例分配给 changedDate 变量,但 ArrayList 不包含变量,它包含 objects。
注意这里:
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);
}
});
// ...
}
}
});
您再次更改了 dateArray 变量持有的 Date 实例,但这同样对 ArrayList 持有的空值没有影响。
我建议您摆脱 dateArray ArrayList,而是在需要时(在侦听器中)从 JSpinners 中提取数据。
@Hovercraft Full Of Eels 我已经解决了这个问题!!我很自豪,现在一切正常。因此它动态添加 JSpinners 并将日期存储在各个数组槽中。如果有人会搜索类似的东西,我会粘贴代码,以便他们可以将其用作参考。再次感谢
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;
ArrayList<Date> dateArray; // array to store the dates from the request date
JSpinner spinner;
ArrayList<JSpinner> spinnerArray;
SpinnerModel model1;
JPanel mainGridPanel;
int countAddClicks;
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();
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;
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
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);
dateArray.remove(dateArray.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 = new JSpinner(); // creating a
// temporary spinner
calendar2 = Calendar.getInstance(); // creating a temporary
// calendar
Date now2 = calendar2.getTime();
calendar2.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
}
});
saveThisDate = (Date) spinnerArray.get(i).getValue();
System.out.println("Content of the Spinner Array is: " + spinnerArray.get(i).getValue());
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();
}
}