943,752 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 13203
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 16th, 2004
0

java and using observable class

Expand 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.
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Mar 22nd, 2004
0

Re: java and using observable class

Nobody knows? Haven't found a forum yet that can answer this.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Aug 14th, 2007
0

Re: java and using observable class

Click to Expand / Collapse  Quote originally posted by Phaelax ...
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.
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trajesh is offline Offline
3 posts
since Aug 2007
Aug 14th, 2007
0

Re: java and using observable class

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?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Aug 14th, 2007
0

Re: java and using observable class

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trajesh is offline Offline
3 posts
since Aug 2007
Aug 14th, 2007
0

Re: java and using observable class

Also I wanted to learn about how to use observer and Observable
Thank You
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trajesh is offline Offline
3 posts
since Aug 2007
Aug 14th, 2007
0

Re: java and using observable class

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Aug 14th, 2007
0

Re: java and using observable class

Click to Expand / Collapse  Quote originally posted by Phaelax ...
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.
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Aug 14th, 2007
0

Re: java and using observable class

Click to Expand / Collapse  Quote originally posted by trajesh ...
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!
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Apr 27th, 2010
0
Re: java and using observable class
hi I am writing A program which consists of Multiple Jframe Screen which takes input from user. I have used singleton object to create only one instance due to which I am unable to clear the TextField value while moving between the JFrames as it shows the earlier input too the object doesn't update the state because of singleton pattern.
what is the solution?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harshalforu is offline Offline
1 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Final Year Project SE
Next Thread in Java Forum Timeline: help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC