Feeds:
Posts
Comments

Archive for August, 2011

Visual Stack in JavaFX

We all know that Stack is an abstract data type and data structure in any programming language.  Stack works as Last in First out (LIFO).  This blog is to show the functionality of the stack visually, how to show multiple stage and how to insert the node into the layout by specifying the index, get the size of the layout ( number of elements in the layout) and finally delete the node from the layout.

I have used VBox layout for showing the element vertically.

File Name : StackElement.java


import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

/**
 *
 * @author Lawrence PremKumar
 */
public class StackElement {

    public String value;
    private Label txt;

    StackElement() {
    }

    StackElement(String val) {
        txt = new Label(val);
        txt.setFont(new Font(15));
        txt.setLayoutX(10.0);
    }

    public Group getElement() {
        Group grp = new Group();
        Rectangle rect = new Rectangle(150, 30);
        rect.setFill(Color.RED);
        grp.getChildren().addAll(rect, txt);
        return grp;
    }
}

 

File Name : StackUI.java

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
*
* @author Lawrence PremKumar
*/
public class StackUI extends Application {

     VBox layout = new VBox();

     public static void main(String args[]) {
         Application.launch(args);
     }

     @Override
     public void start(Stage stage) throws Exception {
          Group root = new Group();
          Scene scene = new Scene(root, 200, 400);
          stage.setScene(scene);
          stage.setTitle("Visual Stack UI");
          layout.setSpacing(2.0);
          root.getChildren().addAll(layout);
          stackControl();
          stage.setVisible(true);
    }

     public void stackControl() {
         Stage stg = new Stage();
         stg.setX(10.0);
         stg.setY(10.0);
         Group root = new Group();
         Scene scene = new Scene(root, 400, 50);
         stg.setScene(scene);

         Label lbl1 = new Label("Enter the data : ");
         final TextBox tb = new TextBox(30);
         Button pushBtn = new Button("Push");

         pushBtn.setOnAction(new EventHandler<ActionEvent>() {
              @Override
              public void handle(ActionEvent e) {
                   StackElement stkElement = new StackElement(tb.getText());
                   Group grp = stkElement.getElement();
                   layout.getChildren().add(0, grp);
                   FadeTransition ft = new FadeTransition(Duration.valueOf(1000), grp);
                   ft.setFromValue(0.1);
                   ft.setToValue(1.0);
                   ft.play();
                   tb.setText("");
             }
         });
         Button popBtn = new Button("Pop");
         popBtn.setOnAction(new EventHandler<ActionEvent>() {

              @Override
              public void handle(ActionEvent e) {
                   if(!layout.getChildren().isEmpty()) {
                      layout.getChildren().remove(0);
                  }
             }
         });

         HBox layout1 = new HBox();
         layout1.setSpacing(5.0);
         layout1.setLayoutX(10.0);
         layout1.getChildren().addAll(lbl1, tb, pushBtn, popBtn);
         root.getChildren().add(layout1);
        stg.setVisible(true);
   }
}

Advertisements

Read Full Post »