java and using observable class

Reply

Join Date: Mar 2004
Posts: 762
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

java and using observable class

 
0
  #1
Mar 16th, 2004
I need to make my popup class observable and the main frame class observer. When the user clicks the ok button on the popup, I need the string from the popup's textfield returned to my main program.

Main fram class.
  1.  
  2. import javax.swing.*;
  3. import java.util.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. public class TestFrame extends JFrame implements Observer
  7. {
  8. public TestFrame()
  9. {
  10. JMenuBar menuBar = new JMenuBar();
  11.  
  12. menuBar.add(createOptionsMenu());
  13.  
  14. getContentPane().add(menuBar, BorderLayout.NORTH);
  15. }
  16.  
  17.  
  18.  
  19.  
  20. public static void main()
  21. {
  22. TestFrame f = new TestFrame();
  23. f.pack();
  24. f.show();
  25. }
  26.  
  27.  
  28. /***************************************
  29. */
  30. public void update(Observable o, Object arg)
  31. {
  32. }
  33.  
  34. /***************************************
  35. */
  36. private JMenu createOptionsMenu()
  37. {
  38. JMenu menu = new JMenu("Options");
  39. menu.add(createOptionsDirectoriesItem());
  40.  
  41. return menu;
  42. }
  43.  
  44.  
  45. /***************************************
  46. */
  47. private JMenuItem createOptionsDirectoriesItem()
  48. {
  49. JMenuItem item = new JMenuItem("Directories");
  50. class MenuItemListener implements ActionListener
  51. {
  52. public void actionPerformed(ActionEvent e)
  53. {
  54. launchPopupPathConfigure();
  55. }
  56. }
  57. ActionListener listener = new MenuItemListener();
  58. item.addActionListener(listener);
  59.  
  60. return item;
  61. }
  62.  
  63.  
  64. /***************************************
  65. */
  66. private void launchPopupPathConfigure()
  67. {
  68. PopupPathConfigure pathConfigure = new PopupPathConfigure(this);
  69. pathConfigure.setVisible(true);
  70. }
  71. }


Popup class.
  1.  
  2. import javax.swing.JDialog;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JTextField;
  6. import javax.swing.JButton;
  7. import javax.swing.JPanel;
  8. import java.awt.FlowLayout;
  9. import javax.swing.JFileChooser;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.io.File;
  13. import java.util.Observable;
  14.  
  15. public class PopupPathConfigure extends JDialog
  16. {
  17. private JFileChooser browse = new JFileChooser();
  18.  
  19.  
  20. public PopupPathConfigure(JFrame parent)
  21. {
  22. super(parent, "Configure paths", true);
  23.  
  24. browse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  25. browse.setDialogTitle("Add Library Folder");
  26.  
  27. JLabel libraryLabel = new JLabel("Library");
  28. final JTextField libraryField = new JTextField("", 14);
  29. final JButton browseButton = new JButton("Browse");
  30. final JButton okButton = new JButton("Ok");
  31. JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
  32.  
  33. class ButtonListener extends Observable implements ActionListener
  34. {
  35. public void actionPerformed(ActionEvent e)
  36. {
  37. Object source = e.getSource();
  38. if (source == browseButton)
  39. {
  40. browse.showOpenDialog(null);
  41. File f = browse.getSelectedFile();
  42. String s = libraryField.getText();
  43. s += f.toString() + ";";
  44. libraryField.setText(s);
  45. }
  46. if (source == okButton)
  47. {
  48. setVisible(false);
  49. }
  50. }//end actionPerformed() method
  51. }//end class ButtonListener
  52.  
  53.  
  54.  
  55. p.add(libraryLabel);
  56. p.add(libraryField);
  57. p.add(browseButton);
  58. p.add(okButton);
  59.  
  60. ButtonListener listener = new ButtonListener();
  61. browseButton.addActionListener(listener);
  62. okButton.addActionListener(listener);
  63.  
  64.  
  65. getContentPane().add(p);
  66.  
  67. setSize(300,200);
  68. setResizable(false);
  69. }
  70.  
  71. }


Also, if anyone could show an example of the above code using a listener class without making it an inner-class, it would be appreciated. Thats the only way my professors taught it, and my degree doesn't have any more java classes scheduled for it. So if you're wondering, no this isn't a homework assignment.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 762
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: java and using observable class

 
0
  #2
Mar 22nd, 2004
Nobody knows? Haven't found a forum yet that can answer this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3
Reputation: trajesh is an unknown quantity at this point 
Solved Threads: 0
trajesh trajesh is offline Offline
Newbie Poster

Re: java and using observable class

 
0
  #3
Aug 14th, 2007
Originally Posted by Phaelax View Post
I need to make my popup class observable and the main frame class observer. When the user clicks the ok button on the popup, I need the string from the popup's textfield returned to my main program.

Main fram class.
  1.  
  2. import javax.swing.*;
  3. import java.util.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. public class TestFrame extends JFrame implements Observer
  7. {
  8. public TestFrame()
  9. {
  10. JMenuBar menuBar = new JMenuBar();
  11.  
  12. menuBar.add(createOptionsMenu());
  13.  
  14. getContentPane().add(menuBar, BorderLayout.NORTH);
  15. }
  16.  
  17.  
  18.  
  19.  
  20. public static void main()
  21. {
  22. TestFrame f = new TestFrame();
  23. f.pack();
  24. f.show();
  25. }
  26.  
  27.  
  28. /***************************************
  29. */
  30. public void update(Observable o, Object arg)
  31. {
  32. }
  33.  
  34. /***************************************
  35. */
  36. private JMenu createOptionsMenu()
  37. {
  38. JMenu menu = new JMenu("Options");
  39. menu.add(createOptionsDirectoriesItem());
  40.  
  41. return menu;
  42. }
  43.  
  44.  
  45. /***************************************
  46. */
  47. private JMenuItem createOptionsDirectoriesItem()
  48. {
  49. JMenuItem item = new JMenuItem("Directories");
  50. class MenuItemListener implements ActionListener
  51. {
  52. public void actionPerformed(ActionEvent e)
  53. {
  54. launchPopupPathConfigure();
  55. }
  56. }
  57. ActionListener listener = new MenuItemListener();
  58. item.addActionListener(listener);
  59.  
  60. return item;
  61. }
  62.  
  63.  
  64. /***************************************
  65. */
  66. private void launchPopupPathConfigure()
  67. {
  68. PopupPathConfigure pathConfigure = new PopupPathConfigure(this);
  69. pathConfigure.setVisible(true);
  70. }
  71. }


Popup class.
  1.  
  2. import javax.swing.JDialog;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JTextField;
  6. import javax.swing.JButton;
  7. import javax.swing.JPanel;
  8. import java.awt.FlowLayout;
  9. import javax.swing.JFileChooser;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.io.File;
  13. import java.util.Observable;
  14.  
  15. public class PopupPathConfigure extends JDialog
  16. {
  17. private JFileChooser browse = new JFileChooser();
  18.  
  19.  
  20. public PopupPathConfigure(JFrame parent)
  21. {
  22. super(parent, "Configure paths", true);
  23.  
  24. browse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  25. browse.setDialogTitle("Add Library Folder");
  26.  
  27. JLabel libraryLabel = new JLabel("Library");
  28. final JTextField libraryField = new JTextField("", 14);
  29. final JButton browseButton = new JButton("Browse");
  30. final JButton okButton = new JButton("Ok");
  31. JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
  32.  
  33. class ButtonListener extends Observable implements ActionListener
  34. {
  35. public void actionPerformed(ActionEvent e)
  36. {
  37. Object source = e.getSource();
  38. if (source == browseButton)
  39. {
  40. browse.showOpenDialog(null);
  41. File f = browse.getSelectedFile();
  42. String s = libraryField.getText();
  43. s += f.toString() + ";";
  44. libraryField.setText(s);
  45. }
  46. if (source == okButton)
  47. {
  48. setVisible(false);
  49. }
  50. }//end actionPerformed() method
  51. }//end class ButtonListener
  52.  
  53.  
  54.  
  55. p.add(libraryLabel);
  56. p.add(libraryField);
  57. p.add(browseButton);
  58. p.add(okButton);
  59.  
  60. ButtonListener listener = new ButtonListener();
  61. browseButton.addActionListener(listener);
  62. okButton.addActionListener(listener);
  63.  
  64.  
  65. getContentPane().add(p);
  66.  
  67. setSize(300,200);
  68. setResizable(false);
  69. }
  70.  
  71. }


Also, if anyone could show an example of the above code using a listener class without making it an inner-class, it would be appreciated. Thats the only way my professors taught it, and my degree doesn't have any more java classes scheduled for it. So if you're wondering, no this isn't a homework assignment.
Can Anyone Please How can i run this program?
Is it a Applet Program or Application Program?
If so,How to run this ?
Thank You
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java and using observable class

 
0
  #4
Aug 14th, 2007
It's a Swing application, not an Applet, and, I'm sorry, but, if you can't figure out both that, and how to run it, from looking at the code, then I don't think the code is going to do you much good.

What do you want it for?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3
Reputation: trajesh is an unknown quantity at this point 
Solved Threads: 0
trajesh trajesh is offline Offline
Newbie Poster

Re: java and using observable class

 
0
  #5
Aug 14th, 2007
Thanks for your reply Mr.masijade

I just wanted to know which file we have to run..
While running, Whether Will it execute in Applet window or in Console...?
Thank you
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3
Reputation: trajesh is an unknown quantity at this point 
Solved Threads: 0
trajesh trajesh is offline Offline
Newbie Poster

Re: java and using observable class

 
0
  #6
Aug 14th, 2007
Also I wanted to learn about how to use observer and Observable
Thank You
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java and using observable class

 
0
  #7
Aug 14th, 2007
Well, what are the defining methods of an Applet and what is the defining method of a console application. If you can answer that question, then a quick (and I do mean quick) look at the code will tell you which class to run and (at least generally) how to run it. If it is (were) an Applet a little more diggin would be necessary to discover the parameters.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: java and using observable class

 
0
  #8
Aug 14th, 2007
Originally Posted by Phaelax View Post
Nobody knows? Haven't found a forum yet that can answer this.

Hi Phaelax,
I don't think forums can directly help you in this problem. Because you specified you want to add observer and observable to your classes, but you did not even implemented the observer interface correctly. I will recommend you to read how MVC (model/view/controller) pattern works, then you can figure out how to do this. After you are ready with your code, and have any problem, please let us know, we should be able to help you.
Here is a good link to learn about MVC:
http://csis.pace.edu/~bergin/mvc/mvcgui.html

HINTS: to make observer/observable works, you must have to notify observer about the change is made in observable object by calling notifyObserver(); Besides, you must have to write the implementation of update() method.
A Perfect World
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: java and using observable class

 
0
  #9
Aug 14th, 2007
Originally Posted by trajesh View Post
Can Anyone Please How can i run this program?
Is it a Applet Program or Application Program?
If so,How to run this ?
Thank You
Hey! this code is not complete, so you can not run it. Are you trying to learn Java? In that case, you should start from little beginning. Observer/Observable is little advanced level. If you already know how to write code, you might be interested in studying more about design patterns. Good luck!

Just to clarify! Java and javascript is not same! If you have previous experience with only javascript or java applets, you should start from beginning in java!
A Perfect World
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC