Text fields and Java events : TextField « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » TextFieldScreenshots 
Text fields and Java events
Text fields and Java events
   

// : c14:TextFields.java
// Text fields and Java events.
// <applet code=TextFields width=375 height=125></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class TextFields extends JApplet {
  private JButton b1 = new JButton("Get Text"), b2 = new JButton("Set Text");

  private JTextField t1 = new JTextField(30), t2 = new JTextField(30),
      t3 = new JTextField(30);

  private String s = new String();

  private UpperCaseDocument ucd = new UpperCaseDocument();

  public void init() {
    t1.setDocument(ucd);
    ucd.addDocumentListener(new T1());
    b1.addActionListener(new B1());
    b2.addActionListener(new B2());
    DocumentListener dl = new T1();
    t1.addActionListener(new T1A());
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(b1);
    cp.add(b2);
    cp.add(t1);
    cp.add(t2);
    cp.add(t3);
  }

  class T1 implements DocumentListener {
    public void changedUpdate(DocumentEvent e) {
    }

    public void insertUpdate(DocumentEvent e) {
      t2.setText(t1.getText());
      t3.setText("Text: " + t1.getText());
    }

    public void removeUpdate(DocumentEvent e) {
      t2.setText(t1.getText());
    }
  }

  class T1A implements ActionListener {
    private int count = 0;

    public void actionPerformed(ActionEvent e) {
      t3.setText("t1 Action Event " + count++);
    }
  }

  class B1 implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (t1.getSelectedText() == null)
        s = t1.getText();
      else
        s = t1.getSelectedText();
      t1.setEditable(true);
    }
  }

  class B2 implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      ucd.setUpperCase(false);
      t1.setText("Inserted by Button 2: " + s);
      ucd.setUpperCase(true);
      t1.setEditable(false);
    }
  }

  public static void main(String[] args) {
    run(new TextFields()375125);
  }
  public static void run(JApplet applet, int width, int height) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet);
    frame.setSize(width, height);
    applet.init();
    applet.start();
    frame.setVisible(true);
  }
  
}

class UpperCaseDocument extends PlainDocument {
  private boolean upperCase = true;

  public void setUpperCase(boolean flag) {
    upperCase = flag;
  }

  public void insertString(int offset, String str, AttributeSet attSet)
      throws BadLocationException {
    if (upperCase)
      str = str.toUpperCase();
    super.insertString(offset, str, attSet);
  }

///:~


           
         
    
    
  
Related examples in the same category
1. Make a Text Field two columns wide
2. Water mark text field
3. Auto complete TextField
4. JTextField Alignment SampleJTextField Alignment Sample
5. Create the textfieldCreate the textfield
6. FieldEdit - an Applet to validate data as it's being entered
7. TextField with only Integer value
8. File Name Bean
9. Textfield only accepts numbersTextfield only accepts numbers
10. Overwritable TextFieldOverwritable TextField
11. Numeric TextFieldNumeric TextField
12. Passive TextField 1Passive TextField 1
13. Passive TextField 2Passive TextField 2
14. Text Accelerator ExampleText Accelerator Example
15. TextField Look Ahead ExampleTextField Look Ahead Example
16. Passive TextField 3Passive TextField 3
17. Non Wrapping(Wrap) TextPaneNon Wrapping(Wrap) TextPane
18. EditabilityExampleEditabilityExample
19. Bounded TextFieldBounded TextField
20. TextField ElementsTextField Elements
21. TextFieldViews 2TextFieldViews 2
22. TextField with ConstaintsTextField with Constaints
23. JTextField Sample 2JTextField Sample 2
24. JTextField Verifier SampleJTextField Verifier Sample
25. A simple label for field form panelA simple label for field form panel
26. A hack to make a JTextField really 2 columns wideA hack to make a JTextField really 2 columns wide
27. Limit JTextField input to a maximum length
28. Make sure that my JTextField has the focus when a JFrame is created
29. Make the ENTER key act like the TAB key
30. Setting up a textfield and modifying its horizontal alignment at runtimeSetting up a textfield and modifying its horizontal alignment at runtime
31. Aligning the Text in a JTextField Component
32. Based on JTextField content, enable or disable a JButton
33. Cut, paste, and copy in a JTextField under program control.
34. Add key listener event handler to JTextField
35. Right justified JTextfield content
36. Set the focus on a particular JTextField
37. Associate JLabel component with a JTextField
38. Right justified JTextField contents
39. Validate a value on the lostFocus event
40. Modify horizontal alignment of text field at runtime
41. Make sure that my Text field has the focus when a JFrame is created
42. Firing Item Events
43. extends JTextField to create integer JTextField
44. JTextField Max Length
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.