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.

Recommended Answers

All 10 Replies

Nobody knows? Haven't found a forum yet that can answer this.

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

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?

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

Also I wanted to learn about how to use observer and Observable
Thank You

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.

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.

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!

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?

This post is completely unrelated to the rest of the thread. Start your own thread and do not resurrect zombies to post unrelated material.

commented: close++ +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.