Pages

Thursday, June 14, 2012

Wednesday, June 6, 2012

JavaFx - CHART example

We would like to have a chart of the imported fruits. 
Let' see in JavaFx the PieChart diagram:

public class JavaFXApplication1 extends Application {

    public static void main(String[] args) {
        launch(args);
    }
   
    @Override
    public void start(Stage primaryStage) {
       
      primaryStage.setTitle("Chart example");
      ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
                new PieChart.Data("Grapefruit", 13),
                new PieChart.Data("Oranges", 25),
                new PieChart.Data("Plums", 10),
                new PieChart.Data("Pears", 22),
                new PieChart.Data("Apples", 30));
        PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");       
       
        StackPane root = new StackPane();
        root.getChildren().add(chart);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
               
    }
}

Java - read from console, dos prompt

The JAVA file is the following, for an example of reading input values from console:

package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {
       
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       
        String prompt = "";       
        System.out.println("Enter your name: ");
        prompt = reader.readLine();
        if (prompt.equals("\t"))
            System.out.println("You entered a TAB from your keyboard.");
        else
            System.out.println("Your name is: "+prompt);
       
    }
}

Friday, June 1, 2012

Java - sorting strings with Hungarian characters

        Let's sort some Hungarian strings in alphabetical order:

        String[] words = {"Bruce","Géza","Ákos"};

        Collator c = Collator.getInstance(new Locale("hu"));      
        Arrays.sort(words, c);


        for(int m=0; m<words.length; m++)
            System.out.print(words[m]+", ");

Tuesday, May 29, 2012

Java - Enumerated types

To enumerate some constant values, and/or associate to them methods or values, we can use enum in java. Enum types in Java are considered like classes, they can have constructors too, can implement interfaces, but can have no descendants. Let see, how we work with enum in java, through an example that classifies programming languages.
Create your own test class, and then test the following code:

    private enum prog_language {
        //enumerated constants
        java(true), c(true), basic(true), php(true), delphi(true), csharp(false), cplus(false);
       
        //fields to associate with the enum types
        private int position;
        private boolean usedbyme;
       
        //constructor
        prog_language(boolean bUsedByMe) {
            usedbyme = bUsedByMe;
        }           
        //getters and setters of the fields
        public int getPosition() {
            return position;
        }
        public void setPosition(int position) {
            this.position = position;
        }          
        public boolean isUsedbyme() {
            return usedbyme;
        }       
    };

.......................................       
        int i = 0;
        for (prog_language p : prog_language.values()) {
            i++;
            p.setPosition(i);
        }                              
        System.out.print(prog_language.valueOf("java").name());      
        System.out.println(" - at position: "+prog_language.valueOf("java").getPosition());
        System.out.println("I am using this language: "+prog_language.valueOf("java").isUsedbyme());

   
 
Enum types can also participate in switch-case statements too.

Thursday, May 24, 2012

Java - Data Structures

Data Structure       Advantages                Disadvantages



Array
Quick insertion
(very fast access if index known.)
Slow search, 
Slow deletion, 
fixed size.
Ordered Array
Quicker search
Slow insertion,
Slow deletion,
fixed size. 
Stack
Provides last-in, first-out access
Slow access to other items
Queue
Provides first-in, first-out access
Slow access to other items
Linked list
Quick insertion, quick deletion
Slow search
       ArrayList           
(is same as a Vector. Vectors are
synchronized, can be thread safe, but
slower)
Binary Search, Sort
Slow insertion, deletion
Binary tree
Quick insertion, deletion (if tree remains balanced), and search.
Deletion algorithm is complex
Red-black tree
Quick insertion, deletion (this tree is always balanced), and search.
Complex
2-3-4 tree
Quick insertion, deletion (this tree is always balanced, is good for disk storage progr.), and search.
Complex
Hash table (~arrays)
Very fast access if key known. Fast insertion
Slow deletion, access slow if key not known, inefficient memory usage.
Heap (~LinkedList)
Fast insertion, deletion, access to largest item.
Slow access to other items
Graph
Models real-world situations.
Slow and complex.
                       

Java - Maps / storing values in key-value pair

        Map<String, Integer> mPair = new HashMap<String, Integer>();       

        mPair.put("one", new Integer(1));
        mPair.put("two", new Integer(2));
        mPair.put("three", new Integer(3));
        System.out.println(mPair.values());

        if (mPair.containsKey("two"))
            System.out.println(mPair.get("two"));

Thursday, May 17, 2012

Vaadin - TextField componet, value change listener

Working with TextField component in Vaadin framework web environment, is much like, working with components in swing/awt applications. Here, we also have events and listeners.
For example, changing a value in a TextField componet, can be captured right after the field, loses focus.



Just have the following class and add to your Vaadin web application:

public class MyApplication extends Application {
    //global components
    TextField tfInput;
    Window mainWindow;
   
    //main method - init()
    @Override
    public void init() {
    mainWindow = new Window("MyApplication");
        tfInput = new TextField("Enter your Name: ");
        tfInput.setValue("empty");
       
        //handling the change of the texfield value property:
        tfInput.addListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                // Assuming that the value type is a String
                String value = (String) tfInput.getValue();               
                Window.Notification notif = new Window.Notification("Be warned "+value+"!",
                        "<br/>This message warning, after a field value change, stays centered, till you do sthg!",
                        Window.Notification.TYPE_WARNING_MESSAGE);
                notif.setPosition(Window.Notification.POSITION_CENTERED);
                mainWindow.showNotification(notif);
            }           
        });       
        // Fire value changes immediately when the field loses focus
        tfInput.setImmediate(true);       
               
    mainWindow.addComponent(tfInput);
    setMainWindow(mainWindow);
    }
}

The same thing you can achieve by binding the TextField component to an ObejectProperty value. The change of this ObjectProperty value, is immediately reflected anywhere.

public class MyApplication extends Application {
    Double trouble = 42.0;
    final ObjectProperty<Double> property = new ObjectProperty<Double>(trouble);
      
    @Override
    public void init() {
    Window mainWindow = new Window("MyApplication");
        TextField tf = new TextField("The Answer",property);
        tf.setImmediate(true);
       
        Label feedback = new Label(property);
        feedback.setCaption("The Value");
       
        Panel pnl = new Panel();
        pnl.addComponent(tf);
        pnl.addComponent(feedback);
       
    mainWindow.addComponent(pnl);
    setMainWindow(mainWindow);
    }
}

Just try it and test, this code on: http://lehelsipos.appspot.com/

Wednesday, May 16, 2012

Creating an embedded JavaDB with netbeans with a given path at your local hard disk

Using the Netbeans IDE it is very easy to create an embedded JavaDB database. Just follow the steps:
1. open your netbeans ide :)
2. go to services tab
3. open Databases then Drivers and right click the Java DB (Embedded)
4. after "connect using" in the jdbc url write the following:
jdbc:derby:c:/yourPath/DB/yourDb;create=true
Success.