Results 1 to 8 of 8
- 07-03-2011, 09:15 AM #1
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
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
}
}
- 07-03-2011, 10:18 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.new LookAndfeel(this); //<-- this one is a method but it does not work
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.
- 07-03-2011, 10:40 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Here is what I mean about calling methods and constructors as a result of timer ticks:
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.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"); } }
- 07-03-2011, 03:51 PM #4
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
I try your code but i got some error look. i hope you can understand what i want to do.
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.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){ } } }
-
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
but "this" represents the Message object which really doesn't make sense.Java Code:SwingUtilities.updateComponentTreeUI(this);
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:
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: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) { } } }
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); } }); } }
- 07-10-2011, 05:23 PM #6
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
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) { } } }
- 07-10-2011, 05:25 PM #7
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
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) { } } }
-
Similar Threads
-
How to invoke method present in abstract class?
By java_no1 in forum New To JavaReplies: 3Last Post: 06-01-2011, 07:13 AM -
Update Method inside a GUI?
By Zepher in forum New To JavaReplies: 8Last Post: 02-07-2011, 01:33 AM -
reflection invoke method
By pprl in forum New To JavaReplies: 12Last Post: 11-16-2010, 05:43 PM -
How to invoke out side web root files inside application?
By damodar in forum JavaServer Faces (JSF)Replies: 3Last Post: 06-03-2010, 12:06 PM -
[SOLVED] I want to Invoke an Instance Method as a Parameter is it Possible?
By Laythe in forum New To JavaReplies: 10Last Post: 06-12-2009, 06:41 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks