Helper function for creating Swing based UI’s
The Myna.Swing library contains functions for interacting with javax.swing.* and java.awt.*. This is primarily intended for use with the Myna commandline.
The core of this library is Myna.Swing.build. This function takes a config object and returns a dynamically extended instance of the requested class. This allows for a more representative definition of the GUI hierarchy.
| Myna. Swing | Helper function for creating Swing based UI’s |
| Functions | |
| getColor | returns a java.awt.Color object from an RGB value |
| getDimension | returns a java.awt.Dimension object from a width,height value |
| getConst | returns the value of the passed AWT/Swing constant Value. |
| alert | Creates an alert dialog |
| getInput | Displays an input dialog, similar to window.prompt() in the browser |
| confirm | Displays a confirmation dialog, similar to window.confirm() in the browser |
| showDialog | Creates a dialog with arbitrary fields |
| build | Creates Swing component from an object config |
| Properties | |
| BorderFactory | shortcut to the javax.swing.BorderFactory class |
getColor:function( r, g, b )
returns a java.awt.Color object from an RGB value
| r | integer “red” value |
| b | integer “blue” value |
| g | integer “green” value |
getDimension:function( w, h )
returns a java.awt.Dimension object from a width,height value
| w | integer “width” value in pixels |
| h | integer “heigth” value in pixels |
getConst:function( name )
returns the value of the passed AWT/Swing constant Value.
| name | name of AWT/Swing constant |
var MS = Myna.Swing;
...
layoutPos:{
anchor:MS.getConst("GridBagConstraints.WEST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
...
alert:function( text, title, type )
Creates an alert dialog
| text | Text to display in the alert box |
| title | Optional, default “Alert” Title of the alert window |
| type | *Optional, default “info” The possible types are “info”,”warning”, and “error”. This selects the icon displayed next to text |
getInput:function( prompt, options, defaultIndex )
Displays an input dialog, similar to window.prompt() in the browser
| prompt | Text to display above the input control |
| options | Optional, default “” Either a string, or Array of string values to display. If Array, a drop down or scrolling select box will be displayed, depending on the number of items in this array. If this is a string, this will be the initial value of the text box |
| defaultIndex | Optional, default 0 Only applies when options is an Array. This is the option in the array to pre-select |
text selected/entered by the user, or null if the dialog is canceled
confirm:function( prompt, buttons, defaultIndex )
Displays a confirmation dialog, similar to window.confirm() in the browser
| prompt | Text to display |
| buttons | Optional, default [“OK”,”Cancel”] Array of text button labels to display. |
| defaultIndex | Optional, default 0 Button to make the default option |
True, if the “OK” button is pressed false, if canceled
showDialog:function( fields, buttonLabels, defaultIndex )
Creates a dialog with arbitrary fields
| fields | Array of field configuration objects. See build |
| title | Optional, default “” Title of dialog |
| buttonLabels | Optional, default [“OK”,”Cancel”] |
| defaultIndex | Optional, default 0 Button to make the default option |
Array of generated fields, or null if canceled. If the config objects in fields include the name property, then a property of that name will also be in the returned array. This means that fields can be referenced by name or index
var result = showDialog([{
cls:"JLabel",
text:"Username:"
},{
cls:"JTextField",
name:"username"
},{
cls:"JLabel",
text:"Password:"
},{
cls:"JPasswordField",
name:"password"
}],"SVN Authentication")
if (result){
p.username = result.username.getText()
//getPassword returns a char[], so we filter it through a native Java string
p.password = new java.lang.String(result.password.getPassword()).toString()
svn = new WebSvn().connect(p.username,p.password);
}
build:function( config )
Creates Swing component from an object config
| config | Object configuration, see “Config” below. This config object will be stored in a “config” property in the returned object |
| cls | String name of a class in Swing or AWT, like “JLabel” or “JTextField” |
| items | Optional, default [] An array of component configs that represent children of this component |
| defaults | Optional, default {} An object representing a template for objects in items. Every property in this object will be applied to each item unless that property is already defined. if defaults appears as a property in defaults then that will apply to the grandchildren items, etc. This is useful for setting default properties for a group of fields |
| handler | Optional Function that handles the default action for this component. This will create an ActionListener to this component |
| listeners | Optional, default {} Allows the creation of component listeners for this component, by defining an event property and function handler. The following events can be monitored: componentResized, componentShown, componentHidden, and componentMoved. See http://download.oracle.com- /javase- /6- /docs- /api- /java- /awt- /event- /ComponentListener.html |
| layoutPos | Optional A Java options object appropriate to use as the second parameter to java.awt.Contrainer.add(). See http://download.oracle.com- /javase- /1.5.0- /docs- /api- /java- /awt- /Container.html#add%28java.awt.Component%2C%20java.lang.Object%29 Alternatively, this can be a config object that describes an options object. |
| [*] | Optional Any other property. If this property has a match as a setter in cls then that setter will be called with the value of this property. If the calue of this property has a cls property, build() will be called against it before passing it to the setter |
An extended instance of cls with these extra properties:
| config | The original config object |
| getCmp | A function that takes a component name and searches this component and all child components and returns a reference to the named component, or null if not found |
var MS = Myna.Swing;
var config = {
//Special Property: Swing class to create
cls:"JFrame",
//any property that has setter is set on the resulting object
title:"HelloWorldSwing",
//getConst returns the value of the passed constant Value.
defaultCloseOperation:$server.isCommandline?MS.getConst("JFrame.EXIT_ON_CLOSE"):MS.getConst("WindowConstants.DISPOSE_ON_CLOSE"),
//any property not already defined by the class, will be added to the resulting object
center:function(){
this.setLocationRelativeTo(null)
},
//Special Property: elements to add to this Swing object
items:[{
cls:"JPanel",
//Special Property: layout specific positioning hint for this control
layoutPos:MS.getConst("BorderLayout.CENTER"),
//this is equivalent to setLayout(new java.awt.GridBagLayout())
layout:{
cls:"GridBagLayout"
},
//MS.BorderFactory is a shortcut to java.swing.BorderFactory
border:MS.BorderFactory.createCompoundBorder(
MS.BorderFactory.createTitledBorder("Text Fields"),
MS.BorderFactory.createEmptyBorder(5,5,5,5)
),
//Special Property: properties of "defaults" are copied to each item in "items".
//Nested "defaults" will applied to nested "items"
defaults:{
cls:"JLabel",
layoutPos:{
cls:"GridBagConstraints",
anchor:MS.getConst("GridBagConstraints.WEST"),
gridwidth:MS.getConst("GridBagConstraints.RELATIVE"), //next-to-last
fill:MS.getConst("GridBagConstraints.NONE"), //reset to default
weightx:0, // don't expand to fill space
}
},
items:[{
//all other properties are coming from "defaults" above
text:"JTextField:"
},{
cls:"JTextField",
preferredSize:MS.getDimension(100,22),
layoutPos:{
anchor:MS.getConst("GridBagConstraints.EAST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
handler:function(event){
var src =event.getSource();
//getCmp is custom extension for each component that searches
// recusively through its children looking for a component by name
src.getParent().getCmp("OutputField").setText("JTextField set to: " + src.getText())
}
},{
text:"JPasswordField:",
},{
cls:"JPasswordField",
layoutPos:{
anchor:MS.getConst("GridBagConstraints.EAST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
handler:function(event){
var src =event.getSource();
src.getParent().getCmp("OutputField").setText("JPasswordField set to: " + src.getText())
}
},{
text:"JFormattedTextField:",
},{
cls:"JFormattedTextField",
value:new java.util.Date(new Date().getTime()),
layoutPos:{
anchor:MS.getConst("GridBagConstraints.EAST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
//Special Property: automatically adds an ActionListener for this component
handler:function(event){
var src =event.getSource();
src.getParent().getCmp("OutputField").setText("JFormattedTextField set to: " + src.getText())
}
},{
text:"Type text in a field and press Enter.",
border:MS.BorderFactory.createEmptyBorder(10,0,0,0),
name:"OutputField",//this is how we find this component later with getCmp(name)
layoutPos:{
anchor:MS.getConst("GridBagConstraints.WEST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
}]
}]
}
var panel = new MS.build(config);
panel.center();
panel.show()
returns a java.awt.Color object from an RGB value
getColor:function( r, g, b )
returns a java.awt.Dimension object from a width,height value
getDimension:function( w, h )
returns the value of the passed AWT/Swing constant Value.
getConst:function( name )
Creates an alert dialog
alert:function( text, title, type )
Displays an input dialog, similar to window.prompt() in the browser
getInput:function( prompt, options, defaultIndex )
Displays a confirmation dialog, similar to window.confirm() in the browser
confirm:function( prompt, buttons, defaultIndex )
Creates a dialog with arbitrary fields
showDialog:function( fields, buttonLabels, defaultIndex )
Creates Swing component from an object config
build:function( config )