JOptionPane tutorial

25Jun2010 In: Swing

JOptionPane Introduction

JOptionPane is a basic component defined in Java swing, it makes easy to pop a standard dialog box that accept user input or prompts user for a value, JOptionPane contains classes used for a graphical user interface (GUI) Facilitates data entry and data output.

How Do I use Joptionpane – tutorial and example

JOptionPane has four static methods showXxxDialog, which are used to prompt different dialog, we can directly call these methods to display dialog to users.
Read the rest of this entry »

JAX-RPC QuickOview

JAX-RPC (Java API for XML-Based RPC) is an application program interface (API) in the Java Web Services Developer Pack (WSDP), JAX-RPC is a specification that is developing through the Java Community Process (JCP). It aims to provide a JCP-sanctioned standard set of Java APIs for both a client-side and server-side programming model.
These JAX-RPC APIs leverage interoperable communications within Java applications with a protocol design center based on, but not limited to,SOAP. It covers the following areas:

  • A Java code generation model for client-side stubs and server-side tie classes, based on a set of conventions for mapping WSDL to Java and Java to WSDL.
  • An API for dynamic SOAP-RPC and a Call interface that is conceptually similar to Apache SOAP.  Call semantics include synchronous invoke and synchronous invoke/one-way. JAX-RPC does not address asynchronous invocation in its 1.0 rendition. A true asynchronous model would require callbacks (onMessage, etc). The one-way invocation model defined in the API is considered synchronous.
  • A model for defining a service, registering it, and invoking it within the J2EE and J2SE environments. This model covers typical J2EE/J2SE deployment issues such as creating deployment descriptors and packaging Web Application Archive (WAR) files.
  • A binding to SOAP, including SOAP Fault  handling through Java exceptions and HeaderFault processing.
  • Type mappings between Java and XML datatypes.
  • A service-side invocation handler mechanism used to chain together service method
  • invocations.
  • A reference implementation (RI) that provides a runtime implementation and a code generation tool, xrpcc.
  • A serialization framework for marshalling and unmarshalling data between Java and XML based on soap-encoding rules. The RI from Sun includes an implementation of this framework.

JAX-RPC is a funny kind of animal. Finding the right kinds of things to write about it in the context of this book was a challenge. At the time of writing, the specification was still evolving. This chapter is based on a Version 0.6  snapshot of the specification’s first public draft, issued as part of the Winter 01 Java Web Services Developer Pack. There are no known implementations, except for the RI (which is evolving with the specification) and the Apache Axis project (which is in alpha stage and based on Version 0.5 of the specification). Therefore, many examples in this section are repurposed directly from the specification. Several areas in the specification are in flux, need more definition, and are extremely likely to change before it hits a 1.0 status. Currently, the specification is 152 pages long and has enough information in it to comprise a whole book when it’s finally finished. Read the rest of this entry »

Java copy to clipboard

18Jun2010 In: Core Java

The clipboard is a set of functions and messages that enables applications to transfer data each other, Java implements the mechanism to transfer data using cut/copy/paste operations. This chapter demonstrate some examples about coping data to clipboard, clearing clipboard and reading existing clipboard contents.

Java have system clipboard and local Clipboard, system clipboard is more commonly and widely used, local Clipboard is only visible for single application or specified program. Read the rest of this entry »

How to convert string to int in java

18Jun2010 In: Core Java

The propose of this tutorial is to guide you how to convert a string data to int in Java, it’s very basic skill for a Java developer.

(1). Use method parseInt() in class Integer to covert string object to int.

public class ConvertStringToInt {
  public static void main(String[] args){
  String str = "124";
  int strInt = Integer.parseInt(str);

(2). Alternatively, method valueOf() in class Integer can also be used to covert string to int, this is a piece of codes to covert string to int.

  String str = "124";
  int strInt = Integer.valueOf(str);

Notice if the string is not a number pattern, the conversation will fail and a NumberFormatException will be thrown, in case you should handle in a try/catch block.

After Swing components have been displayed on the screen, they should only be operated on by the event-handling thread. The event-handling thread (or just event thread) is started automatically by the Java VM when an application has a graphical interface. The event thread calls methods like paint() on Component, actionPerformed() on ActionListener, and all of the other event-handling methods.

Most of the time, modifications to Swing components are done in the event-handling methods. Because the event thread calls these methods, it is perfectly safe to directly change components in event-handling code. SimpleEvent (see below simple code) shows safe Swing code.   Read the rest of this entry »

I recently developed an online website using tomcat 5.5 as web server, one functionality is that the end user changes their profile and uploads image, when the size of image is 500×600 there have no any problem, when changing the size to 800×800 I ran into java.lang.IllegalStateException: Post too large exception, I finally determined IllegalStateException: Post too large is caused by that tomcat has size limitation when uploading attachments and files.

Apache Tomcat by default sets a limit on the maximum size of the accepted post requests, the default configuration of tomcat  is set to 2MB, when try to upload file whose size is bigger than 2MB, the error java.lang.IllegalStateException: Post too large exception occurred.

How to solve IllegalStateException: Post too large?

One approach is to validate size of image on view layer using JavaScript, given an error message if the size of the file is over than 2MB, alternative, we can reconfigure Tomcat accept larger POST requests, either increase the limit or by disable it, open the Tomcat installation dir conf/servler.xml, find out the following statement then reset maxPostSize value, it limits the maximum post size of its accepted every post requests. setting 0 to disable the size check.

 <Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" maxPostSize="0" />

Then restart tomcat service the error java.lang.IllegalStateException: Post too large is gone.

I’m recently working on a large online website and using Tomcat in development phase, I broke the tomcat so that I got the NoClassDefFoundError exception. Here is the stack trace of NoClassDefFoundError.

at org.apache.catalina.loader.WebappClassLoader.loadClass(
at org.apache.catalina.loader.WebappClassLoader.loadClass(
at java.lang.ClassLoader.loadClassInternal(
at SecurePaymentAjaxData.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.process(
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(

Read the rest of this entry »

Which file defines the Tomcat Port?

The configuration of tomcat presents in server.xml and located under {Tomcat installation folder}\conf\, open the server.xml file and search the following statement:

<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />

The default HTTP port is 8080 configured in the HTTP connector, these types of servers are designed to be able to listen on (almost) arbitrary ports, for example, says we want to change the port to 80, which is the standard HTTP port. You can change the value of port attribute as showed below.

<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />

How do I Get  Tomcat Port Number in Java Programming Code?

I created a static method getTomcatPortFromConfigXml to read the tomcat port defined in server.xml.

Read the rest of this entry »

How to Change Tomcat Port

14Jun2010 In: Tomcat

In Tomcat, all configuration information are maintained in file server.xml, the default tomcat port number is 8080. However the default port might conflict with other programs when starting up tomcat, in such case, you could meet the error ‘the tomcat server port 8080 is already in use’. The follow steps demonstrate how to change tomcat default port.

How to change the tomcat default port:

(1) Go to the tomcat installation directory {Tomcat installation folder}\conf\, open server.xml and find the below:

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<!-- Note : To disable connection timeouts, set connectionTimeout value
to 0 -->

Read the rest of this entry »

How to Center a Window on Screen

7Jun2010 In: Swing

The customers’ computer has various resolution, it’s really hard to calculate location of frame to center a on screen. here is a piece of code to share to you. On a multimonitor system the window is centered on the same screen as the owner. If the window doesn’t have an owner it is centered on the primary screen.

Read the rest of this entry »