Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How to invoke constructor inside the method of SwingTimer to update LookAndFeel

  1. #1
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default How to invoke constructor inside the method of SwingTimer to update LookAndFeel

    My problem here is i dont now how to invoke Code #3 inside the method of Timer plss help.

    //Code #1
    NewJFrame class -- start
    NewJFrame constructor --- start
    javax.swing.Timer t = new javax.swing.Timer(1,new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    new LookAndfeel(this); //<-- this one is a method but it does not work
    //to update the LookAndFeel of NewJFrame
    //how to invoke "Code #3" inside this code?
    });
    t.start();
    --end of constructor
    --end of NewJFrame class


    other class
    //Code #2
    class LookAndfeel {
    public LookAndfeel(ActionListener aThis) {
    //cant update look and feel using this code.
    }
    }


    other class
    //Code #3
    class LookAndfeel {
    public LookAndfeel(NewJFrame aThis){
    //This one work for updating look and feel but i dont know
    //how to invoke this inside the method of SwingTimer from NewJFrame. //see Code #1
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    new LookAndfeel(this); //<-- this one is a method but it does not work
    Which do you mean? The code invokes a constructor ("new ...") to make a new object which is not used, but the comment talks about a method.

    You can, in fact, do either: create an object or call a method of an existing one. Perhaps you could post runnable (brief!) code and say what "does not work" means.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Here is what I mean about calling methods and constructors as a result of timer ticks:

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class MyFrame extends JFrame {
    	
        private Message message = new Message();
    	
        public MyFrame() {
            Timer timer = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        // With each tick we can call a constructor.
                        // normally we would actually do something with the
                        // object created
                    new Message(); 
                }
            });
            timer.start();
            timer = new Timer(1300, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        // With each tick we can call a call a method
                        // of an already existing object.
                    message.display();
                }
            });
            timer.start();
        }
    	
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    MyFrame test = new MyFrame();
                    test.setTitle("MyFrame test");
                    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    test.setSize(new Dimension(500, 400));
                    test.setVisible(true);
                }
            });
        }
    }
    
    class Message {
        Message() {
            System.out.println("Message " + this + " created");
        }
    	
        void display() {
            System.out.println("Message " + this + " display() method called");
        }
    }
    It is not altogether clear why you would use another class (constructor or method) to change the look and feel, why you would change the lnf within a timer, or why you would want lnf changes at 1KHz. But the syntax above shows how you would.

  4. #4
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    I try your code but i got some error look. i hope you can understand what i want to do.
    Java Code:
    public class MyFrame extends JFrame {
    
        private Message message = new Message();
    
        public MyFrame() {
            Timer timer = new Timer(3000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        // With each tick we can call a constructor.
                        // normally we would actually do something with the
                        // object created
                    new Message();
                      SwingUtilities.updateComponentTreeUI(this); //<-- i got error here
                }
            });
            timer.start();
            timer = new Timer(1300, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        // With each tick we can call a call a method
                        // of an already existing object.
                    message.display();
                     SwingUtilities.updateComponentTreeUI(this); //<-- i got error here
                }
            });
            timer.start();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    MyFrame test = new MyFrame();
                    test.setLayout(null);
                    JButton b = new JButton("Test");
                    b.setBounds(10,10,60,30);
                    test.add(b);
                    test.setTitle("MyFrame test");
                    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    test.setSize(new Dimension(500, 400));
                    test.setVisible(true);
                }
            });
        }
    }
    
    class Message {
        Message() {
            System.out.println("Message " + this + " created");
            try{
                UIManager.setLookAndFeel(new MetalLookAndFeel());
                SwingUtilities.updateComponentTreeUI(this); //<-- i got error here
            }catch(Exception ex){
    
            }
        }
    
        void display() {
            System.out.println("Message " + this + " display() method called");
            try{
                UIManager.setLookAndFeel(new NimbusLookAndFeel());
                 SwingUtilities.updateComponentTreeUI(this); //<-- i got error here
            }catch(Exception ex){
    
            }
        }
    }
    i want to use the constructor or method of the other class to update my Main Frame but i dont know how to do it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you get an error or exception, you should post the entire error message here so we can better understand what isn't working.

    Anyway, in your Message class, you're calling

    Java Code:
    SwingUtilities.updateComponentTreeUI(this);
    but "this" represents the Message object which really doesn't make sense.

    If you need to call a method of another object or pass another object into a method parameter, you'll need a reference to that object. So for instance in your example you'd want something like so:

    Java Code:
    class Message {
       private MyFrame myframe;
    
       Message(MyFrame myFrame) {
          this.myframe = myFrame;
          System.out.println("Message " + this + " created");
          try {
             UIManager.setLookAndFeel(new MetalLookAndFeel());
             SwingUtilities.updateComponentTreeUI(myframe); // use the MyFrame
             // reference here
          } catch (Exception ex) {
    
          }
       }
    
       void display() {
          System.out.println("Message " + this + " display() method called");
          try {
             UIManager.setLookAndFeel(new NimbusLookAndFeel());
             SwingUtilities.updateComponentTreeUI(myframe); // ditto
          } catch (Exception ex) {
    
          }
       }
    }
    You have a similar problem in your ActionListener's above which are passing the current ActionListener instance into the this variable which won't work. To use the "this" for the outer class, you must specific which this you mean:

    Java Code:
    public class MyFrame extends JFrame {
    
       private Message message = new Message(this);
    
       public MyFrame() {
          Timer timer = new Timer(3000, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                // With each tick we can call a constructor.
                // normally we would actually do something with the
                // object created
                new Message(MyFrame.this);
                // you need to pass MyFrame's this into this message like so:
                SwingUtilities.updateComponentTreeUI(MyFrame.this);
             }
          });
          timer.start();
          timer = new Timer(1300, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                // With each tick we can call a call a method
                // of an already existing object.
                message.display();
                SwingUtilities.updateComponentTreeUI(MyFrame.this); // ditto
             }
          });
          timer.start();
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                MyFrame test = new MyFrame();
                test.setLayout(null);
                JButton b = new JButton("Test");
                b.setBounds(10, 10, 60, 30);
                test.add(b);
                test.setTitle("MyFrame test");
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test.setSize(new Dimension(500, 400));
                test.setVisible(true);
             }
          });
       }
    }
    Jhovarie likes this.

  6. #6
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    A little error in netbeans but i just modified your code and it is now working thanks Fubarable.

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class MyFrame extends JFrame {
    
       private Message message;
    
       public MyFrame() {
           message = new Message(this);
          javax.swing.Timer timer = new javax.swing.Timer(3000, new ActionListener() {
                @Override
             public void actionPerformed(ActionEvent e) {
                // With each tick we can call a constructor.
                // normally we would actually do something with the
                // object created
                new Message(MyFrame.this);
                // you need to pass MyFrame's this into this message like so:
                SwingUtilities.updateComponentTreeUI(MyFrame.this);
             }
          });
          timer.start();
          javax.swing.Timer timer2 = new javax.swing.Timer(1300, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                // With each tick we can call a call a method
                // of an already existing object.
                message.display();
                SwingUtilities.updateComponentTreeUI(MyFrame.this); // ditto
             }
          });
          timer2.start();
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                MyFrame test = new MyFrame();
                test.setLayout(null);
                JButton b = new JButton("Test");
                b.setBounds(10, 10, 60, 30);
                test.add(b);
                test.setTitle("MyFrame test");
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test.setSize(new Dimension(500, 400));
                test.setVisible(true);
             }
          });
       }
    }
    Java Code:
    import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.plaf.metal.MetalLookAndFeel;
    
    class Message {
       private MyFrame myframe;
    
       Message(MyFrame myFrame) {
          this.myframe = myFrame;
          System.out.println("Message " + this + " created");
          try {
             UIManager.setLookAndFeel(new MetalLookAndFeel());
             SwingUtilities.updateComponentTreeUI(myframe); // use the MyFrame
             // reference here
          } catch (Exception ex) {
    
          }
       }
    
       void display() {
          System.out.println("Message " + this + " display() method called");
          try {
             UIManager.setLookAndFeel(new NimbusLookAndFeel());
             SwingUtilities.updateComponentTreeUI(myframe); // ditto
          } catch (Exception ex) {
    
          }
       }
    }

  7. #7
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    A little error in netbeans but i just modified your code and it is now working thanks Fubarable.

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class MyFrame extends JFrame {
    
       private Message message;
    
       public MyFrame() {
           message = new Message(this);
          javax.swing.Timer timer = new javax.swing.Timer(3000, new ActionListener() {
                @Override
             public void actionPerformed(ActionEvent e) {
                // With each tick we can call a constructor.
                // normally we would actually do something with the
                // object created
                new Message(MyFrame.this);
                // you need to pass MyFrame's this into this message like so:
                SwingUtilities.updateComponentTreeUI(MyFrame.this);
             }
          });
          timer.start();
          javax.swing.Timer timer2 = new javax.swing.Timer(1300, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                // With each tick we can call a call a method
                // of an already existing object.
                message.display();
                SwingUtilities.updateComponentTreeUI(MyFrame.this); // ditto
             }
          });
          timer2.start();
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                MyFrame test = new MyFrame();
                test.setLayout(null);
                JButton b = new JButton("Test");
                b.setBounds(10, 10, 60, 30);
                test.add(b);
                test.setTitle("MyFrame test");
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test.setSize(new Dimension(500, 400));
                test.setVisible(true);
             }
          });
       }
    }
    Java Code:
    import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.plaf.metal.MetalLookAndFeel;
    
    class Message {
       private MyFrame myframe;
    
       Message(MyFrame myFrame) {
          this.myframe = myFrame;
          System.out.println("Message " + this + " created");
          try {
             UIManager.setLookAndFeel(new MetalLookAndFeel());
             SwingUtilities.updateComponentTreeUI(myframe); // use the MyFrame
             // reference here
          } catch (Exception ex) {
    
          }
       }
    
       void display() {
          System.out.println("Message " + this + " display() method called");
          try {
             UIManager.setLookAndFeel(new NimbusLookAndFeel());
             SwingUtilities.updateComponentTreeUI(myframe); // ditto
          } catch (Exception ex) {
    
          }
       }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. How to invoke method present in abstract class?
    By java_no1 in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 07:13 AM
  2. Update Method inside a GUI?
    By Zepher in forum New To Java
    Replies: 8
    Last Post: 02-07-2011, 01:33 AM
  3. reflection invoke method
    By pprl in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 05:43 PM
  4. How to invoke out side web root files inside application?
    By damodar in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 06-03-2010, 12:06 PM
  5. Replies: 10
    Last Post: 06-12-2009, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •