Solved

Fixed Length TextFields

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now