1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| import java.awt.*;
import javax.swing.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
// par Nicolas_75
// mais très fortement inspiré de http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
// en réponse à http://www.developpez.net/forums/showthread.php?t=171309
// dimanche 25 juin 2006
public class Exemple056_JFormattedTextField extends JPanel
implements PropertyChangeListener {
private double nombre = 46;
//Labels to identify the fields
private JLabel nombreLabel;
private JLabel resultatLabel;
//Strings for the labels
private static String nombreString = "nombre choisi par l'utilisateur : ";
private static String resultatString = "double : ";
//Fields for data entry
private JFormattedTextField nombreField;
private JFormattedTextField resultatField;
//Formats to format and parse numbers
private NumberFormat nombreFormat = NumberFormat.getNumberInstance();
private NumberFormat resultatFormat = NumberFormat.getCurrencyInstance();
public Exemple056_JFormattedTextField() {
super(new BorderLayout());
double resultat = 2*nombre;
//Create the labels.
nombreLabel = new JLabel(nombreString);
resultatLabel = new JLabel(resultatString);
// l'utilisateur ne peut rentrer qu'un entier à 2 chiffres maxi :
nombreFormat.setMaximumIntegerDigits(2);
//Create the text fields and set them up.
nombreField = new JFormattedTextField(nombreFormat);
nombreField.setValue(new Double(nombre));
nombreField.setColumns(10);
nombreField.addPropertyChangeListener("value", this);
resultatField = new JFormattedTextField(resultatFormat);
resultatField.setValue(new Double(resultat));
resultatField.setColumns(10);
resultatField.setEditable(false);
resultatField.setForeground(Color.red);
//Tell accessibility tools about label/textfield pairs.
nombreLabel.setLabelFor(nombreField);
resultatLabel.setLabelFor(resultatField);
//Lay out the labels in a panel.
JPanel labelPane = new JPanel(new GridLayout(0,1));
labelPane.add(nombreLabel);
labelPane.add(resultatLabel);
//Layout the text fields in a panel.
JPanel fieldPane = new JPanel(new GridLayout(0,1));
fieldPane.add(nombreField);
fieldPane.add(resultatField);
//Put the panels in this panel, labels on left,
//text fields on right.
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(labelPane, BorderLayout.CENTER);
add(fieldPane, BorderLayout.LINE_END);
}
/** Called when a field's "value" property changes. */
public void propertyChange(PropertyChangeEvent e) {
Object source = e.getSource();
if (source == nombreField) {
nombre = ((Number)nombreField.getValue()).doubleValue();
}
double resultat = 2*nombre;
resultatField.setValue(new Double(resultat));
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("FormattedTextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new Exemple056_JFormattedTextField();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |
Partager