Results 1 to 3 of 3
  1. #1
    jv5
    jv5 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default Unable to display JDialog from JFrame

    Hello, I wonder if anyone has the time to check over my code and see why the method I am calling from my main UI to open a dialog box isn't working.

    I've already seen a similar post in these forums, but unfortunately the answer didn't work for me.

    So, I have my JFrame which has a menu bar at the top, and I've used the Netbeans GUI Builder to create an event when someone clicks on one of the menu items. This should open the dialog.

    Here is the method in my main UI:

    Java Code:
        
    private void createNewTorrent(java.awt.event.MouseEvent evt) {                                  
            NewTorrentDialog nt = new NewTorrentDialog(this, true);
            nt.setVisible(true);
     }
    Yet when I run the application, nothing happens when I click on the menu option. I include the full code below, just in case you'd like to see it, but it's pretty minimal at the moment seeing as I seemed to have come to a halt fairly early on!

    Thanks for any help :)

    **Main UI Code**
    Java Code:
    package torrentUI;
    
    public class UIWindow extends javax.swing.JFrame {
        
        /** Creates new form UIWindow */
        public UIWindow() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            menuBar = new javax.swing.JMenuBar();
            fileMenu = new javax.swing.JMenu();
            createNewTorrentOption = new javax.swing.JMenuItem();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            fileMenu.setText("File");
    
            createNewTorrentOption.setText("Create new Torrent...");
            createNewTorrentOption.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    createNewTorrent(evt);
                }
            });
            fileMenu.add(createNewTorrentOption);
    
            menuBar.add(fileMenu);
    
            setJMenuBar(menuBar);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 586, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 377, Short.MAX_VALUE)
            );
    
            pack();
        }// </editor-fold>                        
    
        private void createNewTorrent(java.awt.event.MouseEvent evt) {                                  
            NewTorrentDialog nt = new NewTorrentDialog(this, true);
            nt.setVisible(true);
        }                                 
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new UIWindow().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify                     
        private javax.swing.JMenuItem createNewTorrentOption;
        private javax.swing.JMenu fileMenu;
        private javax.swing.JMenuBar menuBar;
        // End of variables declaration                   
        
    }
    **Dialog code**

    Java Code:
    package torrentUI;
    import javax.swing.*;
    
    /**
     *
     * @author jv5
     */
    public class NewTorrentDialog extends JDialog {
    
        /** Creates new form NewTorrentDialog */
        public NewTorrentDialog(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            torrentNameInput = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jComboBox1 = new javax.swing.JComboBox();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
            jLabel1.setText("Torrent Name:");
    
            torrentNameInput.setText("MyTorrent");
    
            jLabel2.setText("File location:");
    
            jLabel3.setText("Select group(s) to post to:");
    
            jTextField1.setText("C:\\");
    
                jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select group..." }));
    
                jButton1.setText("Browse...");
    
                jButton2.setText("Search...");
    
                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextField1)
                                    .addComponent(torrentNameInput, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                        .addContainerGap(61, Short.MAX_VALUE))
                );
                layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(torrentNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1))
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2))
                        .addContainerGap(172, Short.MAX_VALUE))
                );
    
                pack();
            }// </editor-fold>                        
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    NewTorrentDialog dialog = new NewTorrentDialog(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField torrentNameInput;
        // End of variables declaration                   
    
    }
    Last edited by jv5; 03-05-2011 at 05:36 PM.

  2. #2
    jv5
    jv5 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    Oh happy joyous days. I changed it from a mousePressed event to an actionPerformed event and it works fine now.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. can display image in JFrame?
    By xCLARAx in forum AWT / Swing
    Replies: 14
    Last Post: 04-03-2009, 08:02 PM
  2. Returning data from a JFrame/JDialog?!
    By Joe2003 in forum AWT / Swing
    Replies: 6
    Last Post: 01-08-2009, 01:14 AM
  3. Help Needed, simple jframe display
    By typh0n in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 02:06 AM
  4. How to display scrolling text and image on a JFrame
    By Abhi_vk in forum AWT / Swing
    Replies: 1
    Last Post: 06-20-2008, 11:19 PM
  5. problems with jDialog in a JFrame
    By bbq in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 05:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •