Search This Blog

Showing posts with label button. Show all posts
Showing posts with label button. Show all posts

How to Logoff or Sign Out Windows Using Java Code

You can sign out the current user account from windows using java code. Sign out, log out or logoff button can be created using simple lines of code. To logout from Windows operating system using java code, use the following code:


try {
        Runtime.getRuntime().exec("shutdown /l");
    } catch (IOException ex) {}

As the above code shows, we are simply executing a cmd command shutdown /l. It is actually running shutdown.exe with parameter /l to force logoff. If you add above code in the actionPerformed() function of a button, the button becomes a logoff button. The following video will show an example in which a simple button is made into a logout button.



How to Close or Exit a Java App When some Button is Clicked

To close a Java app, System.exit() can be used. This function can be used to close the app if an error occurs or no error is occurred. If no, error occurred, the argument  of the function should be 0. This method terminates the currently running Java Virtual Machine. The argument stands for status code; by convention, a nonzero status code indicates abnormal termination. If you are calling the System.exit() function without any errors (in normal case), just give the argument as zero. This method calls the exit method in class Runtime.

How to Change Image in JButton in Java - Set Icon of JButton

In this post we are discussing how to change the image in button in Java. There may be situations in
change jbutton image java jbutton image jbutton background image jbutton image icon how to add image to jbutton in java swing jbutton picture adding image to jbutton add image to jbutton
Jbutton with Image set
programming where we want to know how to set JButton image in java or to change button icon in java. The java code to change the icon of a Jbutton is so short. We use the
setIcon()
method in JButton class. The setIcon method takes an ImageIcon object as an argument. I will show you a sample java code in which the icon of  button is set. First of all,  you have to copy the icon image (jpg, bmp, png or gif image) to the src folder in the project directory. You may also place it in some folder inside the src folder. If you are using netbeans IDE, you can simply drag and drop the image file into the desired folder in the projects pane. After copying the icon image to src folder (or any subfolder in it), use the following code to set it as the button's icon.
playbtn.setIcon(new ImageIcon(getClass().getResource("/manhaj/images/play.png")));