DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   java and using observable class (http://www.daniweb.com/forums/thread4730.html)

Phaelax Mar 16th, 2004 9:27 pm
java and using observable class
 
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.
  
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class TestFrame extends JFrame implements Observer
{
        public TestFrame()
        {
                JMenuBar menuBar = new JMenuBar();
               
                menuBar.add(createOptionsMenu());
               
                getContentPane().add(menuBar, BorderLayout.NORTH);
        }
       
       
       
       
        public static void main()
        {
                TestFrame f = new TestFrame();
                f.pack();
                f.show();
        }
       
       
        /***************************************
        */ 
        public void update(Observable o, Object arg)
        {
        }
       
        /***************************************
        */       
        private JMenu createOptionsMenu()
        {
                JMenu menu = new JMenu("Options");
                menu.add(createOptionsDirectoriesItem());
               
                return menu;
        }
       
       
        /***************************************
        */
        private JMenuItem createOptionsDirectoriesItem()
        {
                JMenuItem item = new JMenuItem("Directories");
                class MenuItemListener implements ActionListener
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                launchPopupPathConfigure();
                        }
                }
                ActionListener listener = new MenuItemListener();
                item.addActionListener(listener);
               
                return item;
        }
       
       
        /***************************************
        */       
        private void launchPopupPathConfigure()
        {
                PopupPathConfigure pathConfigure = new PopupPathConfigure(this);
                pathConfigure.setVisible(true);
        }
}


Popup class.
 
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JFileChooser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Observable;
 
public class PopupPathConfigure extends JDialog
{
        private JFileChooser browse = new JFileChooser();
       
       
        public PopupPathConfigure(JFrame parent)
        {
                super(parent, "Configure paths", true);
               
                browse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                browse.setDialogTitle("Add Library Folder");
               
                JLabel libraryLabel = new JLabel("Library");
                final JTextField libraryField = new JTextField("", 14);
                final JButton browseButton = new JButton("Browse");
                final JButton okButton = new JButton("Ok");
                JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
               
                class ButtonListener extends Observable implements ActionListener
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                Object source = e.getSource();
                                if (source == browseButton)
                                {
                                        browse.showOpenDialog(null);
                                        File f = browse.getSelectedFile();
                                        String s = libraryField.getText();
                                        s += f.toString() + ";";
                                        libraryField.setText(s);
                                }
                                if (source == okButton)
                                {
                                        setVisible(false);                                       
                                }
                        }//end actionPerformed() method
                }//end class ButtonListener
               
               
               
                p.add(libraryLabel);
                p.add(libraryField);
                p.add(browseButton);
                p.add(okButton);
               
                ButtonListener listener = new ButtonListener();
                browseButton.addActionListener(listener);
                okButton.addActionListener(listener);
               
               
                getContentPane().add(p);
               
                setSize(300,200);
                setResizable(false);
        }
       
}


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.

Phaelax Mar 22nd, 2004 8:08 am
Re: java and using observable class
 
Nobody knows? Haven't found a forum yet that can answer this.

trajesh Aug 14th, 2007 8:29 am
Re: java and using observable class
 
Quote:

Originally Posted by Phaelax (Post 20976)
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.
  
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class TestFrame extends JFrame implements Observer
{
        public TestFrame()
        {
                JMenuBar menuBar = new JMenuBar();
               
                menuBar.add(createOptionsMenu());
               
                getContentPane().add(menuBar, BorderLayout.NORTH);
        }
       
       
       
       
        public static void main()
        {
                TestFrame f = new TestFrame();
                f.pack();
                f.show();
        }
       
       
        /***************************************
        */ 
        public void update(Observable o, Object arg)
        {
        }
       
        /***************************************
        */       
        private JMenu createOptionsMenu()
        {
                JMenu menu = new JMenu("Options");
                menu.add(createOptionsDirectoriesItem());
               
                return menu;
        }
       
       
        /***************************************
        */
        private JMenuItem createOptionsDirectoriesItem()
        {
                JMenuItem item = new JMenuItem("Directories");
                class MenuItemListener implements ActionListener
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                launchPopupPathConfigure();
                        }
                }
                ActionListener listener = new MenuItemListener();
                item.addActionListener(listener);
               
                return item;
        }
       
       
        /***************************************
        */       
        private void launchPopupPathConfigure()
        {
                PopupPathConfigure pathConfigure = new PopupPathConfigure(this);
                pathConfigure.setVisible(true);
        }
}


Popup class.
 
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JFileChooser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Observable;
 
public class PopupPathConfigure extends JDialog
{
        private JFileChooser browse = new JFileChooser();
       
       
        public PopupPathConfigure(JFrame parent)
        {
                super(parent, "Configure paths", true);
               
                browse.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                browse.setDialogTitle("Add Library Folder");
               
                JLabel libraryLabel = new JLabel("Library");
                final JTextField libraryField = new JTextField("", 14);
                final JButton browseButton = new JButton("Browse");
                final JButton okButton = new JButton("Ok");
                JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
               
                class ButtonListener extends Observable implements ActionListener
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                Object source = e.getSource();
                                if (source == browseButton)
                                {
                                        browse.showOpenDialog(null);
                                        File f = browse.getSelectedFile();
                                        String s = libraryField.getText();
                                        s += f.toString() + ";";
                                        libraryField.setText(s);
                                }
                                if (source == okButton)
                                {
                                        setVisible(false);                                       
                                }
                        }//end actionPerformed() method
                }//end class ButtonListener
               
               
               
                p.add(libraryLabel);
                p.add(libraryField);
                p.add(browseButton);
                p.add(okButton);
               
                ButtonListener listener = new ButtonListener();
                browseButton.addActionListener(listener);
                okButton.addActionListener(listener);
               
               
                getContentPane().add(p);
               
                setSize(300,200);
                setResizable(false);
        }
       
}


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

masijade Aug 14th, 2007 8:49 am
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?

trajesh Aug 14th, 2007 11:54 am
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

trajesh Aug 14th, 2007 11:55 am
Re: java and using observable class
 
Also I wanted to learn about how to use observer and Observable
Thank You

masijade Aug 14th, 2007 2:57 pm
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.

orko Aug 14th, 2007 6:38 pm
Re: java and using observable class
 
Quote:

Originally Posted by Phaelax (Post 21462)
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.

orko Aug 14th, 2007 6:43 pm
Re: java and using observable class
 
Quote:

Originally Posted by trajesh (Post 418660)
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!


All times are GMT -4. The time now is 10:11 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC