Solved

Fixed Length TextFields

Posted on 1998-06-10
4
670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

630 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