简体   繁体   English

如何设置jSpinner的默认值?

[英]how to set jSpinner default value?

I'm using a jSpinner to set the time with format "HH:MM" and it works fine. 我正在使用jSpinner以格式“HH:MM”设置时间,它工作正常。

My problem is I want to set my jSpinner to "00:00" when the window is activated, and once jSpinner is clicked I want the value to be changed to the "HH:MM" format. 我的问题是我想在激活窗口时将我的jSpinner设置为“00:00”,并且一旦点击了jSpinner,我希望将值更改为“HH:MM”格式。 And I can't do it myself, the jSpinner value doesn't change it is still "00:00". 我不能自己做,jSpinner值不会改变它仍然是“00:00”。 Please help me. 请帮我。 Thank you in advance. 先感谢您。

Assuming that your are using a SpinnerDateModel , then the value that the JSpinner is managing is java.util.Date . 假设您使用的是SpinnerDateModel ,那么JSpinner管理的值是java.util.Date You need to create a Date instance which has it's hour/minute fields set to midnight 您需要创建一个Date实例,将其小时/分钟字段设置为午夜

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);

Date date = cal.getTime();
spinner.setValue(date);

To get the format the value from the JSpinner , you can use a SimpleDateFormat 要从JSpinner获取格式值,可以使用SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String value = sdf.format(spinner.getValue());

but how can I set it with "00:00" when the window is activated 但是当窗口激活时,如何用“00:00”设置它

Without more information, I'd suggest using a WindowListener of some kind, assuming that you're not creating the window each time. 没有更多信息,我建议使用某种类型的WindowListener ,假设您不是每次都创建窗口。

Have a look at How to Write a Focus Listener and How to Write Window Listeners 看看如何编写焦点监听器以及如何编写窗口监听器

And changed the value once clicked? 点击后改变了价值?

That's a little unclear, but you should just get the value from the JSpinner at the time you need to know what it is and format based on your needs. 这有点不清楚,但是当你需要知道它是什么时,你应该从JSpinner获得价值,并根据你的需要进行格式化。 The JSpinner should be taking care of formatting the value automatically (based on your settings) when the values is updated. 更新值时, JSpinner应自动(根据您的设置)设置值的格式。

Having said that, I did need to make the "start" (or lowest value) null when I set up the model, which seemed to allow the spinner to update the time value 话虽如此,我确实需要在设置模型时使“start”(或最低值)为null ,这似乎允许微调器更新时间值

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);

Date startTime = cal.getTime();
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
Date endTime = cal.getTime();

System.out.println(startTime);
System.out.println(endTime);

// The Calendar field seems to get ignored and overriden 
// by the UI delegate based on what part of the field
// the cursor is current on (hour or minute)
SpinnerDateModel model = new SpinnerDateModel(startTime, null, endTime, Calendar.MINUTE);
JSpinner spinner = new JSpinner(model);
spinner.setEditor(new JSpinner.DateEditor(spinner, "HH:mm"));

spinner.setValue(startTime);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM