Articles tagged with: Code
Java Codes »
This code shows how to create instances of both the java.util.Date and java.sql.Date classes using a Calendar.
import java.util.Calendar;public class DateUtil { public void createDates() { int year = 2006; int month = 0; //January int date = 1; Calendar cal = Calendar.getInstance(); //Clear all fields cal.clear(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DATE, date); //Create instance of java.util.Date java.util.Date utilDate = cal.getTime(); //Create instance of java.sql.Date java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis()); System.out.println(utilDate); System.out.println(sqlDate); } public static void main(String[] args) { DateUtil dateutil = new DateUtil(); dateutil.createDates(); }}
Java Codes »
Execution in Java always take form of a thread. In simple programs only one thread occurs, often referred to as the ‘main thread’.In some programs though concurrent threads is necessary, or if you are building a Swing application with for example a progress bar you don’t want the main thread to handle the updates of the UI.Then you create an additional thread to take care of that. There are two main ways of creating a thread. The first is to extend the Thread class and the second is to implement …
Java Codes »
Setting a threads priority can be very useful if one thread has more critical tasks to perform than another.The Thread class has a method called setPriority(int level) with which you can alter the priority a Thread instance has.The priority level range from 1 (least important) to 10 (most important) and if no level is explicitly set, a Thread instance has the priority level of 5.In the first example below no priorites are set, so both threads have the priority level 5. The TestThread class implements the Runnable interface and in …
Java Codes »
>To make a JLabel contain more than one row of text, use HTML to add the text with a new line (br-tag) between the text items.
JLabel label = new JLabel();…label.setText(“<html>First row<br>Second row</html>”);
Java Codes »
The ‘Look and Feel’ is the term for the appearance of the graphical user interface of an application and it should be set prior to constructing and rendering any of the GUI. This example shows a couple of variants on how to set the look and feel for a Swing application.The Java Development Kit contains a few look and feel classes (i.e. subclasses of the LookAndFeel class):com.sun.java.swing.plaf.gtk.GTKLookAndFeeljavax.swing.plaf.metal.MetalLookAndFeelcom.sun.java.swing.plaf.windows.WindowsLookAndFeelcom.sun.java.swing.plaf.motif.MotifLookAndFeelYou can set any of these by calling the setLookAndFeel() method of the UIManager:
public static void main(String[] args) { try { UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifLookAndFeel”); } catch (Exception ex) { //Just …
Java Codes »
This code example shows how to easily display a message dialog using the Swing class JOptionPane.
public class myFrame extends JFrame { //…. public void displayMessage() { JOptionPane.showMessageDialog(this, “The message”, “The Title”, JOptionPane.INFORMATION_MESSAGE); }}
If you want an icon on the dialog, provide an instance of theIcon class as a fifth parameter to showMessageDialog().
Java Codes »
This code example shows how to calculate and find prime numbers. It has an int variable called UPPER_LIMIT which tells the program for how long it will keep on looking and calculating.Since it’s not possible to divide a prime value with any other number (except for itself and one) and receive an integer without rounding, we try to divide every number we find with smaller numbers. To avoid making too many calculations we can first calculate the square root of each number and then use that for the division.If a …
Java Codes »
This example shows how to get the parameters sent in a http request to a servlet. In this example we use the method service() which will process requests of type GET as well as requests of type POST.To get the parameter names the method getParameterNames() is called on the request object. The method returns an Enumeration which we use to loop through the parameter names, and for each name the method getParameter() is called on the request object to get the value of that parameter.
import java.io.*;import java.util.Enumeration;import javax.servlet.*;import javax.servlet.http.*;/** * Example …
Java Codes »
This code example shows how to set and get variables stored in a session. To get a reference to the session object we call the getSession() method of the HttpServletRequest object.We send the argument true to the method to tell it to create the session if it doesn’t exist.Then we try to get the session variable ‘VisitCounter’ from the session by calling the method getAttribute(). It’s supposed to be stored as an Integer object, so we cast it to an Integer directly.If the parameter is null, we create a new …
Java Codes »
This code example shows how to obtain the IP address of the user that calls the server.The Request object has a few neat methods to get information about the call and about the caller, and one of them is named getRemoteAddr() which returns the IP address of the calling computer.In the example below we print out the IP address on a blank html-page.
import java.io.*;import java.util.Enumeration;import javax.servlet.*;import javax.servlet.http.*;/** * Example Servlet * @a*#117;th*#111;*#114; *#119;*#119;w.j*#97;v*#97;db.*#99;o*#109; */public class ExampleServlet extends HttpServlet { /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param …
