Screenshot of following example java program |
Tutorials, tips and tricks about java programming language. Answers for many doubts about java programming language. Answering many howtos. Sample java projects and source code, java games, java programs for java practical labs.
Search This Blog
How to Filter Filetypes using File Extensions in JFileChooser - Java File Chooser
What Does Modal and Modeless (Non Modal) Mean in Java
A Modal dialog box is a dialog box that blocks input to some other top-level windows in the application. The modal dialog box captures the window focus until it is closed, usually in response to a button press.
A Modeless dialog box is a dialog box that does not block input to any other top level window while it is shown.See following java code example:
How to Get User Related Informations in Java
In java, there are some methods that will help you to know some user related informations. Here also,
System.getProperty()method can give some informations about the user like:
User account name
User Working directory
User Home directory
To get the username of current user on the computer just call
System.getProperty("user.name")
It will return the current username as a string.
To get the working directory of current user on the computer call
System.getProperty("user.dir")
It will return the path of working directory of current user as a string.
To get the home directory (user's home folder) of current user on the computer call
System.getProperty("user.home")
It will return the path to home directory (folder) of current user as a string.
How to Get Operating System Information in Java
Using java, you can gather some information about the operating system of the computer system on which the code is running. That us java provides some API classes that will help you to know about the operating system of the computer on which your application program is running. You can use System.getProperty() method for this purpose. There are some system property keys (strings) which can be used to get the respective information. The parameter to the System.getProperty() method is a string, the property key. The following values for the parameter string will give operating system related informations.
"os.arch"
Operating system architecture
"os.name"
Name of the operating system.
"os.version"
Version of Operating system.
Usage examples:
The following java program displays the Operating system related informations using getProperty() method.How to Get JRE installation Directory in a Java Application
System.out.println(System.getProperty("java.home"));
How to Get Class Path in a Java Application
Class Path is the Path used to find directories and JAR archives containing class files. Elements of the class path are separated by platform-specific characters. To get class path from a running java application, you may use the getProperty() method in System class. The following java code will show you how to get class path in java.
System.out.println("Class path="+ classpath);
Why FileOutputStream or BufferedWriter Does Not Write New Line to File
The new-line character is system dependent. It depends on operating system. It can be \n, \r, \r\n or something else depending on the operating system. So, when we are writing a cross platform java program, it is important to take care when we use a line separator or new line character. It is always safe to use system specific line separator provided by JVM. You can get the system specific newline character in different ways.
Method 1:
System.lineSeparator() instead of new line character. The java method Sytem.lineSeparator() returns the newline character (line separator) as a strong. If you are appending a string on another with a line separator in between, it can be done as follows.
line1+System.lineSeparator()+line2
Method 2:
You can also get the line separator using the getProperty() method with property key line.separator.
System.getProperty("line.separator");
Method 3:
If your are using a BufferedReader object to write text into a file, you can use the newLine() method to write a newline into the file. If br is a BufferedReader object, br.newLine() will write a newline (line separator) into the file.
Method 4:
If you want to get the line separator as a character, you may use the static member Character.LINE_SEPARATOR. It returns the Unicode newline character.
How to Make a Timer Controlled Progress Bar in Java
timer controlled progress bar in java |
JProgressBar to create progress bars. It can have horizontal or vertical orientation. We can set the maximum and minimum values for JProgressBar. It can also have an indeterminate state in which the progressbar does not show a definite value and plays an animation to show that the process is in progress. JProgressBar can be used in indeterminate mode when neither can you determine how long a process would take to complete nor can you track its progress. In some other cases, you may have to use timers. Java provides a Timer class (Java.util.Timer). But you can create a very simple and flexible timer if you want. If the timer does not have crucial importance, you can use a thread that sleeps for every one second as a timer. I have been using such threads as timers for a long time and never failed. Such timers may fail (although very rarely) when the processor has very heavy workload.
Problem With JProgressBar setValue Method? JProgressBar Value Not Updating - Solution
Java Program to Send Image between Computers using UDP and Display it
Java program to send image file over network using UDP |
picture (image) from a computer to another via network using UDP protocol and to display it at the receiving end. Simply, this is the java code to make a java application which can send images from one computer to another over a network. The program uses UDP (User Datagram Protocol). For both the computers (on either ends) the same code is enough. This app can act as a image sender or a receiver. The program uses a Progress bar (JProgressBar) to show the progress of the data transfer. Remember, transferring files over UDP is not easy because UDP does not guarantee ordered delivery, and not even successful delivery. So, here in this program, to ensure ordered delivery, the sender sends next chunk of data (DatagramPacket) only after receiving an acknowledgement for last sent packet. For acknowledgement, i used a string "ACK".