Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Fixed Length TextFields

Posted on 1998-06-10
4
Medium Priority
?
677 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 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