Thinking in programming

Things I like to talk about programming

Can “your” language beat this? – Part 1

with 2 comments

Important: This first post is discussed on JavaHispano.org

This series of posts is not intended to demonstrate the superiority of one language over others, the aim is to find the best way to write code snippets that a GUIs developer faces daily. When i said “the best way“, i mean a proper balance between flexibility and readability, which in my view, defines the quality of a language. The idea is that if you knows a language that allows to improve the way of codifying the snippet shown here (surely you knows), then you published that piece of code here, and then we will evaluate the best. As well are welcomes improved versions of the ones I have posted, and of course, comments in general.

The main reason why i choose JavaFX, is frankly because of i think that is the language that gives more agility to work with graphics (at the moment).

This first part aims to show how to bind two objects, in this case, the color of an object is determined by the text of a text field. If the text inside the text field is ‘aeiou’ then the circle will be filled with the green color, otherwise will be filled with the red color.

Note that understanding the processTheText function as the “controller”, so you can notice the complete separation between the view and the logic.

/*
 * DoItBetter1.fx
 *
 */

package doitbetter;

import javafx.scene.control.TextBox;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

var theText = "";

Stage {
    title: "Binding between GUI controls"
    width: 400
    height: 200
    scene: Scene {
        content: HBox {
            spacing: 10
            content: [                
                TextBox{                    
                    text: bind theText with inverse
                }
                Circle{                    
                    radius: 10 centerY: 10
                    fill: bind processTheText(theText)
                }
            ]
        }
    }
}

function processTheText(auxText){
    if(auxText == "aeiou") Color.GREEN else Color.RED
}

Advertisements

Written by roger.padilla

January 15, 2009 at 18:11

Posted in Uncategorized

Tagged with

2 Responses

Subscribe to comments with RSS.

  1. Interesting snippet using JavaFX, I’ll try to post the same using Silverlight and Flex when I get a chance.

    Guillaume Gros

    January 18, 2009 at 12:59

  2. Hi Guillaume, that would be great.

    As well you can visit the discussion in JavaHispano.org

    http://www.javahispano.org/contenidos/es/puedes_hacerlo_mejor_en_tu_lenguaje_de_programacion/

    rogerpadilla

    January 22, 2009 at 11:14


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: