creating a pop up window


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: creating a pop up window

  1. #1
    Join Date
    Mar 2004
    Posts
    635

    creating a pop up window

    I need to have a small popup window with various texfields on it. I don't want the popup to be a child stuck inside the parent window either.

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    create one out of a JFrame and show it non-modal

    though tbh, when you say "stuck inside" the only windows that can be "stuck inside" another are JInternalFrames, stuck inside JDesktopPanes
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Mar 2004
    Posts
    635
    I've made a class that extends JDialog, and used that to create my popup. (it is modal) Once the user clicks OK, I need to get the string that's in the text field and assign its value to a variable in my main program. (the string represents a path) During that same action, I need to update certain data based on that new string. How would I go about doing this?

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    make the actionlistener Observable (class MyActionListener extends Observable implements ActionListener) and then make the main form an observer of the actionlistener

    class MyMainFrame implements Observer
    ...
    MyActionListener mal = new MyActionListener();
    mal.addObserver(this);
    MyPopupForm.addActionListener(mal);


    on any interesting actions of the ActionListener, call a this.setChanged(), followed by this.notifyObservers("the string the user tped goes here");

    and in the update(object o, Object arg) of your MyMainFrame, cast the arg into a string and work with it
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    I've tried, but can't seem to fully understand how to implement what you're saying.



    Here's what would be the main frame.

    Code:
    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);
        }
    }


    And the popup frame.

    Code:
    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);
        }
        
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center