View RSS Feed

All Blog Entries

  1. The Thread Class

    by , 10-29-2011 at 07:29 PM (My Java Tips)
    java.lang.Thread is used to create and control threads. A new instance of this class must be created to use a new thread. Once a thread is created, it does not start running right away. Thread.start() must be called to actually make the thread run. On calling Thread.start(), the thread begins executing in the run() method of the target class.


    Remember that a new Thread class always starts running the public void run() method of a class. There are two ways to create a thread:
    ...

    Updated 10-29-2011 at 07:32 PM by Java Tip

    Categories
    Java SE , Java ME
  2. Java Threads - synchronized

    by , 10-28-2011 at 07:27 PM (My Java Tips)
    In multithreaded environment, data may be corrupted. A was to prevent this is to prevent the interruption of critical regions.


    You have to fidn the critical region in your code which you think should be accessed by one thread at a time. Java's synchronized is used to ensure that only one thread is in a critical region at once. When the thread enters a synchronized code block, it locks on that region and no other thread can enter the critical region as long as the lock is released. ...
    Categories
    Java SE
  3. View all keyboard shortcuts

    by , 10-28-2011 at 07:22 PM (My Java Tips)
    You need to know shortcut keys to speed up your work when working in Eclipse. In this post, Ill tell you how to view currently available key bindings.


    In Eclipse, just press Ctrl+Shift+L to see a full list of the currently available key bindings. This is a great way to learn what is available in the UI and to speed up your productivity by learning more key bindings. This information is also available in the improved General > Keys preference page.

    Name:  keycompletions.gif
Views: 334
Size:  12.3 KB ...
    Categories
    Uncategorized
  4. Eclipse - Key Binding Assistance

    by , 10-28-2011 at 07:20 PM (My Java Tips)
    Using keyboard shortcuts while working in Eclipse saves a lot of effort. But some shortcuts are not easy to remember and some comprise of multiple keys like: Alt+Shift+Q Y" ("Open Synchronize View").


    Eclipse provides a little pop-up showing you the possible completions for the keys you have pressed already. In the preferences, under General > Keys, there is an "Advanced" tab. Go to this tab, and check "Help Me With Multi-Stroke Keyboard Shortcuts". ...
    Categories
    Eclipse
  5. Eclipse - Creating path variables

    by , 10-28-2011 at 06:59 PM (My Java Tips)
    This post is about setting class path variables.


    When creating a linked folder or file, you can specify the target location relative to a path variable. By using path variables, you can share projects containing linked resources without requiring team members to have exactly the same path in the file system. You can define a path variable at the time you create a linked resource, or via the General > Workspace > Linked Resources preference page.

    Name:  path-vars.png
Views: 444
Size:  14.1 KB ...

    Updated 10-28-2011 at 07:04 PM by Java Tip

    Categories
    Eclipse
  6. GoogleTalk in Eclipse

    by , 10-27-2011 at 06:58 PM (My Java Tips)
    Eclipse communication Framework is a collection of communication and collaboration tools. One of exciting tool is support for talking to any Jabber server. This means that we can easily configure Google Talk to run inside an Eclipse view.


    You should have Eclipse Europa and ECF installed. To install ECF, go to Help -> Software Updates -> Find and Install > 'Search for new features to install'. The Europa Discovery Site (http://download.eclipse.org/releases/europa) should be ...
    Categories
    Eclipse
  7. Finally Block

    by , 10-25-2011 at 07:17 PM (My Java Tips)
    You must be using try catch block in your programming practices. The finally block is used to ensure resources are recovered regardless of any problems that may occur.


    Remember that there are several different ways of using the finally block and it depends on how exceptions are handled. Let me introduce the first style. If a method throws all exceptions, then it may use a finally with no catch :

    Java Code:
    import java.io.*;
     
    
    public final class SimpleFinally
    ...

    Updated 10-25-2011 at 07:36 PM by Java Tip

    Categories
    Java SE
  8. Eclipse - Quick Access

    by , 10-25-2011 at 07:11 PM (My Java Tips)
    Eclipse Europa introduces a noteworthy feature called Quick Access . Using it where needed can save time and money.



    The Quick Access is your window to; perspectives, views, open editors, commands, menus. After pressing Ctrl + 3, start typing, and it will show you the possible matches. It filters the most likely ones first, but if you press it again, will show all the possible matches that it knows about.

    Those of you who have used Ctrl+E to open up the list ...
    Categories
    Eclipse
  9. Enabling Debugging

    by , 10-25-2011 at 07:08 PM (My Java Tips)
    There are different ways to enable debugging in Glassfish. Let's start with enabling debugging using administration console.



    To enable debugging follow the following after each server restart:

    - Login to the Administration Console using web browser:
    http://host:port/asadmin
    For example:http://localhost:4848/asadmin

    - Go to the Application Server page.

    - Select the JVM Settings tab and the General option.
    ...
    Categories
    Glassfish
  10. Working with XML data

    by , 10-25-2011 at 06:59 PM (My Java Tips)
    Hibernate allows us to work with persistent XML data same as POJOs. A parsed XML tree is actually a representation of relational data at the object level, instead of POJOs.




    Hibernate uses dom4j API for manipulating XML trees. We can write queries that retrieve dom4j trees from the database and then all modifications to the tree automatically are synchronized to the database.

    One can take an XML document, parse it using dom4j, and write it to the database ...
    Categories
    Hibernate
  11. Hibernate - Fetching strategies

    by , 10-25-2011 at 06:49 PM (My Java Tips)
    Hibernate uses fetching strategy for retrieving associated objects when an application needs to navigate the association. Fetch strategies are declared in the object relational mapping metadata, or over-ridden by a particular HQL or Criteria query.




    Hibernate defines following fetching strategies:

    - Join fetching is used to retrieve the associated instance or collection in the same SELECT, using an OUTER JOIN.

    - Select fetching is used ...
    Categories
    Hibernate
  12. Hibernate Filters

    by , 10-21-2011 at 07:40 PM (My Java Tips)
    A Hibernate filter is a global, named, parameterized filter that may be enabled or disabled for a particular Hibernate session. Filters can be used like database views.




    Filters were introduced in Hibernate3. Using filters, you can define filter criteria and attach those filters at class and a collection level. A filter criteria is actually defining a restriction clause. This is same as "where" attribute available on the class and various collection elements. ...
    Categories
    Hibernate , XML
  13. "Hello world with Java"

    by , 10-21-2011 at 04:53 AM
    Hello,
    This is my first blog on the internet EVER lol

    I have just started to read the book Thinking in Java 3rd edition, its starting out to be very helpful talking about object in memory and where this all fits in with object oriented programming. I still have not yet done any work with the Stack data structure but I am hopeful by the end of the month that is shall!

    If anyone out there reads this and wanted to talk more about object oriented programming or Java ...
    Tags: java Add / Edit Tags
    Categories
    Uncategorized
  14. Breakout Tutorial

    by , 10-21-2011 at 12:32 AM
    The first step in designing something is to think it through. What pieces should it have and how should they work together? What methods, and instance variables should a class have? Let’s start by naming the classes we will need for brick breaker (or breakout/whatever you want to name it).

    We will need a paddle which the player controls, a ball which moves around the screen, and the bricks which are meant to be broken. There will also be a canvas class which everything is drawn upon. ...
    Categories
    Uncategorized
  15. Setting JWindow or JFrame Opacity

    by , 10-17-2011 at 09:37 PM
    Ever wanted to try and set the transparency level of some JWindow's or JFrames? Here is another snippet of code to keep close by that might come in handy.

    Java Code:
    try {
    	Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
    	Method method = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);	
    		
    	method.invoke(null, /*JFrame or JWindow here*/, /*The opacity level in float value from 0.0 to 1*/);
    ...

    Updated 10-19-2011 at 09:41 AM by DuvanSlabbert

    Categories
    AWT and Swing
  16. Setting JWindow or JFrame Opaque

    by , 10-17-2011 at 09:31 PM
    Ever tried setting the visibility of a rootpane on your JWindow or JFrame without loosing visibility of the components on that JWindow or JFrame???

    Here is a little snippet of code to keep close by as it might come in handy

    Java Code:
    try {
    	Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
    	Method method = awtUtilitiesClass.getMethod("setWindowOpaque", Window.class, boolean.class);
    	method.invoke(null, /*JFrame
    ...

    Updated 10-19-2011 at 09:34 AM by DuvanSlabbert

    Categories
    AWT and Swing
  17. Adding "Global" KeyListener

    by , 10-17-2011 at 06:00 PM
    Want to add a so called "Global" Keylistener to your Java application even if it is a JWindow or JFrame without having focus on it. Just download the file in the link and extract, add the jhook.jar file to your project library and use this demo code to get the keys pressed.

    Java Code:
    Keyboard kb=new Keyboard();
            kb.addListener(new KeyboardListener() {
                public void keyPressed(boolean keydown, int vk){
                    System.out.println(vk);
    ...

    Updated 10-19-2011 at 08:33 AM by DuvanSlabbert

    Categories
    AWT and Swing
  18. Packages, accessibility/visibility, synthetic access and protected constructors

    by , 08-12-2011 at 07:21 PM
    In Java it is common to use packages, especially for libraries.
    Two common packages, that are built in to Java, are java.io and java.util.
    (Another, even more common is java.lang, but that one is imported automatically.)

    Before you can use a package [without using the reflection classes] you most import it in
    the file (not class) that uses it. To import the package java.io, you add the line
    Java Code:
    import java.io.*;
    just below your package line (if any; otherwise ...
    Categories
    Java's syntax
  19. Linux terminal magick, part 2

    by , 08-12-2011 at 05:05 AM
    In the previous part if ”Linux terminal magick” we show how to let use user use ^C and ^\ in the terminal, that was the last part of creating a graphical terminal user interface. The parts are [almost] not ordered!

    For this part we add two more functions:
    Java Code:
    /**
     * Sets the the value of the ECHO flag; iff on the input to the terminal will be echoed back while typing.
     * This can be used to hide what the user is type, for example when a password is requested.
    ...

    Updated 08-12-2011 at 05:42 AM by Hibernate

    Categories
    Terminal cues , Linux
  20. The Multiton pattern

    by , 08-12-2011 at 04:39 AM
    The Multiton pattern is a pattern missing from ”the pattern bible”. It is extensive to dual layer (and beyond), but that is left for you to use intuitively when needed, it is not too difficult to do so.

    The Multiton pattern is an extension of Singleton pattern.
    Multiton lets you create and get any number of instances, they are distinguished by keys,
    so if you want instance "a" it creates it for you the first time, and returned the created instance the next ...

    Updated 08-12-2011 at 06:16 PM by Hibernate

    Categories
    Patterns
  21. The Singleton pattern

    by , 08-12-2011 at 04:29 AM
    In mathematics a singleton (not to be confused with a simpleton) is a set with exactly one element.
    In computer science Singleton is a pattern which enforces the existence of only up to one instance of a class.

    Singleton is sometimes considered an anti-pattern, and it's famous for being considered unnecessary in the world of Python (where you duck type).
    Singleton is however useful, it is a nice and clean why to restrict the number of instances, to only one.
    In ...

    Updated 08-12-2011 at 06:13 PM by Hibernate (typo)

    Categories
    Patterns
  22. Linux terminal magick, part 1

    by , 08-12-2011 at 01:15 AM
    Are you a hardcore GNU/Linux user like me?
    Then you are probably sick of Java (natively) not letting you print in the terminal with colours.
    In a series of Web logs, I will describe how to, in GNU/Linux, print in the terminal with colours and other stuff.
    Keep in mind that you need a TTY (e.g. gnome-terminal, xterm, Linux VT (linux)), Eclipse's built in console will not do.

    We will begin with letting Java accept ^C and ^\ (also know as ^4) from the terminal. ...

    Updated 08-12-2011 at 05:42 AM by Hibernate

    Categories
    Linux , Terminal cues
  23. Variadic functions (varargs)

    by , 08-12-2011 at 12:47 AM
    Ever heard of variadic functions of varargs.
    It is my favourite synthetic sugar (although I must admit i prefer cane sugar).

    Variadic functions have undefine arity (the parameter is called a "vararg" (variadic argument)), meaning
    that it lets you use any amount of parameters when invoking it.

    For example:
    Java Code:
    public class MyClass
    {
        private static void print(final String prefix, final int... lines)
        {
    ...

    Updated 08-12-2011 at 05:44 AM by Hibernate

    Categories
    Java's syntax
  24. Unusual members, part 2

    by , 08-12-2011 at 12:22 AM
    Just like there is a static, otherwise headless, method, the class initialiser, this is an object initialiser (or just initialiser), that is a non-static and completely headless.

    Java Code:
    public class MyClass
    {
        public static class Subclass extends MyClass
        {
            {
                System.out.println("Subclass Initialiser 1");
            }
            public Subclass()
            {
                System.out.println("Subclass
    ...

    Updated 08-12-2011 at 05:45 AM by Hibernate

    Categories
    Java's syntax
  25. Unusual members, part 1

    by , 08-12-2011 at 12:09 AM
    Did you ever want to run something the first, and only first, time a class was being used.
    Perhaps wanted to do some complex calculations and assign it to a constant.

    One way to do this is by writing, for example:
    Java Code:
    public class YourClass
    {
        public static final int YOUR_CONSTANT = constantValue();
    
        private static int constantValue()
        {
            int value1 = 0, value2 = 1;
            int value = 0;
            for
    ...

    Updated 08-12-2011 at 05:45 AM by Hibernate

    Categories
    Java's syntax
  26. Legal forward reference assignment weirdness on declarations

    by , 08-11-2011 at 11:38 PM
    Observer the glorious code by your's truly Hibernate:

    Java Code:
    public class YetAnotherClass
    {
        //public static final int YET_ANOTHER_CONSTANT = YET_ANOTHER_CONSTANT; //Compile-time error: illegal forward reference
        public static final int ANOTHER_CONSTANT = YetAnotherClass.ANOTHER_CONSTANT; //OK!
    
        //public int yetAnotherVariable = yetAnotherVariable; //Compile-time error: illegal forward reference
        public int anotherVariable = this.anotherVariable
    ...

    Updated 08-12-2011 at 05:46 AM by Hibernate

    Categories
    Java's syntax
  27. The Decorator pattern

    by , 07-23-2011 at 02:42 PM (JosAH's blog)
    Greetings,

    the previous blog article talked a bit about the Visitor design pattern. This article talks a bit about additional functionality that is sometimes wanted, i.e. the functionality is optional. Assume there is a lot of optional functionality that people want.

    This article discusses the Decorator (or 'Wrapper') pattern. For the sake of the example we'll use array manipulation. People always fiddle diddle with arrays, i.e. they copy values from one array to another, ...

    Updated 07-23-2011 at 02:45 PM by JosAH

    Categories
    Uncategorized
  28. The Visitor pattern

    by , 07-23-2011 at 02:35 PM (JosAH's blog)
    Greetings,

    Java claims to support OO, so why not use it? In this article we're going to talk a bit about when and why to apply certain patterns. We'll start with the Visitor pattern. The pattern is also named 'double dispatch' which will become clear near the end of this little article. Here's the story:

    Suppose some time ago you wrote a bunch of beautiful, efficient classes; they're real gems. For the sake of this article let's use the following classes:
    ...
    Categories
    Uncategorized
  29. Java ArrayList : Generics & Sorting

    by , 07-21-2011 at 06:20 AM


    Abstract

    Java ArrayList (ArrayList (Java 2 Platform SE 5.0)) class is a fastest and easiest way to represent one-dimensional array. ArrayList not synchronized, that is cross thread access may cause problems. It holds an ordered item sequence of items (objects) like an array, but there are differences:

    • Size of an ArrayList could change dynamically, by adding or removing elements
    • An ArrayList can hold only object references, not primitives

    ...
    Attached Thumbnails Attached Files
  30. Compilers

    by , 07-16-2011 at 04:41 PM (JosAH's blog)
    Greetings,

    Frequently a question pops up in this forum (or other forums for that matter) where the OP wants to implement a calculator or wants to compile an expression given in text (String) form. Often they get lost in tokenizers or regular expressions or whatever. This article describes how a compiler (for expressions) can be designed and implemented. The attachment contains a zip file with all the sources for a complete compiler.

    Top level architecture
    ...

    Updated 07-18-2011 at 05:18 PM by JosAH

    Categories
    Uncategorized
    Attached Thumbnails Attached Files
Page 15 of 18 FirstFirst ... 51314151617 ... LastLast