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.