selecting (highlighting) the text of jdatechooser when focus gained - java

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();
}});
}
}
});

Related

How can I react to JEditorPane text changes?

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

JCalendarButton receive date

How to receive a selected date using method actionPerformed?
Do you know other useful methods?
I cannot find proper method to receive selected date.
_jCalendarButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Object[] tab = _jCalendarButton.getSelectedObjects();
}
});
The project is documented here: http://www.jbundle.org/jbundle/jcalendarbutton/quickstart.html. You must add a PropertyChangeListener on the button to be notified when the date changes. The ActionListener is only called when you press the button. Given that the project has no published javadoc, look at the source code for more information.
You will need to use a PropertyChangeListener rather than an ActionListener and read the new value from its PropertyChangeEvent
jCalendarButton.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() instanceof Date) {
System.out.println(evt.getNewValue());
}
}
});
If you want to select an date with GUI of Calendar you can use JXDatePicker (Component of SwingX). You will need an SwingX jar file to use it.
Use the following code:
JXDatePicker datePicker = new JXDatePicker(System.currentTimeMillis());
datePicker.addActionListener(new ActionListener(){
label.setText(datePicker.getDate().toString());
});

How to delete text when a user clicks a JTextField?

In my program, the user writes something in a JTextField then clicks a 'generate' button, which triggers the characters in the JTextField to be drawn to a JPanel.
I would then like to clear all the text in the JTextField when the user clicks the JTextField again. I tried to achieve this by adding a FocusListener and an ActionListener to the JTextField, however my attempts did not work. Moreover, my implementation of the FocusListener gave an Unreachable Statement compiler error.
Is this possible to do in Java and if so how can I do this?
The code below is my ActionListener implementation.
dfaText = new JTextField(6);
dfaText.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
generateLabel.setText("NOOOOO!!!");
dfaText.setText("");
isDfaDrawn = false;
canDraw = false;
repaint();
}
});
Add a mouse listener:
field.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
field.setText("");
}
});
Bear in mind this could get frustrating if the user legitimately clicks elsewhere and returns to the field. You may wish to maintain some state, e.g. only clear the field if the button has been clicked in the interim.
You can do this:
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField.setText("");
}
});
Just an addition to others' code.
public void textfieldMouseClicked(java.awt.event.MouseEvent evt) {
uname_tf.setText(null);
} //This will set the JTextfield blank on mouse click//
public void textfieldFocusLost(java.awt.event.FocusEvent evt) {
uname_tf.setText("Username");
} //This will bring back the default text when not in focus//
Hope it helps, Cheers!!!
If you want it just one Click on it to delete the text you can do like this :
textField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
textFieldMousePressed(evt);
}
});
private void textFieldMousePressed(java.awt.event.MouseEvent evt) {
textField.setText("");
}
Add 1 line to you buttonMethod e.g:
txtfield.clear();
or set your txtfield to an empty string

JXDatePicker and selectAll()

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;
}

How to select all text in a JFormattedTextField when it gets focus?

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();
}
});
}
}
});

Categories