Is it possible to change to change the format in which DataTime widget diplays date? The matter is that even though I set the locale to one that uses European format (dd/mm/yyyy) I still have DateTime widget in mm/dd/yyyy format.
Edit: There have been similat questions on SO along the lines "How to change the format of DateTime" and they were answered by something like "change the locale and DateTime will adapt". My question is about the situation when changing the locale doesn't have any effect on DateTime widget. What might be wrong? Is there any other solution to force format change?
After running some tests on Linux, I can confirm that the DateTime widget does not appear to be using the OS's locale. This seems to be a bug and you should report it.
What you can do in the meantime is use Nebula's CDateTime which supports Java Locales:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Locale.setDefault(Locale.GERMAN);
CDateTime cdt = new CDateTime(shell, CDT.DATE_SHORT);
cdt.setSelection(new Date());
Locale.setDefault(Locale.ENGLISH);
cdt = new CDateTime(shell, CDT.DATE_SHORT);
cdt.setSelection(new Date());
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Related
After reloading this effect disappears and labels are in their suitable sizes. This repeats only if the application is reloaded. On English locale, this dialog shows normally.
Example of code containing problem label:
private void createDailyGroup(Composite composite)
{
m_typePanels[DAILY] = new Composite(composite, SWT.NONE);
Composite panel = m_typePanels[DAILY];
panel.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).create());
panel.setLayoutData(GridDataFactory.fillDefaults().create());
addRunTime(panel, DAILY);
}
private void addRunTime(Composite panel, int scheduleType)
{
Label runTimeLabel = new Label(panel, SWT.NONE);
runTimeLabel.setText(QmfResources.getString(IDS_RUNTIME_LABEL));
runTimeLabel.setLayoutData(GridDataFactory.fillDefaults().
align(SWT.BEGINNING, SWT.CENTER).grab(false, false).create());
m_runTimes.put(Integer.valueOf(scheduleType), new DateTime(panel, SWT.TIME | SWT.BORDER | SWT.SHORT));
DateTime runTime = m_runTimes.get(Integer.valueOf(scheduleType));
runTime.addSelectionListener(m_parametersChangeListener);
runTime.setLayoutData(GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).create());
}
Try below code, here I am considering runTimeLabel is label where you want to add Chinese locale string
runTimeLabel.getParent().requestLayout();
runTimeLabel.getParent().redraw();
runTimeLabel.getParent().getParent().update();
I am trying to use JDatePicker to display a calendar. However, I only want to display months and years, not the days.
I tried poking around the model and JDatePickerImpl objects, without luck.
Here is the code I have to show the JDatePicker, from documentation:
UtilCalendarModel model = new UtilCalendarModel();
Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateComponentFormatter());
Thanks in advance!
Note: Here is an image of what I mean.
After looking at the code from the JDatePicker project, I think this is possible by making a custom version of the JDatePanelImpl class (and perhaps some other classes). The functionality you want is not yet configurable in the standard classes, but it could be implemented as an enhancement and sent to the JDatePicker developers as a proposal (pull request).
Just to be sure of what you need for your application: you want to use a calendar similar to the example below? The user could change the selected month & year by clicking the next/previous month buttons, selecting a month from the month popup menu, or selecting a different year (using the year spinner):
Edit: added example with adapted versions of JDatePicker classes
I have added a modified example of your code and two adapted versions of JDatePicker classes. The normal component closes the popup when the user clicks a specific day, which is not possible in this case (since the days are hidden). I have added a small OK button to make it possible to close the date picker (see screenshot above). This is clearly a proof of concept only; the code really needs more work.
(Note: when I tried to add the two modified classes, my answer became to big. Therefore I forked the JDatePicker project on GitHub, rewrote the customizations from JDatePicker version 1.3.4 to version 1.3.4.1, and added links for these two files instead of all the code.)
// ExampleDatePickerWithoutDay class:
import java.text.*;
import javax.swing.*;
import org.jdatepicker.*;
public class ExampleDatePickerWithoutDay {
public static void main(String[] arguments) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ExampleDatePickerWithoutDay().createAndShowGui();
}
});
}
private void createAndShowGui() {
JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 800, 200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// Set two date formats and a text label.
DateFormat dateFormat = new SimpleDateFormat("MMMM yyyy");
ComponentFormatDefaults.Key formatKey;
formatKey = ComponentFormatDefaults.Key.SELECTED_DATE_FIELD;
ComponentFormatDefaults.getInstance().setFormat(formatKey, dateFormat);
formatKey = ComponentFormatDefaults.Key.TODAY_SELECTOR;
ComponentFormatDefaults.getInstance().setFormat(formatKey, dateFormat);
ComponentTextDefaults.Key textKey = ComponentTextDefaults.Key.TODAY;
ComponentTextDefaults.getInstance().setText(textKey, "Current month");
// Create the date picker.
UtilCalendarModel calendarModel = new UtilCalendarModel();
CustomDatePanel datePanel = new CustomDatePanel(calendarModel);
CustomDatePicker datePicker = new CustomDatePicker(datePanel);
panel.add(datePicker);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
CustomDatePanel class: CustomDatePanel.java (on GitHub)
CustomDatePicker class: CustomDatePicker.java (on GitHub)
I want to use this JXDatePicker component in a application that will work on a touch display. Because the default component is small, all the dates and the buttons are hard to click using a bad touch screen I wanted to make them bigger. So far I successfully made the result text field bigger (the one showing the selected date, by changing the font), make the pop-up bigger (the JXMonthView, also by changing its font), change the picture of the JXDatePicker with a bigger image, set the default date to be the current date, set the date format, etc. This is my code:
private void touch_screen_datepicker(JXDatePicker date_picker) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
JXMonthView monthView = date_picker.getMonthView();
date_picker.setDate(new Date());
date_picker.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
JButton btn_pick = (JButton) date_picker.getComponent(1);
btn_pick.setBackground(new Color(66, 147, 223));
Image image = toolkit.getImage("/home/adrrian/Image/calendar/" + "calendar image 4.png"); //Земање на сликата за мк знаме
ImageIcon icon = new ImageIcon(image); //Правење на икона
btn_pick.setIcon(icon); //Поставување на иконата
SimpleDateFormat longFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat shortFormat = new SimpleDateFormat("yy-MM-dd");
Date startDate = new Date(0);//01.01.1970
shortFormat.set2DigitYearStart(startDate);
DatePickerFormatter formatter = new DatePickerFormatter(
// invers sequence for parsing to satisfy the year parsing rules
new DateFormat[]{shortFormat, longFormat}) {
#Override
public String valueToString(Object value) throws ParseException {
if (value == null) {
return null;
}
return getFormats()[1].format(value);
}
};
DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
date_picker.getEditor().setFormatterFactory(factory);
monthView.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
monthView.setFirstDayOfWeek(Calendar.MONDAY);
}
and this is an image of my final work:
My main problem is how to make the arrow that are changing the months (for example if I go back from this image to show September). I tried listing all of the components, like I did for the button, but still I didn't found anything. Also for better GUI I like to find that dark blue color (where the month is displayed), to make my button the same.
Hope someone can help me. Thanks in advance.
Toolkit toolkit = Toolkit.getDefaultToolkit();
JXMonthView monthView = date_picker.getMonthView();
/*EDITED*/
//1
date_picker.putClientProperty("JComponent.sizeVariant", "large");
monthView.putClientProperty("JComponent.sizeVariant", "large");
//2
date_picker.putClientProperty("JXComponent.sizeVariant", "large");
monthView.putClientProperty("JXComponent.sizeVariant", "large");
//3
date_picker.putClientProperty("JXDatePicker.sizeVariant", "large");
monthView.putClientProperty("JXMonthView.sizeVariant", "large");
//
date_picker.putClientProperty("JDatePicker.sizeVariant", "large");
monthView.putClientProperty("JMonthView.sizeVariant", "large");
SwingUtilities.updateComponentTreeUI(this);
SwingUtilities.updateComponentTreeUI(date_picker);
SwingUtilities.updateComponentTreeUI(monthView);
date_picker.updateUI();
monthView.updateUI();
/*EDITED*/
As #Vighanesh Gursale suggested I insterdet this lines and also did the frame.pack() before setVisible(true), but nothing changes.
I have made some code using nimbus look and feel, i know it is not exact that you want but it is pretty much helpful. Check this code. To perform same you need to find the key of your next and previous buttons key in my case it is Button.margin. Try to use the same key in your code if you are lucky it would work.
import javax.swing.*;
import java.awt.*;
public class Demo {
JFrame frame = new JFrame("");
JButton btn = new JButton("Example");
public Demo() {
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(btn);
frame.setVisible(true);
}
public static void main(String[] args) {
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
Insets insets = new Insets(50, 20, 50, 20); //change the size of button
UIManager.put("Button.margin", insets);
}
catch(Exception e)
{
e.printStackTrace();
}
Demo d = new Demo();
}
}
Also keep in mind to use default look and feel or any other look and feel.
This question is quite old but for everyone who is looking how it can be done
UIManager.put("JXMonthView.monthDownFileName", <PATH_TO_IMAGE>);
UIManager.put("JXMonthView.monthUpFileName", <PATH_TO_IMAGE>);
monthDownFileName refers to previous month
monthUpFileName refers to next month
this will change the image with other of your choice (and size)
I'm using DynamicReports API for building reports.
I'm setting the Locale of the report and format the Date columns of the report, but the Dates are always formatted like 10/12/2009 10:54:44 AM no matter what the Locale is.
The code looks like:
rep.setTemplate(Templates.reportTemplate.setLocale(res.getLocale()));
...
if (rs.getString(i).contains("00:00:00"))
rep.addColumn(col.column(title, name, type.dateType()));
else
rep.addColumn(col.column(title, name, type.dateYearToSecondType()));
Is there a way to automatically format dates regarding to the Locale of the report or have I to use a custom ValueFormatter?
I also tryed the Parameter Map with no success
JasperReportBuilder rep = report()
.setDataSource(query, conn.getConnection())
.setParameter(JRParameter.REPORT_LOCALE, res.getLocale());
.setTemplate(Templates.reportTemplate.setLocale(res.getLocale()));
There is no way to automatically format the dates.
The only way is to use patterns for the date column in respect to the locale.
TextColumnBuilder<Date> column = col.column(title, name, type.dateType());
if (res.getLocale().equals("EN") {
column.setPattern("dd.MM.yyyy");
}
else if (res.getLocale().equals("US") {
column.setPattern("MM/dd/yyyy");
}
else {
...
}
rep.addColumn(column);
I managed to format the date field in dynamic jasper in this way.
ColumnBuilder time = ColumnBuilder.getNew();
time.setTitle("Login Time");
time.setWidth(200);
time.setColumnProperty("emp.logintime", Date.class.getName()).setPattern("dd/MM/yyyy hh:mm:ss a");
drb.addColumn(time.build());
I'm using a SWT DateTime component. It sets the current date as a default selection, when instanciated. How can i prevent this?
I want that no date is selected at all...
Thanks
Patrick
The SWT DateTime control does not support this at all.
I recommend CalendarCombo from the Eclipse Nebula project.
If this is still of use to anyone - I had the same problem, meaning that a field on the UI had to show a date or an empty value: because a date that was NOT selected was also valid input.
While SWT DateTime has to show some sort of a date, it is not a problem at all introducing another level of indirection by simply making a label and a button - too look like DateTime: The then button calls DateTime in a separate modal window. After the user has made the selection, we write the result to the label back in the application window. You also add another button to the modal window and call it e.g. NONE. If the user clicks NONE, you clear the label field in your application.
You will see that I scrape the current value of the date from the label first, so that I can initialize the DateTime control in the modal dialog. This way it all behaves like a new composite control, though I admit it is a bit awkward if you need to do it many times over. E.g.:
private Button buttonDeadlineDate;
private Label labelDeadlineDate;
// ... then define your "composite" control:
lblNewLabel_5 = new Label(group_2, SWT.NONE);
lblNewLabel_5.setBounds(10, 14, 50, 17);
lblNewLabel_5.setText("Deadline:");
// We make our own composite date control out of a label and a button
// and we call a modal dialog box with the SWT DateTime and
// some buttons.
labelDeadlineDate = new Label(group_2, SWT.BORDER | SWT.CENTER);
labelDeadlineDate.setBounds(62, 10, 76, 20);
// Note that I use the strange font DokChampa because this was the only way to get a margin at the top.
labelDeadlineDate.setFont(SWTResourceManager.getFont("DokChampa", 8, SWT.NORMAL));
labelDeadlineDate.setBackground(SWTResourceManager.getColor(255, 255, 255)); // so it does appear editable
buttonDeadlineDate = new Button (group_2, SWT.NONE);
buttonDeadlineDate.setBounds(136, 11, 20, 20); // x - add 74, y - add 1 with respect to label
// ... And later we have the call-back from the listener on the little button above:
//========================================
// Deadline Date
//========================================
buttonDeadlineDate.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// Define the dialog shell.
// Note: DIALOG_TRIM = TITLE | CLOSE | BORDER (a typical application dialog shell)
final Shell dialog = new Shell (shlTaskScheduler, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
dialog.setText("Enter deadline date (NONE for none)");
//========================================
// Position and size the dialog (relative to the application).
// could have probably also used a single call to dialog.setBounds()
// instead of calling setLocation() and setSize().
//========================================
Point myPoint = new Point(0,0);
myPoint = shlTaskScheduler.getLocation();
myPoint.x +=80; // myPoint.x +=30;
myPoint.y +=320; // myPoint.y +=350;
dialog.setLocation(myPoint);
dialog.setSize(270, 220);
dialog.setLayout (null);
//========================================
// Define dialog contents
//========================================
// Make controls final they it can be accessed from the listener.
final DateTime DTDeadlineDate;
DTDeadlineDate = new DateTime(dialog, SWT.BORDER | SWT.CALENDAR | SWT.DROP_DOWN);
DTDeadlineDate.setBounds(10, 10, 175, 175);
final Button buttonNone = new Button (dialog, SWT.PUSH);
buttonNone.setText ("NONE");
buttonNone.setBounds(200, 35, 55, 25);
final Button buttonOK = new Button (dialog, SWT.PUSH);
buttonOK.setText ("OK");
buttonOK.setBounds(200, 85, 55, 25);
//========================================
// Initialize the DateTime control to
// the date displayed on the button or today's date.
//========================================
// Get the deadline from the main application window
String newDeadlineDateString = (labelDeadlineDate.getText().toString());
myLogger.i (className, "got deadline from main application window as " + newDeadlineDateString);
// If deadline date found, use it to initialize the DateTime control
// else the DateTime control will initialize itself to the current date automatically.
if ((newDeadlineDateString.length() == 10) // probably unnecessary test
&& (isThisDateValid(newDeadlineDateString, "yyyy-MM-dd"))) {
// parse and extract components
try {
String tmpYearString= newDeadlineDateString.substring(0,4);
String tmpMoString = newDeadlineDateString.substring(5,7);
String tmpDayString = newDeadlineDateString.substring(8,10);
int tmpYearInt = Integer.parseInt(tmpYearString);
int tmpMoInt = Integer.parseInt(tmpMoString);
int tmpDayInt = Integer.parseInt(tmpDayString);
DTDeadlineDate.setYear(tmpYearInt);
DTDeadlineDate.setMonth(tmpMoInt - 1); // the control counts the months beginning with 0! - like the calendar
DTDeadlineDate.setDay(tmpDayInt);
} catch(NumberFormatException f) {
// this should not happen because we have a legal date
myScreenMessage.e(className, "Error extracting deadline date from screen <" + newDeadlineDateString + ">. Ignoring");
}
} else if (newDeadlineDateString.length() > 0) {
myLogger.w (className, "Illegal current deadline date value or format <" + newDeadlineDateString + ">. Ignoring.");
// no need to do anything, as the control will initialize itself to the current date
} else {
// no need to do anything, as the control will initialize itself to the current date
}
//========================================
// Set up the listener and assign it to the OK and None buttons.
// Note that the dialog has not been opened yet, but this seems OK.
//
// Note that we define a generic listener and then associate it with a control.
// Thus we need to check in the listener, which control we happen to be in.
// This is a valid way of doing it, as an alternative to using
// addListener() or
// addSelectionListener()
// for specific controls.
//========================================
Listener listener = new Listener () {
public void handleEvent (Event event) {
if (event.widget == buttonOK) {
int newDeadlineDay = DTDeadlineDate.getDay();
int newDeadlineMonth = DTDeadlineDate.getMonth() + 1; // the returned month will start at 0
int newDeadlineYear = DTDeadlineDate.getYear();
String selectedDeadlineDate = String.format ("%04d-%02d-%02d", newDeadlineYear, newDeadlineMonth, newDeadlineDay);
if (isThisDateValid(selectedDeadlineDate, "yyyy-MM-dd")) {
labelDeadlineDate.setText(selectedDeadlineDate);
} else {
// This is strange as the widget should only return valid dates...
myScreenMessage.e(className, "Illegal deadline date selected: resetting to empty date");
labelDeadlineDate.setText("");
}
} else if (event.widget == buttonNone) {
// an empty date is also an important value
labelDeadlineDate.setText("");
} else {
// this should not happen as there are no other buttons on the dialog
myLogger.e(className, "Unexpected widget state: ignoring");
}
// once a button is pressed, we close the dialog
dialog.close ();
}
};
// Still need to assign the listener to the buttons
buttonOK.addListener (SWT.Selection, listener);
buttonNone.addListener (SWT.Selection, listener);
//========================================
// Display the date dialog.
//========================================
dialog.open ();
//========================================
// If you need to do this - you can wait for user selection before returning from this listener.
// Note that this wait is not necessary so that the above button listeners
// can capture events, but rather so that we do not continue execution and end this
// function call before the user has made a date selection clicked on a button.
// Otherwise we would just go on.
while (!dialog.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
...
}
});
You would have to manually set the fields of the instance to 0 or null whatever is appropriate. You could also implement your own NoDateTime object (using the null object pattern) to accomplish the same thing. I would be tempted to represent no time with just null though, is there a reason why you cannot do that?
One Way is to set a selection listener like in this example on Eclipse-DOC
Here is an adapted version:
private boolean isModified = false;
selectDate = new DateTime(this, SWT.DATE | SWT.DROP_DOWN);
SelectionListener selListener = new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
isModified = true;
}
};
selectDate.addSelectionListener(selListener);
Then you can do an If isModified where needed.