Skip to main content

Welcome to my new website !
   Posted:


Welcome to my new english website :)

I've moved all my english stuff from Developpez.com to make here a completely english website.

I hope you'll find here some interesting stuff. I will publish articles and posts about Java, Spring, Swing, OSGi, Hardware and Web and perhaps others technologies.

Don't hesitate to say me what you think about this website.

Comments

OSGi and cyclic dependencies
   Posted:


I'm currently working on the "bundlisation" of the JTheque Core. I choose to cut core into several bundles each one representing a service provided by the core.

I quickly have experencied problems with

I quickly realized that the decoupling of my various services was almost nil. I had a huge number of dependencies for each bundle and worse, I had a lot of cyclic dependencies, either direct or indirect.

For information, a direct cyclic dependency is a situation in which a bundle X depends on a bundle that depends on Y who is also depending itself of X. An indirect dependency cycle is the situation where X depends on Y, Y depends on Z and Z depends on X. I speak here of bundles, but it may relate to projects, classes or packages.

Note that the different techniques that I will present also apply for dependencies between components non-OSGi, the principles are exactly the same.

Read more…

Comments

The version 2.0 of Sonar has been released
   Posted:


The developers of Sonar have just released the version 2.0 of Sonar.

Several major features:

  • Architecture analysis : The architecture of the projects are now analyzed. We can see a matrix of project dependencies and a list of dependencies to delete to improve architecture quality.
  • Object oriented analysis : New metrics for the quality of the object oriented conception.
  • Improvements of the user interface.

Only good stuff :)

I didn't test this new version, i will wait that my plugins be compatibles with the new version, but i will give informations of the new version once installed.

More informations on the Sonar website.

Comments

Manage focus with Swing in Java
   Posted:


1. Introduction

The focus is the "selected" state of a component. The component who has the focus is the active component. It's possible to ask focus fror a specific component but normally this is perfectly managed by Swing.

With this tutorial, you will learn how to ask focus for a specific component and how to define an order of focus. We will also learn a bit about the validation of input fields, the focus listen and the utility of KeyboardFocusManager.

2. Ask focus

Normally, the focus is managed by a component on the mouse click or when we come to a component with keyboard. A component who is focused is often visually modified, with a special border or an other background color.

To manage the focus of the windows, it's a little bit different and depends of the operating system, but nothing can give you the guarantee to have focus. On Windows, you can obtain focus on a window using toFront but nothing is guraranteed.

For the components, you can use the requestFocusInWindow() method :

component.requestFocusInWindow();

To obtain the focus, you have to ask focus after the add of the component but before the display of the window.

Before Java 1.4, you needed to use the requestFocus() method, but now it is not a good idea to use it, because it give also focus to the window of the component, and that's not always possible.

3. Navigation keys

By default, we navigate in an application with the Tab and Shift+Tab keys. But we can edit these keys.

For that, we have to get the keys with getFocusTraversalKeys(int id) and add the new key. By example, here is how to add the Enter key for the forward focus navigation :

Set keys = textField.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newKeys = new HashSet(keys);
newKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
textField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newKeys);

If you want to add a key for the backward navigation, you need to use BACKWARD_TRAVERSAL_KEYS instead of FORWARD_TRAVERSAL_KEYS.

4. Validate an input

You often need to validate to validate the inputs of the user in a field. The best way to do that is to test the content directly after the focus lost. Of course, there is still Formatter to do that, but it's possible that you must use the validation on a custom component.

To do that, we'lll use InputVerifier. This InputVerifier enable to get the value of a field when this field lost focus. If the value is not valid, the InputVerifier can execute a particular action by example set the old value in the field insted of the new invalid value or give focus again to the field.

To use it, we've to create a class extending InputVerifier and use the setInputVerifier() method on the component to test :

CustomInputVerifier verifier = new CustomInputVerifier();
...
monComposant.setInputVerifier(verifier)

The inputVerifier class has only two methods :

  • verifiy(JComponent input) : Indicate if the value of the component is correct. It's must be overriden.
  • shouldYieldFocus(JComponent input) : This method idnicate if the component can loose focus or not. If it's return true, the focus change normally to the next component else the focus remains in the

current component while the input is invalid. By default, it's only return the value of verify().

For lisibility reasons, it's better to only implement the test of the input value in the verify method and add the other functionalities by example a beep it it's invalid in the shouldYieldFocus method.

We will see a simple example of InputVerifier. This verifier verify that the value is a number greater than zero but lower than x. If the value is not valid, we simple emit a beep.

import java.awt.Toolkit;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;

public class NumberInputVerifier extends InputVerifier {
    private int max = 0;

    public NumberInputVerifier(){
        this(100);
    }

    public NumberInputVerifier(int maximum){
        super();

        this.max = maximum;
    }

   public boolean shouldYieldFocus(JComponent input) {
        boolean valid = verify(input);

        if (valid) {
            return true;
        } else {
            Toolkit.getDefaultToolkit().beep();
            return false;
        }
    }

    public boolean verify(JComponent input) {
        JTextField field = (JTextField)input;

        String value = field.getText();
        int intValue;

        try {
            intValue = Integer.parseInt(value);
        } catch (NumberFormatException pe) {
            return false;
        }

        if(intValue < 0 || intValue > max){
            return false;
        }

        return true;
    }
}

You can of course make a lot of things with this class. You can use it to change the format of a number adding " or , or modify everithing else in the input component.

You can also use the same InputVerifier for several components. You just have to test the concerned component with the parameter of the verify method. But don't make too complex InputVerifier.

5. Listen the focus

A first way to know which component has the focus is to use a focus listener. You just have to create a class implementing FocusListener and add this listerner to every components you want to manage. Then, you just have to make things in the methods of the listener : focusGained and focusLost. This way is quite simple, but if you have a lot of components, it will be really heavy to implement.

An other way is to use the KeyboardFocusManager class. You just have to add a PropertyChangeListener on this class and verify the property focusOwner. You can also listener some other things : the focus of the windows, the changes on the focus order, ... You can find here a complete liste of properties. This times, you'll directly see all the focus changes for all the components in your application. If you want to restrain the set of components, you have to use the first way with FocusListener.

Here is an example with KeyboardFocusManager :

KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();

focusManager.addPropertyChangeListener(
    new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String properties = e.getPropertyName();
            if (("focusOwner".equals(properties)) && (e.getNewValue() != null)) {
                Component component = (Component)e.getNewValue();
                String name = component.getName();

                System.out.println(name + " take focus");
            }
        }
    }
}

6. Manage the order of focus

Swing has a correct focus order by default and normally it's enough. But you can change this order, by example to navigate in a complex form. By default, Swing determinate the focus order using the order of adding the components to the content pane.

Before Java 1.4, you had to use setNextFocusableComponent() on every component to specify the next component to have the focus. This method is now deprecated. We'll use the LayoutFocusTraversalPolicy class to define the focus order in a Swing application.

For that, we have to create a new class extending FocusTraversalPolicy and define this set of methods :

  • getComponentAfter : Indicate which component will have focus after the component in parameter.
  • getComponentBefore : Indicate which component has the focus before the component in parameter.
  • getDefaultComponent : Indicate which component is the default component of the container on which we've applied the policy. It's used when we swith to this focus cycle.
  • getLastComponent : Indicate which component is the last of this continer. It's used when we swith to this focus cycle.
  • getFirstComponent : Indicate which component is the first of this container. It's used when we swith to this focus cycle.
  • getInitialComponent : Indicate which component must have the focus when the window is displayed.

Here is a little example :

public class TestFocusTraversalPolicy extends FocusTraversalPolicy {
    public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {
        if (aComponent.equals(component1)) {
            return component2;
        } else if (aComponent.equals(component2)) {
            return component3;
        } else if (aComponent.equals(component3)) {
            return component4;
        } else if (aComponent.equals(component4)) {
            return component5;
        } else if (aComponent.equals(component5)) {
            return component1;
        } 

        return component1;
    }

    public Component getComponentBefore(Container focusCycleRoot, Component aComponent) {
        if (aComponent.equals(component1)) {
            return component5;
        } else if (aComponent.equals(component2)) {
            return component1;
        } else if (aComponent.equals(component3)) {
            return component2;
        } else if (aComponent.equals(component4)) {
            return component3;
        } else if (aComponent.equals(component5)) {
            return component4;
        }

        return component1;
    }

    public Component getDefaultComponent(Container focusCycleRoot) {
        return component1;
    }

    public Component getLastComponent(Container focusCycleRoot) {
        return component5;
    }

    public Component getFirstComponent(Container focusCycleRoot) {
        return component1;
    }
}

The disadvantage of this technique is that the policy must know all the components. That why we often use an inner class to give it access to the member of the current class. Here is a simple example :

public class View {
    private Component component1;
    private Component component2;
    private Component component3;
    private Component component4;
    private Component component5;

    class TestFocusTraversalPolicy extends FocusTraversalPolicy {
        //Traversal policy methods
        }
}

You can also use an inner anonymous class, but with that kind of class, you cannot use it for several components.

To apply this policy, you have to use the setFocusTraversalPolicy() method of the Container class :

TestFocusTraversalPolicy newPolicy = new TestFocusTraversalPolicy();
myContainer.setFocusTraversalPolicy(newPo

Of course you can define several strategies for several panels or the same policy for several panels.

If you want to restore the default focus policy, you just have to call setFocusTraversalPolicy() with null as parameter.

You can also edit the default FocusTraversalPolicy with one of yours with the setDefaultTraversalPolicy() method from the KeyboardFocusManager class.

6.1. Simplification

But it's not very easy to do that and that can quickly be heavy. So here is a simple class to manage the positions of all the components with an integer :

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.util.HashMap;

/**
 * A focus traversal policy who manage the order of components in the focus cycle using a int.
 * The first component of the cycle  has the position of 0 and all the others follows with an interval of 1.
 * The last component must have the position (size() -1).
 *
 * @author Baptiste Wicht
 *
 */
public class MapFocusTraversalPolicy extends FocusTraversalPolicy {
    private HashMap components = new HashMap();
    private HashMap positions = new HashMap();

    /**
     * Add the component to the order with the specfieid position.
     *
     * @param component The component to add.
     * @param position The position of the component in the focus cycle root.
     */
    public void addComponent(Component component, int position){
        components.put(position, component);
        positions.put(component, position);
    }

    @Override
    public Component getComponentAfter(Container parent, Component component) {
        int position = positions.get(component);

        if(position = positions.size() - 1){
            position = 0;
        }

        return components.get(position + 1);
    }

    @Override
    public Component getComponentBefore(Container parent, Component component) {
        int position = positions.get(component);

        if(position = 0){
            position = positions.size() - 1;
        }

        return components.get(position - 1);
    }

    @Override
    public Component getDefaultComponent(Container parent) {
        return components.get(0);
    }

    @Override
    public Component getFirstComponent(Container parent) {
        return components.get(0);
    }

    @Override
    public Component getLastComponent(Container parent) {
        return components.get(components.size() - 1);
        }
}

Vous can use this class as every other focus traversal policy we saw before. Just add all the components before using the policy.

An other way that you can explore it's to make all your components implementing an interface like Focusable with a getFocusPosition() method and use a policy to make the order with that Focusable components.

7. The KeyboardFocusManager class

There is also other utilities to the KeyboardFocusManager class.

First, you can change the component who has the focus with the focusXXX methods. By example, you can use the focusNextComponent() method to give the focus to the next component. you can also give the focus to a specific component with the setFocusOwner method or to a window with with the setActiveWindows method.

Then, you can also get the active window, the component with the focux and the container who contains the active component. If the components are in an other thread, you have to use the getGlobalXXX methods to get it.

8. Conclusion

To conclude, you can do almost everithing with the focus system. You can modify the order of focus for the components, say which components must have the focus, make tests on the input fields, know who has the focus. But that's sometimes weird when we start. But once we've understand which classes to use, it is quite simple.

Comments

Tip : The bookmarks bar disappears on Google Chrome !
   Posted:


I Found that my bookmarks bar on Google Chrome disappear sometimes. This behavior is very confusing, is searched a few and i find that it was me that make it disappears and appears.

On Google Chrome, you can hide/display the bookmarks bar using + .

Hope that will help someone.

Comments

Hardware Guide : The memory chips
   Posted:


1. Introduction

The RAM memory (Random Access Memory) of a computer is used to stock data during their treatment. The operating system will load in memory the useful files and all the datas of the launched programs. Its necessary to have an adequate quantity of memory.

The RAM has two important caracteristics :

  • Speed data access : The access to data in RAM is very fast, a lot more than access to hard disks. It's essential to give access to data in a quick way.
  • Volatility : The data doesn't remain in memory after computer shutdown. So this is not a storage device only a temporary data storage device.

If a computer doesn't have enough memory, the operating system will simulate virtual memory using the hard disks, but like said above, the access on the hard disk is a lot slower than memory and the performances of the system will decrease.

So this is essential to make the good choice when choosing memory chips.

Memory Chip

Read more…

Comments

JTheque is going to OSGi
   Posted:


Just a little post to inform you that i'm currently migrating JTheque Core to OSGI / Spring Dynamic Modules. The core will be fractioned into several bundles. The JTheque Modules will also be OSGi bundles.

It will increase the modularity of the different services of the Core. And it will also be a more standard way to develop modules. And last but not least, it's an opportunity for me to learn OSGi and Spring Dm.

Comments

Develop a modular application with JTheque Core 2.0.3
   Posted:


1. Introduction

This article will teach you how to develop a first application with JTheque Core.

We will develop a basic modular application, like an hello world. This little program will be composed of :

  • A JTheque application
  • A JTheque module who display an Hello World on the main view
  • A module who will add an option menu to display one more time "Hello World"

Read more…

Comments

Hardware Guide : The Hard Disks
   Posted:


You want buy a new computer, but you cannot which hard disk to buy ? Or you want to buy a new hard disk for your computer ? So, this article is done for you. It will describe all the things you must be careful of when choosing a hard disk.

Read more…

Comments

The reserved keywords of the Java Language
   Posted:


This article will present you all the keywords of the Java Language and their purpose.

First of all, what's a reserved keyword ? It's a keyword of the language, for example true.

What does that change for the developer ? You cannot use one of there keywords to name a variable, a method, a class or a package. So you cannot have something like that :

Boolean true = new Boolean(true);

public class true{}

package com.wichtounet.true;

This example will not pass the compilation. But you can use it into a variable identifier or by changing the case :

Boolean trueBoolean = new Boolean(true);

public class TrueBoolean{}

package com.wichtounet.trueBoolean;

It's also really important for a developer to know all the keywords and to know how to use them.

Read more…

Comments