Wikia

Planet JFX

SwingComponents

Talk1
118pages on
this wiki

30 Essential Controls Edit

See Designing Web Interfaces

Extending SwingComponent class Edit

Code:

import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javafx.ext.swing.SwingComponent;


class MySwingButton extends SwingComponent{

    var button: JButton;

    public var text: String on replace{
        button.setText(text);
    };
    
    public var action: function();

    public override function createJComponent(){
        button = new JButton();
        button.addActionListener(
            ActionListener{
                public override function actionPerformed(e:ActionEvent){
                    action();
                }
            }
        );
        return button;
    }
}

Extending swing component

Example:

import javafx.stage.Stage;
import javafx.scene.Scene;

Stage {
    title: "Swing Component Example"
    scene: Scene {
        width: 250
        height: 100
        content: [
            MySwingButton{
                text: "Click Me!"
                action: function(){
                    println("Hello World!");
                }
            }
        ]
    }
}


Swing Components Edit

wordField Edit

Swing password field

Code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.ext.swing.SwingComponent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JComponent;
import javax.swing.JPasswordField;

public class SwingPasswordField extends SwingComponent {

    var passwordField: JPasswordField;

    public var text:String;

    var external:Boolean = false;
    
    var triggered:String = bind text on replace oldValue{
        if(external){
            external = false;
        }
        else
        passwordField.setText(text);
    }
    public var action: function();

    public override function createJComponent():JComponent{
        passwordField = new JPasswordField();
        passwordField.setColumns(12);

        passwordField.addActionListener(ActionListener{
            public override function actionPerformed(e:ActionEvent){
                action();
            }
        });

        passwordField.getDocument().addDocumentListener(DocumentListener{


            public override function insertUpdate( e:DocumentEvent) {
                external = true;
                text = passwordField.getText();
            }

            public override function removeUpdate( e:DocumentEvent) {
                external = true;
                text = passwordField.getText();
            }

            public override function changedUpdate( e:DocumentEvent) {
                external = true;
                text = passwordField.getText();
            }

        });

        return passwordField;
    }

}

hjhlkjhlkjhlkjh

Example:


Swing password example 1 Swing password example 2


import javafx.stage.*;
import javafx.scene.*;

import javafx.scene.text.*;
import javafx.scene.paint.*;
import javafx.scene.effect.*;

import javafx.ext.swing.*;

var login = false;
var password = "";
var systemPassword = "12345";

Stage {
    title: "My System"
    scene: Scene {
        width: 350
        height: 150
        content: bind if(not login) Group{
                content: [
                    SwingLabel{
                        text: "Enter password:"
                    },
                    SwingPasswordField{
                        translateX: 100
                        text: bind password with inverse
                        action: function() {
                            println("password: {password}");
                            login = (password == systemPassword);
                        }

                    }
                ]
            } else Group{
                content: [
                    Text {
                        x: 10 y: 30
                        fill: Color.BLUE
                        font: Font { size: 20}
                        effect: Reflection{}
                        content: "Welcome to the system!"
                    }SwingButton{
                        translateX: 220
                        translateY: 10
                        text: "Logout"
                        action: function(){
                            password = "";
                            login = false;
                        }
                    }
                ]
            }
    }
}

ProgressBar Edit

Progress bar

Code:

import javax.swing.JProgressBar;
import javafx.ext.swing.SwingComponent;

class SwingProgressBar extends SwingComponent{

    var progressBar: JProgressBar;

    public var  text:String on replace{
        progressBar.setString(text);
        progressBar.setStringPainted(true);
    };

    public var  min:Integer = 0 on replace{
        progressBar.setMinimum(min)
    };

    public var  max:Integer = 100 on replace{
        progressBar.setMaximum(max)
    };
    
    public var value:Integer = 0 on replace{
        progressBar.setValue(value);
    };


    public override function createJComponent(){
        progressBar = new JProgressBar(0, 100);
        return progressBar;
    }
}

Example:

var progress = 0; 

Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame {
            time: 5s
            canSkip: true
            values: [
                progress => 10 tween Interpolator.LINEAR
            ]
        }
    ]
}.play();

Stage {
    title: "Progress Bar Example"
    scene: Scene {
        width: 220
        height: 150
        content: [  
            SwingLabel{
                text: bind "Progress: {progress}"
            },
            SwingProgressBar{
                translateY: 50
                min: 0
                max: 10
                value: bind progress
            }
        ]
    }
}

TabbedPane Edit

Tabbed pane

Code:

import javafx.ext.swing.*;

import javax.swing.JComponent;
import javax.swing.JTabbedPane;

class SwingTab{
    public var title: String;
    public var content: SwingComponent;

}

class SwingTabbedPane extends SwingComponent{

    var tabbedPane: JTabbedPane;

    var updateComponentFlag: Boolean = false;

    public var selectedIndex: Integer = -1 on replace{
        if( not updateComponentFlag){
            if((-1 < selectedIndex) and ( selectedIndex  < tabbedPane.getTabCount() )){
                tabbedPane.setSelectedIndex(selectedIndex);
            }
        }
    };




    public var tabs: SwingTab[] on replace oldValue[lo..hi] = newVals{
        for (tab in oldValue[lo..hi]){
            tabbedPane.remove(tab.content.getJComponent().getParent());
        }
        for (tab in newVals){
            tabbedPane.addTab(tab.title,tab.content.getJComponent());
        }

    };


    override function createJComponent(): JComponent {
        tabbedPane = new JTabbedPane();

        tabbedPane.addChangeListener(
            javax.swing.event.ChangeListener{
                override function stateChanged(e: javax.swing.event.ChangeEvent){
                    updateComponentFlag = true;
                    selectedIndex = tabbedPane.getSelectedIndex();
                    updateComponentFlag = false;
                }
            }
        );
        return tabbedPane;
    }

}


Example:

import javafx.stage.Stage;
import javafx.scene.Scene;


Stage {
    title: "Tabbed Pane"
    scene: Scene {
        width: 300
        height: 150
        content: SwingTabbedPane{
            width: 300
            height: 150

            tabs: for(n in [1..5]) SwingTab{
                title: "Tab: {n}"
                content: SwingLabel{
                    text: "Label: {n}"
                }
            }

        }
    }
}

EditorPaneEdit

Editor pane

Code:

import javax.swing.*;
import javafx.ext.swing.*;
import java.awt.event.KeyAdapter;



public class SwingEditorPane extends SwingComponent{

    var editorPane: JEditorPane;
    var updateComponentFlag: Boolean = false;


    public var text: String on replace{
        if(not updateComponentFlag){
            editorPane.setText(text);
        }
    };

    public var editable: Boolean = true on replace{
        editorPane.setEditable(editable);
    };

    public var onKeyUp: function(keyEvent :java.awt.event.KeyEvent);


    function updateComponentField(){
            //System.out.println("[editor pane] updateComponentField!");
            updateComponentFlag = true;
            text = editorPane.getText();
            updateComponentFlag = false;
    }

    override function createJComponent(): JComponent {
        editorPane = new JEditorPane();
        editorPane.setEditable(editable);

        var keyListener = KeyAdapter{
            override function keyReleased(e: java.awt.event.KeyEvent) {
                updateComponentField();
                if(onKeyUp != null){onKeyUp(e ); }
            }
        };

        editorPane.addKeyListener( keyListener );
        var scrollPane = new JScrollPane(editorPane);
        return scrollPane;
        //return editorPane;

    }

}

Example:

import javafx.stage.Stage;
import javafx.scene.Scene;

Stage {
	title : "MyApp"
	scene: Scene {
		width: 400
		height: 400
		content: SwingEditorPane{
                    width: 200
                    height: 200
                    text: "Line1\nline2"
                }

	}
}

TreeEdit

Swing tree

Code:

import javafx.ext.swing.*;
import javax.swing.*;
import javax.swing.tree.*;


import java.lang.Object;
import javafx.scene.Scene;

class MyTreeCell {

    public var text: String;
    public var cells: MyTreeCell[];
    public var value: Object;

    override function toString ():String { 
        return text;
    }

}

class MyTree extends SwingComponent{

    var tree: JTree;
    var model: DefaultTreeModel;

    public var selectedValue: java.lang.Object;

    public var action: function(obj:Object);


    public var root: MyTreeCell on replace{
        if (root != null){
            model.setRoot(getCell(root));

            var obj = model.getRoot();
            var tp = new TreePath(obj);
        }
    };


    override function createJComponent(): JComponent {
        tree = new JTree();
        model = tree.getModel() as DefaultTreeModel;

        var  mouseListener = java.awt.event.MouseAdapter {
            override function mousePressed(e: java.awt.event.MouseEvent) {
                var selRow = tree.getRowForLocation(e.getX(), e.getY());
                var selPath = tree.getPathForLocation(e.getX(), e.getY());
                var lastPath = selPath.getPathComponent(selPath.getPathCount() - 1);
                var obj = (lastPath as DefaultMutableTreeNode).getUserObject();

                if(action != null ){
                   action(obj);
                }

                if(selRow != - 1) {
                    if(e.getClickCount() == 1) {
                        selectedValue = obj;

                    }
                    else if(e.getClickCount() == 2) {
                    }
                }
                getJComponent().repaint();
            }
        };

        tree.addMouseListener(mouseListener);

        tree.setRootVisible(false);
        var scrollPane = new JScrollPane(tree);
        return scrollPane;

    }

    function getCell(cell: MyTreeCell): MutableTreeNode {
        var node = new DefaultMutableTreeNode(cell);
        for (c  in  cell.cells ){
            node.<<insert>>(getCell(c),node.getChildCount());
        }
        return node;
    }

}


Example:

Scene {
    content: [
        MyTree{
            root: MyTreeCell{
                text: "Root"
                cells: [
                    MyTreeCell{
                        text: "Letters"
                        cells:[
                            MyTreeCell{
                                text: "a"
                            }
                            MyTreeCell{
                                text: "b"
                            }
                        ]

                    }
                    MyTreeCell{
                        text: "Digits"
                        cells: for( i in [0..9]) MyTreeCell{ text: "{i}"}
                    }
                ]
            }
            action: function(obj:Object){
                println("Selected object: {obj}");
            }

        }
    ]
}

Table Edit

  1. Table200.169.120.124 21:02, December 19, 2011 (UTC)

Code:

sdsdsdd



Example:

import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.CustomNode;
import javafx.ext.swing.SwingButton;
//import packageX.filenameX.SwingTable;
//import x.x.TableColumn;
//import x.x.TableRow;
//import x.x.TableCell;


class Contact{
    var firstName: String;
    var lastName: String;
    var eMailAddress: String;
}

class ContactEditor extends CustomNode{
    public var contacts: Contact[];
    public var selection: Integer;


    public override function create(){
        Group{
            content: [ SwingTable{
                width: 300
                height: 200

                columns: [
                    TableColumn {
                        text: "First Name"
                    },
                    TableColumn {
                        text: "Last Name"
                    },
                    TableColumn {
                        text: "EMailAddress"
                    }
                ]

                rows: bind
                    for(p in contacts)
                    TableRow{
                        cells: [
                            TableCell {
                                text:bind p.firstName
                            },
                            TableCell {
                                text:bind p.lastName
                            },
                            TableCell {
                                text:bind p.eMailAddress
                            }
                        ]
                    }
                //]

                selection: bind selection with inverse

            },
            SwingButton{
                translateY: 200
                text: "Add Contact"
                action: function(){
                    insert Contact{} into contacts;
                }
            },
            SwingButton{
                translateX: 100
                translateY: 200
                text: "Remove Contact"
                action: function(){
                    delete contacts[selection];
                }
            }
            ]

        }
    }
}

var contacts =  [
    Contact {
        firstName: "Mike"
        lastName: "Wazowski"
        eMailAddress: "Mike.Wazowski@monster.com"
    },
    Contact {
        firstName: "Sulley"
        lastName: "Monster"
        eMailAddress: "Sulley.Monster@monster.com"
    }
];


Stage {
    title: "MyApp"
    scene: Scene {
        width: 600
        height: 400
        content: ContactEditor{
            contacts: bind contacts with inverse
        }
    }
}

Menus Edit

Menus 2

Code:

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javafx.ext.swing.SwingComponent;


class MenuBar extends SwingComponent{
    var menuBar: JMenuBar;

    public var menus:Menu[] on replace{
        for (m in menus){
            menuBar.add(m.menu);
        }
    }

    public override function createJComponent(){
        menuBar = new JMenuBar();
        return menuBar;
    }

}

class Menu extends SwingComponent{

    var menu: JMenu;

    public var text: String on replace{
        menu.setText(text);
    }

    public var items:MenuItem[] on replace{
        for (item in items){
            menu.add(item.menuItem);
        }
    }

    public var menus:Menu[] on replace{
        for (m in menus){
            menu.add(m.menu);
        }
    }

    public override function createJComponent(){        
        menu = new JMenu();
        return menu;
    }
}

class MenuItem extends SwingComponent{

    var menuItem: JMenuItem;

    public var text: String on replace{
        menuItem.setText(text);
    }


    public var action: function();

    public override function createJComponent(){
        menuItem = new JMenuItem();
        menuItem.addActionListener(
            ActionListener{
                public override function actionPerformed(e:ActionEvent){
                    println("menus: action");
                    action();
                }
            }
        );
        return menuItem;
    }
}

Example:

import javafx.stage.*;
import javafx.scene.*;

Stage {
    title: "Menu Example"
    scene: Scene {
        width: 200
        height: 200
        content: [
            MenuBar{
                menus: [
                    Menu{
                        text: "File"
                        items: [
                            MenuItem{
                                text: "Open File"
                                action: function(){
                                    println("Open File!")
                                }
                            }
                            MenuItem{
                                text: "Save File"
                                action: function(){
                                    println("Save File!")
                                }
                            }
                        ]
                    },
                    Menu{
                        text: "Edit"
                        items: [
                            MenuItem{
                                text: "Copy"
                                action: function(){
                                    println("Copy!")
                                }
                            }
                            MenuItem{
                                text: "Paste"
                                action: function(){
                                    println("Paste!")
                                }
                            }
                        ]
                    }
                ]
            }
        ]
    }
}


FileChooserEdit

Filechooser

Code:

import javafx.scene.*;
import javafx.stage.*;
import javafx.ext.swing.*;

import javax.swing.*;


public class SwingFileChooser extends SwingComponent{

    public var title: String = "Upload File";
    public var selectedFile: String;

    public override function createJComponent():JComponent{

        return SwingButton{
            text: "Browse..."
            action: function(){
                var fileChooser = new JFileChooser();
                if ( fileChooser.showDialog(getRootJComponent(), title) == JFileChooser.APPROVE_OPTION){
                    selectedFile = fileChooser.getSelectedFile().getName();
                }
            }
        }.getJComponent();
    }


}

Sample:

import javafx.scene.*;
import javafx.stage.*;
import javafx.ext.swing.*;

var selectedFile = "";

Stage {
    title: "File Chooser Sample"
    scene: Scene {
        content: [
            SwingLabel{
                width: 250
                text: bind "Selected File: {selectedFile}"
            }
            SwingFileChooser {
                translateX: 270
                selectedFile: bind selectedFile with inverse
            }
        ]
    }
}

ColorChooserEdit

Colorchooser

Code:

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

import javax.swing.event.ChangeListener;
import javax.swing.JColorChooser;

import javafx.ext.swing.SwingComponent;


class SwingColorChooser extends SwingComponent {

    var colorChooser: JColorChooser;

    public var selectedColor: Color;

    public override function createJComponent(){
        colorChooser = new JColorChooser();
        colorChooser.getSelectionModel().addChangeListener(
            ChangeListener {
                override function stateChanged(e) {
                    selectedColor = Color.fromAWTColor(colorChooser.getColor());
                }
            }
        );
        return colorChooser;
    }
}


var color: Color = Color.WHITE;

Stage {
    title: "Application title"
    width: 600
    height: 500
    scene: Scene {
        content: [
            SwingColorChooser{
                translateY: 100
                selectedColor: bind color with inverse
            }

            Circle {
                centerX: 50,
                centerY: 50
                radius: 40
                fill: bind color
                stroke: Color.BLUE
            }
        ]
    }
}

Panels Edit

BorderPanel Edit

Panel border

Code:

import javax.swing.JPanel;
import java.awt.BorderLayout;

import javafx.ext.swing.SwingComponent;

class SwingBorderPanel extends SwingComponent{

    var panel: JPanel;


    public var top: SwingComponent on replace{
        panel.add( top.getJComponent(), BorderLayout.NORTH);
    }

    public var left: SwingComponent on replace{
        panel.add( left.getJComponent(), BorderLayout.EAST);
    }

    public var center: SwingComponent on replace{
        println("[center] set component: {center.getJComponent()}");
        panel.add(center.getJComponent(), BorderLayout.CENTER);
    }

    public var right: SwingComponent on replace{
        panel.add( right.getJComponent(), BorderLayout.WEST);
    }

    public var bottom: SwingComponent on replace{
        panel.add( bottom.getJComponent(), BorderLayout.SOUTH);
    }


    public override function createJComponent(){
        panel = new JPanel(new BorderLayout());
        return panel;
    }

}


Example:

import javafx.stage.Stage;
import javafx.scene.Scene;

import javafx.ext.swing.SwingButton;

Stage {
    title: "Border Panel"
    scene: Scene {
        content: SwingBorderPanel{

            width: 200
            height: 150

            top:    SwingButton{ text: "Top"}
            left:   SwingButton{ text: "Left"}
            center: SwingButton{ text: "Center"}
            right:  SwingButton{ text: "Right"}
            bottom: SwingButton{ text: "Bottom"}
            
        }
    }
}

FlowPanel Edit

Panel flow

Code:

import javax.swing.JPanel;
import java.awt.FlowLayout;

import javafx.ext.swing.SwingComponent;

class SwingFlowPanel extends SwingComponent{

    var panel: JPanel;

    public var content: SwingComponent[] on replace{
        for(component in content){
            panel.add(component.getJComponent());
        }
    }


    public override function createJComponent(){
        panel = new JPanel(new FlowLayout());
        return panel;
    }

}

Example:

import javafx.stage.Stage;
import javafx.scene.Scene;

import javafx.ext.swing.SwingButton;

Stage {
    title: "Flow Panel"
    scene: Scene {
        content: SwingFlowPanel{

            width: 250
            height: 150

            content: for (n in [1..5]) SwingButton{ text: "Button: {n}"}

        }
    }
}


Example of using wrap method Edit

To wrap a swing class use javafx.ext.swing.SwingComponent.wrap() method.

JLabel example:

Swing wrapper label

Scene{
    content: SwingComponent.wrap( new JLabel("Hello World!"));
}


JButton example:

Swing wrapper

Code:

import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.ext.swing.SwingComponent;


var button = new JButton("Click Me!");

button.addActionListener(
    ActionListener{
        public override function actionPerformed(e:ActionEvent){
            println("Hello World!");
        }
    }
);

Stage {
    title: "Wrapper Example"
    scene: Scene {
        width: 250
        height: 100
        content: [ SwingComponent.wrap(button) ]
    }
}

Custom Components Edit

See Custom Components

Advertisement | Your ad here

Around Wikia's network

Random Wiki