| | |
java and using observable class
![]() |
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
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.
Popup class.
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.
Main fram class.
Java Syntax (Toggle Plain Text)
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.
Java Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
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.
Java Syntax (Toggle Plain Text)
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.
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?
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
----------------------------------------------
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
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
----------------------------------------------
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
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
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
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
•
•
•
•
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
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
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Java sound API
- Next Thread: Help!!! Help!!! Help!!!
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class clear client code compile compiler component database development dice digit eclipse equation error event formatingtextintooltipjava fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list main map method methods mobile myregfun netbeans nonstatic notdisplaying openjavafx pearl problem program project qt recursion repositories scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver storm string superclass swing system thread threads tree variablebinding windows xor






