Jump to content

Help: JField getSelectedText().isEmpty() throws NullPointerException

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
johnsonk

johnsonk

    Newbie

  • Members
  • PipPip
  • 17 posts
Hello,

I've written a class which extends JTextField, basically for handling number input.
I'm having a bit of trouble getting one of the parts to work.


//text field's length is the maximum, character typed is a digit

  if(getText().length()==maxCharacters&&Character.isDigit(currentKey))

        {

                //nothing inside the text field is selected

                 if( getSelectedText().isEmpty()){

                      ev.consume();

                     return;}

                 }


   // passed the tests, process key as normal

        else{

            super.processKeyEvent(ev);

                }



Basically I want to consume the event if the max characters has been reached and there is no text selected, this is so it lets users overwrite the text by selecting the text and type a digit.


Regards
Kyle

#2
johnsonk

johnsonk

    Newbie

  • Members
  • PipPip
  • 17 posts
Never mind I managed to get it to work. Basically I got rid of that section of code and added a class called JTextFieldLimit to do the work

setDocument(new JTextFieldLimit(maxCharacters));

The code for JTextFieldLimit

import javax.swing.text.*;

public class JTextFieldLimit extends PlainDocument {
    private int limit;
    // optional uppercase conversion
    private boolean toUppercase = false;

    JTextFieldLimit(int limit) {
        super();
        this.limit = limit;
    }

    JTextFieldLimit(int limit, boolean upper) {
        super();
        this.limit = limit;
        toUppercase = upper;
    }

    public void insertString
            (int offset, String  str, AttributeSet attr)
            throws BadLocationException {
        if (str == null) return;

        if ((getLength() + str.length()) <= limit) {
            if (toUppercase) str = str.toUpperCase();
            super.insertString(offset, str, attr);
        }
    }
}


#3
noel

noel

    Newbie

  • Members
  • PipPip
  • 11 posts
hi i belive that you need to write your own overriding Exception as to intcept the system exception that your code is throwing.
this is a example of one for email
hope it helps :)
---------------------------- emailclass --------------------------------
public class EmailAddress
{
	private String domain = "";			// Domain component
	private String emailAddress;			// Full email address
	private String server = "";			// Server component
	private String user = "";			// User component
							// Constructor default
	public EmailAddress() throws EmailException
        {
		this("user@server.com");
	}
							// Constructor requires address
	public EmailAddress(String address) throws EmailException
        {
		int  pos1, pos2;
							// Parse into components
		pos1 = address.indexOf('@');
		if (pos1 == -1)
                {
			throw new EmailException(address, "Email address requires an @");
		}
		this.user = address.substring(0, pos1);	// Set the user
		if (pos1 == 0)
                {
			throw new EmailException(address, "Email address requires a username");
		}
		pos2 = address.indexOf('.');
		if (pos2 == -1 || (pos1+1) >= pos2)
                {
			throw new EmailException(address, "Email address requires a server name");
		}
		this.server = address.substring(pos1 + 1, pos2); // Set the server
		this.domain = "com";			// Assumed for now
		this.emailAddress = address;		// Set full email address
	}
							// Return email address string
	public String getAddress()
        {
            return this.emailAddress;
        }

	public String toString()
        {
		return this.getAddress() + "\n" + "user   = " + this.user
			+ "\nserver = " + this.server +  "\ndomain = " + this.domain;
	}
}

-------------------------- the custom exception ---------------------------------------------------------
public class EmailException extends Exception
{
	private String invalidAddress;

	public EmailException(String invalidAddress, String message) {
		super(message);
		this.invalidAddress = invalidAddress;
	}

	public String getInvalidAddress() {
		return this.invalidAddress;
	}
}

Edited by Vswe, 20 March 2010 - 02:04 AM.
Use Code tags(# button) when posting code