Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ReplaceDocument for Swing  (Read 1015 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Posted 2006-02-22 01:11:19 »

The following code is a special "ReplaceDocument" for Swing-based JTextComponent's. Basically it waits for text to be entered into the box, and then will replace it with previously-typed words or anything from a list you may want to have it fill in. It's like your internet browser which can automatically remember fields for you, then will fill in the rest of the word for you if it's been previously typed.

Example:
I have a list with {dog,cat,mouse,moose,monkey}
If I start the field by typing the letter 'd' then "dog" will be filled into the box, with "og" highlighted so it will automatically be replaced if you continue typing. If I wrote 'm' then "mouse" would appear, but once I typed in "moo" "moose" would fill in, and "mon" would result in "monkey." If I typed in "I own a d" then "I own a dog" would be filled in, as long as the character '  ' was specified as my word delimiter. If someone pasted the letters "mon" in, however, nothing would be added in. My document only functions for typed letters (but this can be changed, if anyone wants to).

I don't use this code for a game, personally, but I figure someone might. And you can always make your own GLTextComponent that uses Swing's AbstractDocument, or just take snippets of my code, or whatever else might work for your game.

To make it so a TextComponent has this document, use:
1  
2  
3  
4  
//The JTextField title is delimited by a space (ASCII 20)
title.setDocument(new ReplaceDocument(title," "));
//The JTextPane keywords is delimited by a comma (ASCII 2C)
keywords.setDocument(new ReplaceDocument(keywords,","));


Here is the AbstractDocument itself, it should be noted that in my implementation it is a private class within the main JFrame, but you can change this if you want. Just be sure to include both pieces in the class, or a way to access them.
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  
     /**
     * The ReplaceDocument is used for JTextFields that should use the information
     * saved in the RecentPanel to automatically fill in information as it is typed. This code
     * is available via the GNU public liscence in that you can do whatever the hell you want
     * to it, but I need to be credited somewhere as the author. Thankee, enjoy.
     * @author Eli Delventhal
     */

    private class ReplaceDocument extends javax.swing.text.PlainDocument
    {
          private javax.swing.text.JTextComponent field;
          private String delimiter;
         
          public ReplaceDocument (JTextField f, String d)
          {
             field = f;
             delimiter = d;
          }

          public void insertString(int offs, String str, javax.swing.text.AttributeSet a) throws javax.swing.text.BadLocationException
          {
             if (str.length() == 1 && Character.isJavaIdentifierPart(str.charAt(0)))
             {
                String[] s = (getText(0,getLength())+str).split(delimiter);
                 String rest = restWord(s[s.length-1]);
                super.insertString(offs,str + rest,a);
                field.select(offs+1, offs + rest.length() + 1);
             }
             else
                super.insertString(offs,str,a);
          }
       }

   /** The restWord method recieves a String and then returns the rest of word
     * to fill in. In this instance, it searches through a map of words the user has
     * already entered and are saved in a preferences file. Depending on your
     * uses, you will want to change that. To do so, simply change keywords.keySet()
     * to some sort of iterated data structure that fits your program. Also, this way
     * ignore case, if you want to change that remove both instances of .toLowerCase()
     */

   public String restWord(String t)
   {
      if (t.length() <= 0)
         return "";
      for (java.util.Iterator i = keywords.keySet().iterator(); i.hasNext();)
      {
         String s = (String) i.next();
         if (s.toLowerCase().startsWith(t.toLowerCase()))
            return s.substring(t.length());
      }
      return "";
   }


I hope this is useful for some people. Also you geniuses out there, don't feel bad if I did something retarded in my coding – just point it out. Doesn't bother me.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (7 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (23 views)
2014-07-29 03:07:13

E.R. Fleming (9 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!