Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Join related table and count rows or sum sql server 2005 count rows: SELECT * FROM Table1 LEFT JOIN     (SELECT Level1ID, COUNT(Level2ID) AS Total FROM Table2 GROUP BY Level1ID) AS... Append soap header into axis When axis generate classese of web service, it won't include the request of soap header. Here is a way to append soap header manually i...
Blog Archive
Stats
SwiXml - JTextBox (Customized JTextField)
LimitedDocument.java
package swixml.sample;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class LimitedDocument extends PlainDocument {
    private int limit;

    public LimitedDocument(int limit) {
        super();
        this.limit = limit;
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        if (limit == 0 || getLength() + str.length() <= limit) {
            super.insertString(offs, str, a);
        }
    }
}
JTextBox.java
package swixml.sample;

import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

public class JTextBox extends JTextField {

    // input max length
    private int maxLength;

    public JTextBox() {
        super();
    }

    public JTextBox(int columns) {
        super(columns);
    }

    public JTextBox(String text) {
        super(text);
    }

    public JTextBox(String text, int columns) {
        super(text, columns);
    }

    public JTextBox(Document doc, String text, int columns) {
        super(doc, text, columns);
    }

    public void setLimit(int maxLenth) {
        this.maxLength = maxLenth;
        if (maxLenth > 0) {
            this.setDocument(new LimitedDocument(maxLenth));
        } else {
            this.setDocument(new PlainDocument());
        }
    }

    public int getLimit() {
        return this.maxLength;
    }
}
JTextBoxPane.xml
<?xml version="1.0" encoding="UTF-8"?>
<panel layout="FlowLayout">
    <textbox text="aa" columns="20" limit="10" />
</panel>
JTextBoxPane.java
package swixml.sample;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.swixml.SwingEngine;

public class JTextBoxPane extends JPanel {

    public JTextBoxPane() {
        // TODO Auto-generated constructor stub
        try {
            SwingEngine se = new SwingEngine(this);
            se.getTaglib().registerTag("TextBox", JTextBox.class);
            new SwingEngine(this).insert("swixml/sample/JTextBoxPane.xml", this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("SwiXML - JTextBox");

        frame.setContentPane(new JTextBoxPane());
        frame.pack();

        Dimension sc = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((sc.width - frame.getWidth()) / 2, (sc.height - frame.getHeight()) / 2);
        frame.setVisible(true);
    }
}