Search This Blog

Chat Room Application using UDP in Java

UDP chatroom application in java source code Chatting using java UDP java chat application java group chat application
Java Chat room application using UDP
The following is a Java program for chatting. This chatting application uses UDP (User Datagram
Protocol) for chatting. The application gives you to options. One is to create a new chat room and the other is to join a chat room. The one who creates the chat room acts as a server for the chatroom. Others can join this chatroom id they know his IP address. This simple java chat room application is enough for server and clients. The GUI is basic one and not too attractive. Also i have used JOptionPane to reduce code. You may make improvements to the code. The chat rooms are not password protected. Anyone who know the server's IP address are granted access to the chat room. If you wish, you may improve the program. This Group chat application using java can be used in Local area networks. This code for java group chat program can be modified to enable chatting in multiple chat rooms. Try it.

How to download a file from internet Using java if URL is Given

Let us see how to download a file from internet in java. It is so simple. The following java program is a program which will download any file if the URL to the file to be downloaded is given. It also asks the user for destination to save the file. It displays the file size also. It also prints (displays) the progress of download. It uses the classes HttpURLConnection, URL, FileOutputStream, JFileChooser etc. The following java code is a simple java program to download a file from internet using URL. Here in this case, user has to give the file name and extension.


How to Easily Copy a File to Another File or Location in Java

The Files class in java has many useful static methods to copy and move files, create temporary files or folders etc. To copy a file from one location to another in Java, there is a static method copy in Files class. The Files class is in java.nio.file package. The method Files.copy() takes three arguments (parameters) : Path of source file (as object of Path class), Path of destination file ( as object of Path class ) and an option parameter. The option parameter can be one of the following:

StandardCopyOption.REPLACE_EXISTING
If the target file exists, then the target file is replaced.

StandardCopyOption.COPY_ATTRIBUTES
Attempts to copy the file attributes associated with this file to the target file. The exact file attributes
that are copied is platform and file system dependent and therefore unspecified.

StandardCopyOption.NOFOLLOW_LINKS
Symbolic links are not followed. If the file is a symbolic link, then the symbolic link itself, not the target of the link, is copied.

You have to import java.nio.file.StandardCopyOption to use option parameters.

try{
File sourcefile=new File("audio.mp3");
File destination=new File(System.getProperty("java.io.tmpdir")+"\\temp.mp3");
Files.copy(sourcefile.toPath(),destination.toPath(),StandardCopyOption.COPY_ATTRIBUTES);
} catch (IOException ex) {
ex.printStackTrace();
}

For the copy method, there are two other overloaded method definitions. In one, instead of source file's path, inputstream is the first parameter and the rest are same. For the other overloaded definition, an outputstream object is the second parameter instead of destination path object and the rest are same.

Similarly, to move a file from one location to another, there is a method move() in Files class. The parameters of this method is also same as those of copy method.

How to Get Path to Temp Directory for Current User in Java

In some cases, you may need to create temporary files. It is preferred to create temporary files and folders in the current user's temporary folder (temp directory) because, the user will be able to clean unwanted files. To get the path to the temp folder of current user, java provides a property key to be used with

System.getProperty()
. it is
"java.io.tmpdir"
.


The following code displays the path to the temporary folder for the current user.
System.out.println("temp directory ="+System.getProperty("java.io.tmpdir"));

How to Get Center Point of Screen in Java

If you want position your JFrame in center of screen, there is method for that. Read this post:

The LocalGraphicsEnvironment in Java will give you the center point of the display or screen. There is a static method getLocalGraphicsEnvironment() in GraphicsEnvironment class. To get the center point of the screen, you may use the following java code:


Point centre=GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();

You should include these import statements in the file.

import java.awt.Point; import java.awt.GraphicsEnvironment;

How to Set JFrame in Center of Screen

By default, a java application appears at top left corner of the screen. You might have seen that most of the applications appear in the middle of the screen. I will tell you how to center your java application on screen.

To set JFrame in center of Just call this.setLocationRelativeTo(null); in constructor of the JFrame. If you are using Netbeans IDE, it should be after calling initComponents() method. But if you call setLocation() method of JFrame after this, your JFrame will get relocated.

In most cases this works and this is enough. if you want to get the center point of the screen, just refer to the following post:

How to Get Center Point of Screen in Java

How to Set Background Color of a JFrame in Java

It is easy to set Background color of a JFrame in java. But still many people cannot do it (me too was once like this). Most such people may have tried this java code to set java JFrame background color: setBackground(Color.BLACK);. But this won't work. Instead, you should set the background color of the ContentPane of the JFrame. So, use the following code to set Background color of Frame in your Java Application.

getContentPane().setBackground(Color.BLACK);

or


getContentPane().setBackground(new Color(34,172,176));

Keep in mind that you have to import java.awt.Color; You can use the first one if you don't know RGB values of the desired color. Most common colors are defined as constants in Font class. If you know the RGB color values for your desired color, you can use the second example.

How to Change Look and Feel of a Java Application

The following code fragment which can be used within main method or in constructor of the class tells you how to change the look and feel of a java program (java application). This sample code sets the LookAndFeel of the java application into Windows Look and Feel.


Java Application Source Code to Scroll Text Along JFrame

This is a simple java program for beginners to show how to scroll text in the JFrame. The program uses an anonymous (unnamed) thread object to move the scroll or move the text. The Thread moves two JLabel objects (containing text) by repeatedly calling setLocation(int x, int y) method of JLabel. The speed of the motion of text in this program can be changed by changing value of i or changing sleep period of the thread.


Free, Simple and Light weight Download Manager Java Application - Application and Source Code

This is a free Internet Download Manager Application for PC developed in java and free to use. It is
very light weight with size only below 50KB. This download manager allows pause and resume for downloads and requires java jre 1.8 to run. The program source code is given below. This will help you to understand how to download files from internet using java. The program may help you to understand how to pause and resume downloads in a java program. The application also provides drag and drop support. You can drag a link to file to be downloaded from browser in to the drag and drop area (drop target) in the application and it will automatically start download or add to the download queue. This will also help you study how to use Drag and Drop and how to set a drop target.

Download free Download Manager Application for Computer


You may download the  lightweight java Download Manager Software from following link:

https://drive.google.com/file/d/0B5WYaNmq5rbueUl5bW03VllIc2M/view?usp=sharing

View Source code