Skip to main content

Posts

Eclipse Java Editor Shortcuts

Eclipse Oxygen Java editor Shortcuts helps in editing source code more comfortably. Frequently used default shortcuts are listed below.  Description Shortcut Delete Line CTRL + D Copy Line CTRL + ALT + Down Arrow Add method level, class level, member level Comments SHIFT + ALT + J Format Java Source code CTRL + SHIFT + F Make line commented CTRL + / Make multiple lines [block] commented   CTRL + SHIFT + / Search selected similar word within the source document CTRL + K Select line from beginning to end SHIFT + End key Select line from end to beginning SHIFT + Home key Select Word by word in same line CTRL + SHIFT + Right Arrow Move line above ALT + Up Arrow Move line below ALT + Down Arrow Shortcuts can ...

Launch Swing GUI always in a dedicated Thread

1. Since Swing is not thread safe, it is not at all a good practice to launch a UI Container like JFrame, JDialog etc in main Thread. 2. Doing so leads to incorrect behavior of java GUI. 3. To avoid this, Swing toolkit has predefined API as part of JFC. 4.    a. SwingUtilities.invokeAndWait(Runnable r);        b. SwingUtilities.invokeLater(Runnable r); 5. Which part of the code must be in UI Thread?         a. setVisible(true);         b. show(); [deprecated]  and c. pack();       6. Call to these methods mentioned in 5(a), 5(b), and 5(c) must happen in one of the methods mentioned in point 4(a) and point 4(b). That's enough. :)          Example :                        public static void main(String[] args){                         ...

File Manager/File Explorer with Swing JTree Component

This post is intended to beginners of java swing toolkit programmers. It is a simple java program which demonstrates the swing based File explorer GUI. import java.awt.GridLayout; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.event.TreeModelListener; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; /**  * @author NagasharathK  *  */ public class FileExplorer extends JFrame { private JTree fileManagerTree = null; public FileExplorer() { initComponents(); } /** * Initializes components */ private void initComponents() { this.getContentPane().add(new JScrollPane(createFileManagerTree(...

GridLayout: fill the form with gridlayout

Swing controls are of two types they are Containers and components. components are arranged on a containers. example components are JLabel, JButton etc and example containers are JPanel, JFrame and JDialog etc. these components are arranged on a container with the help of layout managers. Few layout managers available in swing toolkit are 1. FlowLayout [Jframe's Default layout manager] 2. GridLayout 3. BorderLayout [JPanel's Default layout manager] 4. GridBagLayout. 5. Custom Layout manager The custom layout amanger is useful to design programmer's own layout manager. Usually this is useful when any of the available layout managers does not give the needy result. apart of these above all layout managers, few third party libraries are also available they are 1. DesignGridLayout 2. Mig Layout etc Design grid Layout Gridlayout is one of the most simple and flexible layout managers. layout manager is used to arrange the components on a container with proper mar...

WMI - Windows Management Instrumentaion

WMI is a windows Library used to get the information like no of processes running currently in the computer and their event notifications like process creation modification and deletion notifications, cumputer's name, version, Win32 processes list etc The information above mentioned can be fetched not only from local PC but also the remote machine too.. WMI can be accessed through a query langauge in two ways they are: 1. WBEMTEST.exe [GUI Tool] 2  WMIC- stands for WMI Commandline tool 3. Programatically [using Powershell and VBScript] 1. wbemtest.exe:       How to start          1. go to start                2.  go to Run                  3. Type wbemtest.exe                  4.  the following window opens up. Queries that can be run with this tool 1. Usual queries 2. Not...

Apache Commons Exec API for executing exe files from java program

In my previous post http://java-gui.blogspot.in/2016/12/api-for-executing-exe-files-from-java.html     you have seen using traditional java API to execute exe files from java program. but it is proved that it is not an ideal solution for  programs which handles IO operations and for async executables. you can find more advantages of Apache Commons Exec library over traditional API in the below link.               http://commons.apache.org/proper/commons-exec/index.html Simple example that uses the Exec library: // class to pass the command ex: cmd.exe and its arguments... CommandLine command = new CommandLine("ping"); // arguments to the command object can be passed in 2 ways... command.addArguments("localhost") ; command.addArguments("-t") ; command.addArguments("-count");    or command.addArguments(new String[]{"-t", "-count"}); //handling the IO operations gracefully using LogOutputStream abstract c...

API for executing exe files from java program.

The traditional API for calling the executable in java program is using the Runtime, Process and their methods like exec(command), waitFor() methods respectively. a simple example to execute a notepad.exe from java program using the above API is given below. Process process = Runtime.getRuntime().exec("notepad.exe"); the above code is used to launch the notepad.exe executable from java program. if there are multiple commands needs to be passed then the code would be.... String[] command = {"cmd.exe", "/C", "echo ", "%TEMP%"};  Process process = Runtime.getRuntime().exec(command); the above snippet is used to execute multiple commands in a java program. this code prints the environment variable TEMP on to the console. Handling standard output produced by the above code: BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); String output = ""; String line = ...