Skip to main content

Posts

Java Multi threaded programming basics

All modern computer hardware architectures and operating systems are capable and optimized for multi processor system and multi threaded system.  Process Vrs Thread: Process is heavy weight where as Thread is light weight. Thread is light weight because process uses it's own address space and thread shares the Process's address space. So Threads are considered to be subset of a process. more over threads consumes less OS resources in terms of physical memory and uses CPU time at most. Junior programmers think concurrency is hard.  Experienced programmers think concurrency is easy.  Senior programmers think concurrency is hard.                                                                                    _ Java Concurrency Essentials by Martin ...

Every Java programmer must read books

Every Java programmer must read and understand the following books. 1. Effective Java 2. Concurrency in practice Effective Java: This book is authored by Joshua Bloch. He took the major role in authoring the java.util package which is [collection framework] one of the java core libraries. He explains the best practices that every programmer should consider while writing java programs. Some of them are: Contract between hashcode() and equals() of Object class when programmer overrides one of these methods in their classes Exceptions concurrency: Inter thread communication, synchronization etc Immutable Objects The above points are few among others. Concurrency in practice: This book gives complete explanation about how efficient concurrency is achieved in java programs using concurrency utility classes which were introduced in Java 5 version. This book is authored by: Brian Goetz Tim peierls Joshua Bloch Joseph Bowbeer David Holmes and Doug Lea ...

Simple program which demonstrates Swing/AWT Grid layout

Simple program which demonstrates Swing/AWT Grid layout: This post is intended to the beginners of swing/awt. This below program creates a JFrame with 2 labels, 2 textfields and 2 buttons. import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; /**  * @author Nagasharath Kopalle  *  * Simple program to demontrate Gridlayout  *  */ public class GridLayoutDemo extends JFrame { private JLabel labelName = null; private JTextField textFieldName = null; private JLabel labelPassword = null; private JPasswordField passwordFieldPassword = null; private JButton buttonSubmit = null; private JButton buttonCancel = null; /** * */ public GridLayoutDemo() { initComponents(); } /** * instantiates components and adds properties. */ void initComponents() { this.setTitle("Grid Layout Dem...

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(...