0 Replies Latest reply: Oct 8, 2009 3:32 PM by 800392 RSS

    Mysterious one time Casting Exception

      I am trying to use the PropertyChangeSupport of JComponent class.
      But when I am executing the following code, Clicking on the menu button first time gives Runtime casting Exception, but then it runs fine always.

      import javax.swing.*;
      import java.beans.*;
      import java.awt.*;
      import java.awt.event.*;
      public class FrameListener extends JFrame implements ActionListener, PropertyChangeListener
           JLabel lblMessage;
           JMenuItem changeFont;
           FontSource fe = new FontSource(this,"Font Editor");
           public FrameListener(){
                super("Hello World");
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
                increaseReadability() ;
           private void increaseReadability(){
                JPanel panel = new JPanel();
                Font f = new Font("Times New Roman",Font.BOLD,24);
                lblMessage = new JLabel("HELLO WORLD",SwingConstants.CENTER);
                JMenuBar actionBar = new JMenuBar();     
                JMenu edit = new JMenu("Edit");
                changeFont = new JMenuItem("Font");
           public void propertyChange(PropertyChangeEvent pcevent){
                Object obj = pcevent.getNewValue() ;
                System.out.println(obj.getClass()) ;
                //Statement occuring problem 1st time
                Font newFt = (Font)obj;
           public void actionPerformed(ActionEvent evt){
           public static void main(String argv[]) {
                new HelloWorld();
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.beans.*;
      public class FontSource extends JDialog implements ActionListener {
           private Font newFont = new Font("Times New Roman",Font.BOLD,12);
           JComboBox cbfType,cbfStyle,cbfSize;
           JButton btnOk,btnCancel;
           //protected PropertyChangeSupport changes = new PropertyChangeSupport(this);
           public Font getNewFont(){
                return newFont;
           public void setNewFont(Font f){
                Font old = newFont;
                     //this statement calls the propertyChange() of FrameListener
                                  //if u are removing comments, replace the following statement with
                             // changes.firePropertyChange("Font Changed",old,f);
                     firePropertyChange("Font Changed",old,f);
                     newFont = f;
                catch(Exception e){
           public FontSource(Frame fr,String title){
                // getting font family from the graphics environment.
                GraphicsEnvironment gf = GraphicsEnvironment.getLocalGraphicsEnvironment();
                String myfont[] = gf.getAvailableFontFamilyNames();
                cbfType = new JComboBox(myfont);
                String fontStyle[] = {"PLAIN","ITALIC","BOLD",};
                cbfStyle = new JComboBox(fontStyle);
                String fontSize[] = {"10","12","14","16","18","20","24","26","28","36","48","72"};
                cbfSize = new JComboBox(fontSize);
                btnOk =new JButton("OK");
                btnCancel =new JButton("Cancel");
                // adding action listener
                // setting layout and size for the dialog
                setLayout(new FlowLayout());
           public void actionPerformed(ActionEvent ae){
                     String type = (String) cbfType.getSelectedItem();
                     String style = (String)cbfStyle.getSelectedItem();
                     int s = 0;
                     int size = Integer.parseInt((String)cbfSize.getSelectedItem());
                          s= Font.PLAIN;
                     else {
                          if(style =="BOLD")
                               s= Font.BOLD;
                               s= Font.ITALIC;
                     Font f = new Font(type,s,size);
           public void addPropertyChangeListener(PropertyChangeListener l){
                System.out.println("attachement done...");
           public void removePropertyChangeListener(PropertyChangeListener l){
      But If i use my own PropertyChangeSupport (remove the comments in FontSource.java), then it's working perfectly.
      I tried my best, but not getting this.
      Thnx in advance *:*--)

      Edited by: rits on Oct 8, 2009 8:19 PM

      Edited by: rits on Oct 8, 2009 8:25 PM

      Edited by: rits on Oct 8, 2009 8:30 PM