Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 8.24 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to implement documentlistener
  2. import java.awt.ComponentOrientation;
  3. import java.awt.Container;
  4. import java.awt.Dimension;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JCheckBox;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTable;
  16. import javax.swing.JTextField;
  17. import javax.swing.event.DocumentEvent;
  18. import javax.swing.event.DocumentListener;
  19.  
  20.  
  21. public class Display {
  22. final static boolean shouldFill = true;
  23. final static boolean shouldWeightX = true;
  24. final static boolean RIGHT_TO_LEFT = false;
  25.  
  26. public static void addComponentsToPane(Container pane) {
  27.  
  28.     if (RIGHT_TO_LEFT) {
  29.         pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  30.     }
  31.     JButton button;
  32.     JLabel label;
  33.  
  34.     pane.setLayout(new GridBagLayout());
  35.     GridBagConstraints c = new GridBagConstraints();
  36.     if (shouldFill) {
  37.     //natural height, maximum width
  38.     c.fill = GridBagConstraints.HORIZONTAL;
  39.     }
  40.     if (shouldWeightX) {
  41.     c.weightx = 0.5;
  42.     }
  43.  
  44.     ...
  45.  
  46.     button = new JButton("Value Bet");
  47.     c.fill = GridBagConstraints.HORIZONTAL;
  48.     c.ipady = 0;
  49.     c.gridx = 0;
  50.     c.gridy = 1;
  51.     pane.add(button, c);
  52.     button.addActionListener(new ActionListener() {
  53.  
  54.         public void actionPerformed(ActionEvent e)
  55.         {
  56.             //Execute when button is pressed
  57.             JFrame frame = new JFrame("Value Bet");
  58.             frame.setVisible(true);
  59.             frame.setSize(500,300);
  60.             GridBagLayout layout = new GridBagLayout();
  61.             frame.setLayout(layout);
  62.             GridBagConstraints c = new GridBagConstraints();
  63.  
  64.             JLabel label;
  65.             JTextField tf;
  66.  
  67.             if (shouldFill) {
  68.             //natural height, maximum width
  69.             c.fill = GridBagConstraints.HORIZONTAL;
  70.             }
  71.             if (shouldWeightX) {
  72.             c.weightx = 0.5;
  73.             }
  74.  
  75.             ...
  76.  
  77.             final JTextField tf1 = new JTextField();
  78.             c.fill = GridBagConstraints.HORIZONTAL;
  79.             c.gridx = 1;
  80.             c.gridy = 2;
  81.             frame.add(tf1, c);
  82.  
  83.             tf1.getDocument().addDocumentListener(new DocHandler(){
  84.                 public class DocHandler implements DocumentListener{
  85.  
  86.                     @Override
  87.                     public void changedUpdate(DocumentEvent arg0) {
  88.                         tfHasChanged();
  89.  
  90.                     }
  91.  
  92.                     @Override
  93.                     public void insertUpdate(DocumentEvent arg0) {
  94.                         tfHasChanged();
  95.  
  96.                     }
  97.  
  98.                     @Override
  99.                     public void removeUpdate(DocumentEvent arg0) {
  100.                         tfHasChanged();
  101.  
  102.                     }
  103.  
  104.                 }
  105.  
  106.                 public void tfHasChanged(){
  107.                     double chance1 = Double.parseDouble(tf1.getText());
  108.                 }
  109.             });      
  110.  
  111.             ... div components
  112.  
  113.  
  114. }
  115.  
  116.  private static void createAndShowGUI() {
  117.         //Create and set up the window.
  118.         JFrame frame = new JFrame("Betting Application");
  119.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  120.  
  121.         //Set up the content pane.
  122.         addComponentsToPane(frame.getContentPane());
  123.  
  124.         //Display the window.
  125.         frame.pack();
  126.         frame.setVisible(true);
  127.     }
  128.  
  129.  public static void main(String[] args) {
  130.         //Schedule a job for the event-dispatching thread:
  131.         //creating and showing this application's GUI.
  132.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  133.             public void run() {
  134.                 createAndShowGUI();
  135.             }
  136.         });
  137.     }
  138. }
  139.        
  140. formatter.setMinimum(0.0);
  141. formatter.setMaximum(1000.00);
  142.        
  143. import java.awt.*;
  144. import java.awt.font.TextAttribute;
  145. import java.math.*;
  146. import java.text.*;
  147. import java.util.Map;
  148. import javax.swing.*;
  149. import javax.swing.JFormattedTextField.*;
  150. import javax.swing.event.*;
  151. import javax.swing.text.InternationalFormatter;
  152.  
  153. public class DocumentListenerAdapter {
  154.  
  155.     public static void main(String args[]) {
  156.         JFrame frame = new JFrame("AbstractTextField Test");
  157.         final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
  158.         textField1.setFormatterFactory(new AbstractFormatterFactory() {
  159.  
  160.             @Override
  161.             public AbstractFormatter getFormatter(JFormattedTextField tf) {
  162.                 NumberFormat format = DecimalFormat.getInstance();
  163.                 format.setMinimumFractionDigits(2);
  164.                 format.setMaximumFractionDigits(2);
  165.                 format.setRoundingMode(RoundingMode.HALF_UP);
  166.                 InternationalFormatter formatter = new InternationalFormatter(format);
  167.                 formatter.setAllowsInvalid(false);
  168.                 formatter.setMinimum(0.0);
  169.                 formatter.setMaximum(1000.00);
  170.                 return formatter;
  171.             }
  172.         });
  173.         final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
  174.         attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
  175.         final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01));
  176.         textField2.setFormatterFactory(new AbstractFormatterFactory() {
  177.  
  178.             @Override
  179.             public AbstractFormatter getFormatter(JFormattedTextField tf) {
  180.                 NumberFormat format = DecimalFormat.getInstance();
  181.                 format.setMinimumFractionDigits(2);
  182.                 format.setMaximumFractionDigits(2);
  183.                 format.setRoundingMode(RoundingMode.HALF_UP);
  184.                 InternationalFormatter formatter = new InternationalFormatter(format);
  185.                 formatter.setAllowsInvalid(false);
  186.                 //formatter.setMinimum(0.0);
  187.                 //formatter.setMaximum(1000.00);
  188.                 return formatter;
  189.             }
  190.         });
  191.         textField2.getDocument().addDocumentListener(new DocumentListener() {
  192.  
  193.             @Override
  194.             public void changedUpdate(DocumentEvent documentEvent) {
  195.                 printIt(documentEvent);
  196.             }
  197.  
  198.             @Override
  199.             public void insertUpdate(DocumentEvent documentEvent) {
  200.                 printIt(documentEvent);
  201.             }
  202.  
  203.             @Override
  204.             public void removeUpdate(DocumentEvent documentEvent) {
  205.                 printIt(documentEvent);
  206.             }
  207.  
  208.             private void printIt(DocumentEvent documentEvent) {
  209.                 DocumentEvent.EventType type = documentEvent.getType();
  210.                 double t1a1 = (((Number) textField2.getValue()).doubleValue());
  211.                 if (t1a1 > 1000) {
  212.                     Runnable doRun = new Runnable() {
  213.  
  214.                         @Override
  215.                         public void run() {
  216.                             textField2.setFont(new Font(attributes));
  217.                             textField2.setForeground(Color.red);
  218.                         }
  219.                     };
  220.                     SwingUtilities.invokeLater(doRun);
  221.                 } else {
  222.                     Runnable doRun = new Runnable() {
  223.  
  224.                         @Override
  225.                         public void run() {
  226.                             textField2.setFont(new Font("Serif", Font.BOLD, 16));
  227.                             textField2.setForeground(Color.black);
  228.                         }
  229.                     };
  230.                     SwingUtilities.invokeLater(doRun);
  231.                 }
  232.             }
  233.         });
  234.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  235.         frame.add(textField1, BorderLayout.NORTH);
  236.         frame.add(textField2, BorderLayout.SOUTH);
  237.         frame.setVisible(true);
  238.         frame.pack();
  239.     }
  240.  
  241.     private DocumentListenerAdapter() {
  242.     }
  243. }
  244.        
  245. tf1.getDocument().addDocumentListener(new DocumentListener() {
  246.  
  247.     @Override
  248.         public void changedUpdate(DocumentEvent e){
  249.  
  250.                               //do some math
  251.                                tf1.getText();
  252.                                    }
  253.  
  254.         @Override
  255.         public void insertUpdate(DocumentEvent e) {
  256.  
  257.                                   /do some math
  258.                                tf1.getText();
  259.         }
  260.  
  261.         @Override
  262.         public void removeUpdate(DocumentEvent e) {
  263.             // TODO Auto-generated method stub
  264.  
  265.         }
  266. });