我目前在使用JSpinners时遇到另一个问题。我想通过添加按钮通过右键单击在屏幕上动态添加JSpinners。添加它们之后,我希望将它们插入JSpinners数组中,然后将JSpinners中的数据存储到Date ArrayList中。到目前为止,我面临着这个问题:
我真的在这里迷路了。在这里感谢任何帮助。谢谢和问候
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();
}
}
答案 0 :(得分:2)
其中一个ArrayList中的空值来源:您正在向dateArray
ArrayList中添加空值
Date changedDate; // null
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// .....
dateArray.add(countAddClicks, changedDate); // changedDate is null
,您永远不会更改这些值。是的,您将一个 新Date 实例分配给了changeDate变量,但是ArrayList不保存变量,它保存的是对象。
请注意此处:
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中提取数据。
答案 1 :(得分:0)
@Hovercraft满是鳗鱼我已经解决了这个问题!我很自豪,现在一切正常。因此,它会动态添加JSpinners并将日期存储在单独的数组插槽中。如果有人搜索相似的内容,我将粘贴代码,以便他们可以将其用作参考。再次感谢
stored as specified and comparisons are case-sensitive