Showing posts with label Desktop Application. Show all posts
Showing posts with label Desktop Application. Show all posts

Thursday, February 23, 2012

Setting Maximum no. of characters in jTextField for Swing Application

Sometimes we need to fixed the maximum size of a text field. For this purpose, we need to implement a document containing an overriding function of plain document.

public class MaxLengthTextDocument extends PlainDocument {

private int maxChars;

@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if(str != null && (getLength() + str.length() < getMaxChars())){ super.insertString(offs, str, a); } } /** * @return the maxChars */ public int getMaxChars() { return maxChars; } /** * @param maxChars the maxChars to set */ public void setMaxChars(int maxChars) { this.maxChars = maxChars; } }



after that we will have to set the following line after initComponents() of view file.


MaxLengthTextDocument maxLength = new MaxLengthTextDocument();
maxLength.setMaxChars(12);//50 is a maximum number of character
this.txtMobileNo.setDocument(maxLength);

Wednesday, September 7, 2011

Java Desktop Application: To set size of the mainFrame of singleFrameApplication in Netbeans n

It was very critical for me to fix the size of the Form in a SingleFrameApplication through Netbeans. In design view if we rezise the frame, it doesn't affect at the run time. As In teh run time it shows the default size. To resize the page, we need to write some code in a specific location. In the view.java file of application package, we need to modify some info.


this.getFrame().setTitle("Stop Job from Database"); //Ti set the title of the default form

Dimension size = new Dimension(0144, 0144); // these values need to be changed to Octal or Hexadecimal after provding Integer value.

this.getFrame().setSize(size); // to set the size of the frame