[#1506] Highlight spinner text after focus gained
This commit is contained in:
parent
54f8276c53
commit
b1483ae03a
@ -1,8 +1,14 @@
|
||||
package net.sf.openrocket.gui;
|
||||
|
||||
import javax.swing.JSpinner;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.text.DefaultFormatter;
|
||||
import javax.swing.text.DefaultFormatterFactory;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
|
||||
/**
|
||||
* Editable editor for a JSpinner. Simply uses JSpinner.DefaultEditor, which has been made
|
||||
@ -22,6 +28,59 @@ public class SpinnerEditor extends JSpinner.DefaultEditor {
|
||||
DefaultFormatterFactory dff = (DefaultFormatterFactory) getTextField().getFormatterFactory();
|
||||
DefaultFormatter formatter = (DefaultFormatter) dff.getDefaultFormatter();
|
||||
formatter.setOverwriteMode(false);
|
||||
|
||||
|
||||
// Add listeners to select all the text when the field is focussed
|
||||
{
|
||||
getTextField().addFocusListener(new FocusListener() {
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
selectAllText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost(FocusEvent e) {
|
||||
}
|
||||
});
|
||||
|
||||
getTextField().addMouseListener(new MouseListener() {
|
||||
private boolean isFocussed = false; // Checks whether the text field was focussed when it was clicked upon
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// If the text field was focussed when it was clicked upon instead of e.g. tab-switching to gain focus,
|
||||
// then the select all action from the focus listener is ignored (it is replaced by a cursor-click event).
|
||||
// So if we detect such a focus change, then redo the select all action.
|
||||
if (!isFocussed) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JTextField tf = (JTextField) e.getSource();
|
||||
tf.selectAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
JTextField tf = (JTextField) e.getSource();
|
||||
isFocussed = tf.hasFocus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,5 +92,17 @@ public class SpinnerEditor extends JSpinner.DefaultEditor {
|
||||
this(spinner);
|
||||
getTextField().setColumns(cols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlights all the text in the text field.
|
||||
*/
|
||||
private void selectAllText() {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getTextField().selectAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user