Does anyone know if it's possible to selected multiple days with swingx's jxdatepicker? I'm using swingx version 1.6.1 (which looks to be the latest).
Looks like there are deprecated methods in the JXMonthView class...so I was able to get it to return all the selected dates by using the getSelection method of the JXMonthView class.
For instance....
JXDatePicker picker = new JXDatePicker(System.currentTimeMillis());
final JXMonthView monthView = picker.getMonthView();
monthView.setSelectionMode(JXMonthView.SelectionMode.SINGLE_INTERVAL_SELECTION);
monthView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println((monthView.getSelection()));
}
});
Related
I have a component that extends TextField where a user can type an web address. I want that after the user type something (for example www.example.org) to change that value to something else (for exemple http://www.example.org)
I have tried this:
urlField = new TextFieldIndicatingError<String>("url", new PropertyModel<String>(this, "url"));
urlField.add(new AjaxFormComponentUpdatingBehavior("onblur") {
#Override
protected void onUpdate(AjaxRequestTarget target)
{
//url = "ABCDDEE";
urlField.getModel().setObject("AAAA");
}
});
but anything inside the onUpdate() doesn't seems to have an effect in the TextField's value.
What I'm doing wrong here?
You need to use target.add(urlField) to update it on the client side after setting its new model.
For school, I'm attempting to recreate Microsoft's Notepad program using Java's Swing. I'm working on the saving and opening of .txt files, and I'm trying to figure out a way for the program to detect when a change has been made to the document. If a change has been detected and the user chooses to open or create a new file, I want the program to prompt the user if they would like to save their changes before continuing.
My thought for this was to create a flag, called documentChanged, that would initially be false, and which would be set to true whenever a change was made to the JTextArea. To detect this change, I thought of using a TextListener as follows:
public class JNotepad implements ActionListener
{
boolean documentChanged;
JTextArea notepad;
JNotepad()
{
documentChanged = false;
notepad = new JTextArea();
notepad.addTextListener(new TextListener() {
public void textValueChanged(TextEvent te) {
documentChanged = true;
}
});
}
}
However, I learned that Java classes are unable to implement multiple interfaces at once, and I'm already using ActionListeners to implement the items of my notepad's menu bar.
My question is, is there any way to use both TextListener and ActionListener (or any other listener) simultaneously in the same class? If not, what would be my best plan of action for detecting a change in the document?
It was answer in another post. See Text Changed event in JTextArea? How to?
And also see How to Write a Document Listener (DocumentListener) in Oracle, you will see an applet example.
How does your this even compile
notepad = new JTextArea();
notepad.addTextListener(new TextListener() {
// ....
}
since TextListeners are not defined to work with JTextAreas but rather with TextAreas, a completely different beast.
You should add a DocumentListener to your JTextArea's Document.
notepad.getDocument().addDocumentListener(new DocumentListener() {
void insertUpdate(DocumentEvent e) {
documentChanged = true;
}
void removeUpdate(DocumentEvent e) {
documentChanged = true;
}
void changedUpdate(DocumentEvent e) {
documentChanged = true;
}
});
Regarding
My question is, is there any way to use both TextListeners and ActionListeners (or any other listener) simultaneously in the same class?
Use of a DocumentListener has nothing to do with ActionListeners used elsewhere in your program since their domains are orthogonal to each other, i.e., the one has absolutely nothing to do with the other.
I want to disable a specific date in my date box. I tried it with this code, but the specified date (current day) does not get disabled.
final DateBox dateBox = new DateBox();
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>() {
#Override
public void onShowRange(final ShowRangeEvent<Date> event) {
List<Date> disabledDates = new ArrayList<Date>();
disabledDates.add(new Date());
dateBox.getDatePicker().setTransientEnabledOnDates(false, disabledDates);
}
});
Is there an other way to do this?
Edit: Following the example of apanizo, the day 29.5 looks greyed out, but is clickable nevertheless.
Really sorry, I did not test the code of my last answer.
I have just tried and your code worked to me, the only thing that I did was pass all the dates that I do not want through the setTransientEnabledOnDates(false, dateToDisable); method.
Example:
public void onModuleLoad() {
final DateBox dateBox = new DateBox();
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>() {
#Override
public void onShowRange(final ShowRangeEvent<Date> dateShowRangeEvent) {
final Date today = new Date(); //30 May 3014
final Date yesterday = new Date(today.getTime()- 24*60*60*1000);
//disabling yesterday - 29 May of 2014
dateBox.getDatePicker().setTransientEnabledOnDates(false, yesterday);
}
});
RootPanel.get().add(dateBox);
}
If you want to disable the click event on a disabled date, this is a resolved and not delivered bug.
See:
https://code.google.com/p/google-web-toolkit/issues/detail?id=7876
I think in the GWT's 2.7 version it will be released, in the mean time you can patch it introducing in the line 77 of com.google.gwt.user.datepicker.client.CellGridImpl :
addDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (isActive(Cell.this)) {
setSelected(Cell.this);
}
}
}, ClickEvent.getType());
In detail here
Simply add a value changed listener on date picker to listen for any change in the date.
For example if current date is not allowed and it's selected then simply discard the changes and revert back to previous date or show a warning message as well if needed and add style for current date to look it as disabled.
Here date time format is used to check for date only, time is discarded while checking dates.
Sample code:
private Date prevDate;
...
final DateBox dateBox = new DateBox();
final DateTimeFormat dateTimeFormat=DateTimeFormat.getFormat("MM/dd/yyyy");
dateBox.getDatePicker().addValueChangeHandler(new ValueChangeHandler<Date>() {
#Override
public void onValueChange(ValueChangeEvent<Date> event) {
if (dateTimeFormat.format(event.getValue()).equals(dateTimeFormat.format(new Date()))) {
dateBox.setValue(prevDate);
// show warning message here
}else{
prevDate=event.getValue();
}
}
});
this one has been puzzling me for a few days now and I feel that I have barely been able to narrow it down.
I am using Java and have a wizard for the user to step through. One of the steps allows the user to select a start time & date and an end time & date to schedule some work. I thought I had the validation on the dates complete (so that the end date must be after the start date & start date must be after current date etc). However, my validation method only fired once focus was lost on either date TextField so if the user selected a new date and immediately clicked next, an invalid choice could continue -- bug!
The start and end date selectors are widgets which are made up of a JSpinner and a calendar dialog which pops up if button is clicked. I have attached a Document Listener to the text field of the JSpinner:
DocumentListener docListener = new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
dateChanged();
System.out.println("insertUpdate");
}
#Override
public void removeUpdate(DocumentEvent e) {
dateChanged();
System.out.println("removeUpdate");
}
#Override
public void changedUpdate(DocumentEvent e) {
dateChanged();
System.out.println("changedUpdate");
}
};
((JSpinner.DefaultEditor) jSpinner1.getEditor()).getTextField().getDocument().addDocumentListener(
docListener);
When I run this class using its own main method to test:
public static void main(String[] args) {
DateSelectorWidget test = new DateSelectorWidget();
JFrame f = new JFrame("T E S T ");
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(test, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
The DocListener fires each time and everything is fine. However this class is part of a bigger program and when it is called in it - the DocListener simply does not fire at all. An instance of the class is simply added to a panel in the wizard and yet it does not function the way it does when tested independently.
Any ideas anyone?
Thanks
My guess is that you directly or indirectly change the editor on the JSpinner after your line to retrieve, cast, get component, get model and add listener.
I am writing a Java Application for Data Entry using Eclipse and SWT. Naturally it has a great many Text objects.
What I would like to happen is that when user enters something into one field focus automatically changes to the next field.
Thanks in advance
final Text textBox = new Text(shell, SWT.NONE);
textBox.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (x.getText().length() == 1); {
x.traverse(SWT.TRAVERSE_TAB_NEXT);
}
}
});
final Text textBox = new Text(shell, SWT.NONE);
textBox.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent arg0) {
if (textBox.getText().equals("") == false) {
textBox.traverse(SWT.TRAVERSE_TAB_NEXT);
}
}});
You may also want to have a look at the VerifyListener interface. See this interesting blog post for a caveat though: http://eclipsenuggets.blogspot.com/2008/10/eclipse-bug-patterns-selfish-validation.html
I assume you want to change the focus after the field has been filled. I suggest using a DocumentListener (or whatever SWT calls it) to be notified of changes to the field's content: if it has the right number of characters, jump to the next field.