Solved

Fixed Length TextFields

Posted on 1998-06-10
4
665 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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