I want a listener that autoselects the entry on the JXDatePickers editor cell when same gains focus.
DatePicker.getEditor().selectAll();
doesn´t work. So i tried this :
DatePicker.getEditor().addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DatePicker. getEditor().selectAll();
}
});
}
public void focusLost(FocusEvent e) {
}
});
Any suggestions ?
Edit
just realized that you probably have stand-alone datepicker, and run your snippet: worksforme. So we'll need to dig for differences - what's your swingx/jdk version and OS?
Original
typically, the JFormattedTextField is tricky to convince being selected ;-) See
Combining JXTable with RXTable
and adapt the solution to handle JXDatePicker as well - by adding
if (editor instanceof JXDatePicker) {
LOG.info("got picker: " + editor);
invokeSelectAll(((JXDatePicker) editor).getEditor());
return;
}
Related
I'm working to build an editor. I'd like to know whether the text in JEditorPane is modified or not. How can I react to editor pane text changes?
The following code seems not work.
JEditorPane editorConfig = new JEditorPane();
editorConfig.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
System.out.println("insertUpdate called");
Dialogc.this.refreshTitle();
}
#Override
public void removeUpdate(DocumentEvent e) {
System.out.println("removeUpdate called");
Dialogc.this.refreshTitle();
}
#Override
public void changedUpdate(DocumentEvent e) {
System.out.println("changedUpdate");
Dialogc.this.refreshTitle();
}
});
Thanks very much for your feedback; the code is right for document update. The code did not work because the document of JTextArea are changed in the coming logic.
Thanks
Klaus
im looking for the line of code, which will select (highlight) the Date-text-string in the jDateChooser when it gets focused.
I read that I might have to do something like .selectAll();. but i cant get access to the textfield of the jDateChooser.
also jDateChooser.selectOnFocus(true); wont compile. NetBeans says: "cannot find symbol".
eventhough i have imported:
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.demo.DateChooserPanel;
any ideas anyone ?
Change the library jar for calendar few libraries do not have all the symbols.
You can download from here and replace it with new one and then check:
JDateChooser dateChooser = new JDateChooser(new Date());
dateChooser.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent evt) {
((JTextFieldDateEditor)evt.getSource()).selectAll();
}
});
dateChooser.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent evt) {
if (evt.getSource() instanceof JTextComponent) {
final JTextComponent textComponent=((JTextComponent)evt.getSource());
SwingUtilities.invokeLater(new Runnable(){
public void run() {
textComponent.selectAll();
}});
}
}
});
cmbCategory.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//Do things
}
});
The above code works just fine when I make selection using the control itself, but does not fire when I try changing the index programmatically.
Can anyone help me?
Maybe this could work:
cmbCategory.addModifyListener(new ModifyListener(){
#Override
public void modifyText(ModifyEvent event) {
//Do things
}
});
have tried a few different approaches to this but with no success so far. Just wondered if I'm missing anything. I have a JSpinner which is a component of a DateSelector widget alongside a Calendar. I am trying to fire a validation method if the user changes any text in the JSpinner instead of using the Calendar control or the JSpinner up and down arrows.
Here are the different approaches I have tried:
jSpinner1.addKeyListener(kl);
jSpinner1.getEditor().addKeyListener(kl);
((JSpinner.DefaultEditor) jSpinner1.getEditor().getTextField().addKeyListener(kl);
Anyone out there got any ideas as to what I'm doing wrong? Thanks
UPDATE
Apologies, I should have said that I have already added a ChangeListener to the JSpinnerDateModel which is attached to the JSpinner. Like so:
ChangeListener changeListener = new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
dateChanged();
}
};
jSpinnerDateModel.addChangeListener(changeListener);
KeyListener keyListener = new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyChar());
dateChanged();
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
};
((JSpinner.DefaultEditor) jSpinner1.getEditor()).getTextField().addKeyListener(
keyListener);
Thanks
Frank
If you want to disable keyboard editing do this:
JFormattedTextField tf = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
tf.setEditable(false);
To listen for key events you need to add a listener to the text field. This works for me:
((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addKeyListener(new KeyListener(){
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
System.out.println("PRESSED!");
}
#Override
public void keyTyped(KeyEvent e) {
}
});
JSpinners handle KeyEvents themselves, but they fire ChangeEvents to the outside world. Adding a ChangeListener should allow you to perform the validation you wish.
See also: Detecting Spinner Value Changes (Java Tutorials)
This is a shortfall of swing, and in my opinion JSpinner should follow JComboBox in supplying the following access to the underlying text field:
JComboBox.getEditor().getEditorComponent()
From going through the source of J1.7 I found you can acheive pretty much the same thing with
JSpinner.getEditor().getComponent(0)
Therefore you can "hack" the listener in the following way:
JSpinner.getEditor().getComponent(0).addKeyListener(...)
Obviously this depends on the 'under the covers' implementation of swing and works as at J1.7 but there is no guarantee this works for other versions future or past.
Enjoy.
EDIT
or if the editor is an instance of DefaultEditor, you can cast it as such and use 'getTextField()'. It would be handy if this were defined in the interface.
I have a small Java desktop app that uses Swing. There is a data entry dialog with some input fields of different types (JTextField, JComboBox, JSpinner, JFormattedTextField). When I activate the JFormattedTextFields either by tabbing through the form or by clicking it with the mouse, I would like it to select all the text that it currently contains. That way, users could just start typing and overwrite the default values.
How can I do that? I did use a FocusListener/FocusAdapter that calls selectAll() on the JFormattedTextField, but it doesn't select anything, although the FocusAdapter's focusGained() method is called (see code sample below).
private javax.swing.JFormattedTextField pricePerLiter;
// ...
pricePerLiter.setFormatterFactory(
new JFormattedTextField.AbstractFormatterFactory() {
private NumberFormatter formatter = null;
public JFormattedTextField.AbstractFormatter
getFormatter(JFormattedTextField jft) {
if (formatter == null) {
formatter = new NumberFormatter(new DecimalFormat("#0.000"));
formatter.setValueClass(Double.class);
}
return formatter;
}
});
// ...
pricePerLiter.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
pricePerLiter.selectAll();
}
});
Any ideas? The funny thing is that selecting all of its text apparently is the default behavior for both JTextField and JSpinner, at least when tabbing through the form.
Wrap your call with SwingUtilities.invokeLater so it will happen after all pending AWT events have been processed :
pricePerLiter.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
pricePerLiter.selectAll();
}
});
}
});
In addition to the above, if you want this for all text fields you can just do:
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("permanentFocusOwner", new PropertyChangeListener()
{
public void propertyChange(final PropertyChangeEvent e)
{
if (e.getNewValue() instanceof JTextField)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JTextField textField = (JTextField)e.getNewValue();
textField.selectAll();
}
});
}
}
});
Thats because the JFormattedTextfield overrides processFocusEvent to format on focus gained/focus lost.
One sure shot way is to extend JFormattedTextField and override the processFocusEvent method :
new JFormattedTextField("...") {
protected void processFocusEvent(FocusEvent e) {
super.processFocusEvent(e);
if (e.isTemporary())
return;
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
selectAll();
}
});
}
};
Using a focusListener might not always work..since it would depend on the time at which it is called relative to the processFocusEvent.
I know this is kind of old, but I came up with a cleaner solution, without invokeLater:
private class SelectAllOfFocus extends FocusAdapter {
#Override
public void focusGained(FocusEvent e) {
if (! e.isTemporary()) {
JFormattedTextField textField = (JFormattedTextField)e.getComponent();
// This is needed to put the text field in edited mode, so that its processFocusEvent doesn't
// do anything. Otherwise, it calls setValue, and the selection is lost.
textField.setText(textField.getText());
textField.selectAll();
}
}
}
The code of camickr can be slightly improved. When the focus passes from a JTextField to another kind of component (such a button), the last automatic selection does not get cleared. It can be fixed this way:
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("permanentFocusOwner", new PropertyChangeListener()
{
#Override
public void propertyChange(final PropertyChangeEvent e)
{
if (e.getOldValue() instanceof JTextField)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JTextField oldTextField = (JTextField)e.getOldValue();
oldTextField.setSelectionStart(0);
oldTextField.setSelectionEnd(0);
}
});
}
if (e.getNewValue() instanceof JTextField)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JTextField textField = (JTextField)e.getNewValue();
textField.selectAll();
}
});
}
}
});