Solved

Fixed Length TextFields

Posted on 1998-06-10
4
663 Views
Last Modified: 2008-03-03
I am using swing's JTextField and I need to limit the characters a user can type into it. I am using a KEY_EVENT_MASK to capture the keystroke and consume it if necessary.  However, how can I capture a user pasting into the TextField and how can I consume the extra text the User may have pasted into it (i.e. If the textfield can only have 6 characters and the user attempts to paste 10 characters)?
0
Comment
Question by:tarheel
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 50 total points
ID: 1222858
Sorry, I have no time to test the code... I never did that in the past, here is a first glance idea:

public class MyTextField extends JTextField {

// we override paste
 public void paste() {
  JTextField field = new JTextField(); // create a dummy field
  field.paste(); // paste in the dummy field
 String text = field.getText(); // get what has been pasted

 if (text.length() > 10) { // if text larger than authorized, shorten it
    text = text.substring(0,9);
    field.setText(text);
 }

 field.selectAll(); // select the (eventually new) content of the field
 field.copy(); // copy it to the clipboard
 super.paste(); // paste it
 }


}
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1222859
OK, it works. I wrote a sample app. Here it is:

import java.awt.*;

import com.sun.java.swing.*;


public class Test extends JFrame {

    public Test() {
        getContentPane().setLayout(new GridLayout(1,2));
        getContentPane().add(new JTextField(30));
        getContentPane().add(new MyTextField(30));
    }

    public static void main(String args[]) {
        Test t = new Test();
        t.pack();
        t.show();
        return;
    }


}

class MyTextField extends JTextField {

    public MyTextField(int cols) {
        super(cols);
    }

   // we override paste
    public void paste() {
     JTextField field = new JTextField(); // create a dummy field
     field.paste(); // paste in the dummy field
    String text = field.getText(); // get what has been pasted

    if (text.length() > 10) { // if text larger than authorized, shorten it
       text = text.substring(0,9);
       field.setText(text);
    }

    field.selectAll(); // select the (eventually new) content of the field
    field.copy(); // copy it to the clipboard
    super.paste(); // paste it
    }
}

Write a long line of text at the right, copy it and paste it at the right. You will see that only
the first ten characters are pasted.
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1222860
Ooops, it takes only 9 characters. I had to write text = text.substring(0,10); to have 10
characters...
0
 

Author Comment

by:tarheel
ID: 1222861
Thanks!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
print map entry 34 72
fibonacci ten numbers 4 45
eclipse console opening separately 2 29
Chrome and Firefox Java 5 50
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question