T
thufir
I'm having trouble assigning ChangeListeners to a source bean and target
bean. Particularly in MySlider, how is changeListener instantiated?
thufir@arrakis:~/bcit3621$
thufir@arrakis:~/bcit3621$ cat lab2/src/a00720398/lab02/util/
ColorSelector.java
package a00720398.lab02.util;
import java.awt.Color;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ColorSelector extends javax.swing.JPanel implements
ChangeListener {
private Color color;
//private ChangeListener changeListener;
/*= new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
//throw new UnsupportedOperationException("Not supported
yet.");
color = new Color(red.getValue(), green.getValue(),
blue.getValue());
}
};*/
/** Creates new form ColorSelector */
public ColorSelector() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
red = new a00720398.lab02.util.MySlider();
green = new a00720398.lab02.util.MySlider();
blue = new a00720398.lab02.util.MySlider();
setLayout(new javax.swing.BoxLayout(this,
javax.swing.BoxLayout.Y_AXIS));
/* what kind of listener do I add
* to the source bean?
* sourceBean.addChangeListener()?
*/
/*
red.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
*/
red.setText("red");
add(red);
/* what kind of listener do I add
* to the source bean?
* sourceBean.addChangeListener()?
*/
green.setText("green");
add(green);
/* what kind of listener do I add
* to the source bean?
* sourceBean.addChangeListener()?
*/
blue.setText("blue");
add(blue);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private a00720398.lab02.util.MySlider blue;
private a00720398.lab02.util.MySlider green;
private a00720398.lab02.util.MySlider red;
// End of variables declaration//GEN-END:variables
public Color getColor(){
return color;
}
public void stateChanged(ChangeEvent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
thufir@arrakis:~/bcit3621$
thufir@arrakis:~/bcit3621$ cat lab2/src/a00720398/lab02/util/
MySlider.java
package a00720398.lab02.util;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MySlider extends javax.swing.JPanel {
private ChangeListener changeListener;
/*= new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
//throw new UnsupportedOperationException("Not supported
yet.");
changeListener.stateChanged(arg0);
}
};*/
/** Creates new form MySlider */
public MySlider() {
//changeListener = new ChangeListener();
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
jLabel1 = new javax.swing.JLabel();
jSlider1 = new javax.swing.JSlider();
jTextField1 = new javax.swing.JTextField();
jLabel1.setText("jLabel1");
add(jLabel1);
jSlider1.setMaximum(255);
jSlider1.addChangeListener(new javax.swing.event.ChangeListener()
{
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
add(jSlider1);
org.jdesktop.beansbinding.Binding binding =
org.jdesktop.beansbinding.Bindings.createAutoBinding
(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE,
jSlider1, org.jdesktop.beansbinding.ELProperty.create("${value}"),
jTextField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
add(jTextField1);
bindingGroup.bind();
}// </editor-fold>//GEN-END:initComponents
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt)
{//GEN-FIRST:event_jSlider1StateChanged
// TODO add your handling code here:
//changeListener.stateChanged(evt);
}//GEN-LAST:event_jSlider1StateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JSlider jSlider1;
private javax.swing.JTextField jTextField1;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration//GEN-END:variables
public int getValue(){
return jSlider1.getValue();
}
public void setText(String text){
jLabel1.setText(text);
}
}
thufir@arrakis:~/bcit3621$
thufir@arrakis:~/bcit3621$ cat lab2/src/a00720398/lab02/view/
Lab02View.java
package a00720398.lab02.view;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Lab02View extends javax.swing.JFrame implements
ChangeListener {
Color color = new Color(0,0,0);
/** Creates new form Lab02View */
public Lab02View() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
jTextArea1 = new javax.swing.JTextArea();
colorSelector1 = new a00720398.lab02.util.ColorSelector();
setDefaultCloseOperation
(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new javax.swing.BoxLayout
(getContentPane(), javax.swing.BoxLayout.Y_AXIS));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
getContentPane().add(jTextArea1);
getContentPane().add(colorSelector1);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//new Lab02View().setVisible(true);
JFrame frame = new Lab02View();
frame.setLocationRelativeTo(null);
frame.setSize(600,400);
frame.setVisible(true);
}
});
}
public void stateChanged(ChangeEvent arg0) {
//throw new UnsupportedOperationException("Not supported yet.");
color = colorSelector1.getColor();
System.out.println(color);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private a00720398.lab02.util.ColorSelector colorSelector1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables
}
thufir@arrakis:~/bcit3621$
thanks,
Thufir
bean. Particularly in MySlider, how is changeListener instantiated?
thufir@arrakis:~/bcit3621$
thufir@arrakis:~/bcit3621$ cat lab2/src/a00720398/lab02/util/
ColorSelector.java
package a00720398.lab02.util;
import java.awt.Color;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ColorSelector extends javax.swing.JPanel implements
ChangeListener {
private Color color;
//private ChangeListener changeListener;
/*= new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
//throw new UnsupportedOperationException("Not supported
yet.");
color = new Color(red.getValue(), green.getValue(),
blue.getValue());
}
};*/
/** Creates new form ColorSelector */
public ColorSelector() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
red = new a00720398.lab02.util.MySlider();
green = new a00720398.lab02.util.MySlider();
blue = new a00720398.lab02.util.MySlider();
setLayout(new javax.swing.BoxLayout(this,
javax.swing.BoxLayout.Y_AXIS));
/* what kind of listener do I add
* to the source bean?
* sourceBean.addChangeListener()?
*/
/*
red.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
*/
red.setText("red");
add(red);
/* what kind of listener do I add
* to the source bean?
* sourceBean.addChangeListener()?
*/
green.setText("green");
add(green);
/* what kind of listener do I add
* to the source bean?
* sourceBean.addChangeListener()?
*/
blue.setText("blue");
add(blue);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private a00720398.lab02.util.MySlider blue;
private a00720398.lab02.util.MySlider green;
private a00720398.lab02.util.MySlider red;
// End of variables declaration//GEN-END:variables
public Color getColor(){
return color;
}
public void stateChanged(ChangeEvent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
thufir@arrakis:~/bcit3621$
thufir@arrakis:~/bcit3621$ cat lab2/src/a00720398/lab02/util/
MySlider.java
package a00720398.lab02.util;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MySlider extends javax.swing.JPanel {
private ChangeListener changeListener;
/*= new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
//throw new UnsupportedOperationException("Not supported
yet.");
changeListener.stateChanged(arg0);
}
};*/
/** Creates new form MySlider */
public MySlider() {
//changeListener = new ChangeListener();
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
jLabel1 = new javax.swing.JLabel();
jSlider1 = new javax.swing.JSlider();
jTextField1 = new javax.swing.JTextField();
jLabel1.setText("jLabel1");
add(jLabel1);
jSlider1.setMaximum(255);
jSlider1.addChangeListener(new javax.swing.event.ChangeListener()
{
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
add(jSlider1);
org.jdesktop.beansbinding.Binding binding =
org.jdesktop.beansbinding.Bindings.createAutoBinding
(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE,
jSlider1, org.jdesktop.beansbinding.ELProperty.create("${value}"),
jTextField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);
add(jTextField1);
bindingGroup.bind();
}// </editor-fold>//GEN-END:initComponents
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt)
{//GEN-FIRST:event_jSlider1StateChanged
// TODO add your handling code here:
//changeListener.stateChanged(evt);
}//GEN-LAST:event_jSlider1StateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JSlider jSlider1;
private javax.swing.JTextField jTextField1;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration//GEN-END:variables
public int getValue(){
return jSlider1.getValue();
}
public void setText(String text){
jLabel1.setText(text);
}
}
thufir@arrakis:~/bcit3621$
thufir@arrakis:~/bcit3621$ cat lab2/src/a00720398/lab02/view/
Lab02View.java
package a00720398.lab02.view;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Lab02View extends javax.swing.JFrame implements
ChangeListener {
Color color = new Color(0,0,0);
/** Creates new form Lab02View */
public Lab02View() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
jTextArea1 = new javax.swing.JTextArea();
colorSelector1 = new a00720398.lab02.util.ColorSelector();
setDefaultCloseOperation
(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new javax.swing.BoxLayout
(getContentPane(), javax.swing.BoxLayout.Y_AXIS));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
getContentPane().add(jTextArea1);
getContentPane().add(colorSelector1);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//new Lab02View().setVisible(true);
JFrame frame = new Lab02View();
frame.setLocationRelativeTo(null);
frame.setSize(600,400);
frame.setVisible(true);
}
});
}
public void stateChanged(ChangeEvent arg0) {
//throw new UnsupportedOperationException("Not supported yet.");
color = colorSelector1.getColor();
System.out.println(color);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private a00720398.lab02.util.ColorSelector colorSelector1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables
}
thufir@arrakis:~/bcit3621$
thanks,
Thufir