Monday, November 23, 2009

Simple JavaServerFaces (JSF) Reference Example - Table

In the world of modern Integrated Development Environments (IDE)s, we have become accustomed to visual development, or at least code completion. We have also come to rely on advanced JSF frameworks like Woodstock, ICEFaces, myFaces, etc. A couple of weeks ago someone asked me about how to create a JSF page including a table, and how to "bind" the values to the table. That person did not have access to an advanced framework, and wanted to use a reference implementation. I created a simple NetBeans project which included only the reference implementation. I forgot how strange it is to code everything. A lot of the modern components have taken the complexity of creating the components out of your hands.

Below is an example application which uses the reference implementation only.



JSFRIDemo-1.0.zip

Saturday, November 21, 2009

Swing Event Examples - JButton and JRadioButton

I was looking at some code examples I created, and decided to polish them up and post them. One of the most challenging issues for most Swing developers is learning, and using events. I have included to examples below: One using events associated with a JButton, and the other using ActionEvents when a JRadioButton is pressed.

Note: The attached project is a NetBeans 6.5.1 project.

Here is the JButtonsEvent.java example

/*
* Copyright 2009 John Yeary <jyeary@bluelotussoftware.com>.

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*

* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/

/**
* JButtonEvents.java

*
* Created on Dec 30, 2008, 3:00:00 PM
*

* $Id: JButtonEvents.java 155 2009-11-21 15:06:40Z jyeary $
*/

package com.bluelotussoftware.examples.swing;

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

import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
*
* @author John Yeary <jyeary@bluelotussoftware.com>

* @version 1.0
*/
public class JButtonEvents extends javax.swing.JFrame {

JButton button;

public JButtonEvents() {
initialize();
}

private void initialize() {
button = new JButton("Press Me");

//Add Listeners as anonymous inner classes.

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent!");
}
});

button.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {
System.out.println("ItemEvent!");
}
});

button.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {
System.out.println("ChangeEvent!");
}
});


// Exit the application when the frame is closed

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(button);
this.pack();
}

/**
* @param args the command line arguments

*/
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new JButtonEvents().setVisible(true);
}
});
}
}

Here is the JRadioButtonActionEvents.java example

/*
* Copyright 2008-2009 John Yeary <jyeary@bluelotussoftware.com>.

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*

* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/

/*
* JRadioButtonActionEvents.java

*
* Created on Dec 30, 2008, 3:45:09 PM
*
* $Id: JRadioButtonActionEvents.java 155 2009-11-21 15:06:40Z jyeary $
*/
package com.bluelotussoftware.examples.swing;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.ButtonModel;
import javax.swing.JRadioButton;

/**

*
* @author John Yeary <jyeary@bluelotussoftware.com>
* @version 1.0

*/
public class JRadioButtonActionEvents extends javax.swing.JFrame {

/** Creates new form JRadioButtonActionEvents */

public JRadioButtonActionEvents() {
initComponents();

int value = 0;
ButtonModel[] models = new ButtonModel[3];
int ctr = 0;
Enumeration e = buttonGroup1.getElements();

while (e.hasMoreElements()) {
JRadioButton x = (JRadioButton) e.nextElement();
models[ctr++] = x.getModel();

}
jRadioButton1.setActionCommand("A");
jRadioButton2.setActionCommand("B");
jRadioButton3.setActionCommand("C");
ActionListener action = new MyActionListener();
jRadioButton1.addActionListener(action);
jRadioButton2.addActionListener(action);
jRadioButton3.addActionListener(action);
buttonGroup1.setSelected(models[value], true);
}

class MyActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();

if ("A".equalsIgnoreCase(s)) {
System.out.println("Radio Button \"A\" ActionEvent!");
} else if ("B".equalsIgnoreCase(s)) {
System.out.println("Radio Button \"B\" ActionEvent!");
} else if ("C".equalsIgnoreCase(s)) {
System.out.println("Radio Button \"C\" ActionEvent!");
}
}
}

/** This method is called from within the constructor to

* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.
*/

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("A");

buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("B");

buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("C");

jLabel1.setText("Select a radio button to generate an ActionEvent");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel1)
.add(layout.createSequentialGroup()
.add(21, 21, 21)
.add(jRadioButton1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jRadioButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jRadioButton3)))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jRadioButton1)
.add(jRadioButton2)
.add(jRadioButton3))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments

*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new JRadioButtonActionEvents().setVisible(true);
}
});
}

// Variables declaration - do not modify

private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
// End of variables declaration
}

NetBeans project files SwingEventsExamples-1.0.zip

Monday, November 02, 2009

JSF 1.2: Woodstock and Facelets

I have been asked if there is an example of how to use facelets. In this example application, I have changed the default namespace in my NetBeans project to w from webuijsf to make it simpler to see. In this example I display the customers from the sample database which is part of the default installation of NetBeans.

Here is what it looks like displayed:


Here is the facelets view:

The source code can be found on BitBucket here:WoodstockFaceletsExample

Popular Posts