Results 1 to 6 of 6
  1. #1
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Unhappy vm runs through jdialog directly to jframe

    hallo everybody
    i have a jframe showing a jtable with data readed from a mysql table. now i want to prompt a jdialog for username, password for the connection to mysql. i already got the code with buttons and actionslistener for the jdialog, but the problem is that the vm is not waiting till the user has entered his name and pw, so that the jdialog and jtable are shown simultaneously. i need help, so that first the jdialog is shown and only if the connection is ok the jtable is shown. how can accomplish this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Are you showing the JDialog as a modal dialog?

    You may wish to create a very small simple program that mimics your program flow but does not require an actual database or other outside resource, so we can compile, run and test this program to better be able to help you. This program is called an SSCCE. Please see this link for more details on this: Short, Self Contained, Correct Example

    Much luck!

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i posted a small example with two frames,

    Java Code:
    import java.awt.Container;
    import java.awt.FlowLayout;
    
    import javax.swing.*;
    
    public class JFrameDemo {
    
    	private static JFrame loginFrame;
    	private static JFrame appFrame;
    
    	public static void main(String[] args) {
    		// First window
    		loginFrame = new JFrame("Login Window");
    		loginFrame.setBounds(100, 100, 400, 300);
    		JFrame.setDefaultLookAndFeelDecorated(false);
    		loginFrame
    				.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    
    		JButton push = new JButton("push me");
    		push.addActionListener(new loginListener());
    		Container loginPane = loginFrame.getContentPane();
    		loginPane.add(push);
    		loginFrame.setVisible(true);
    
    		// Second window, should only appear AFTER first Window is closed
    		appFrame = new JFrame("Application Window");
    		appFrame.setBounds(530, 100, 400, 300);
    		JFrame.setDefaultLookAndFeelDecorated(false);
    		appFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    
    		JButton b = new JButton("let's work");
    		b.addActionListener(new loginListener());
    		Container appPane = appFrame.getContentPane();
    		appPane.add(b);
    		appFrame.setVisible(true);
    	}
    }
    here the code for listener

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class loginListener implements ActionListener {
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		System.out.println("Button was pushed");
    		System.exit(0);
    	}
    
    }
    what code is needed, so that the second frame is shown only after the first frame has been closed?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again, a simple thing to do is to show a modal JDialog as suggested above. For instance,
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DialogDemo {
    
      private static void createAndShowUI() {
        DialogDemoMain main = new DialogDemoMain();
        
        JFrame frame = new JFrame("Main App");
        frame.getContentPane().add(main.getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        //frame.setVisible(true);
        
        DialogDemoDialog signon = new DialogDemoDialog();
        JDialog dialog = new JDialog(frame, "Sign on", true);
        dialog.getContentPane().add(signon.getComponent());
        dialog.pack();
        dialog.setVisible(true);
        
        int returnValue = signon.getReturnValue();
        if (returnValue == JOptionPane.OK_OPTION) {
          frame.setVisible(true);
        }
        else {
          frame.dispose();
        }
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    
    class DialogDemoMain {
      private static final Dimension MAIN_SIZE = new Dimension(400, 300);
      private JPanel mainPanel = new JPanel();
    
      public DialogDemoMain() {
        JButton exitBtn = new JButton("Exit");
        exitBtn.addActionListener(new ActionListener() {
          
          public void actionPerformed(ActionEvent e) {
            Window win = SwingUtilities.getWindowAncestor(mainPanel);
            win.dispose();
          }
        });
        
        mainPanel.add(exitBtn);
        mainPanel.setPreferredSize(MAIN_SIZE);
      }
    
      public JComponent getComponent() {
        return mainPanel;
      }
    
    }
    
    class DialogDemoDialog {
      private static final Dimension MAIN_SIZE = new Dimension(400, 300);
      private JPanel mainPanel = new JPanel();
      private int returnValue = JOptionPane.CANCEL_OPTION;
    
      public DialogDemoDialog() {
        JButton signOnBtn = new JButton("Sign On");
        signOnBtn.addActionListener(new ActionListener() {
          
          public void actionPerformed(ActionEvent e) {
            returnValue = JOptionPane.OK_OPTION;
            Window win = SwingUtilities.getWindowAncestor(mainPanel);
            win.dispose();
          }
        });
        JButton cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener() {
          
          public void actionPerformed(ActionEvent e) {
            returnValue = JOptionPane.CANCEL_OPTION;
            Window win = SwingUtilities.getWindowAncestor(mainPanel);
            win.dispose();
          }
        });
        
        mainPanel.add(signOnBtn);
        mainPanel.add(cancelBtn);
        mainPanel.setPreferredSize(MAIN_SIZE);
      }
      
      public void setSetReturnValue() {
        this.returnValue = JOptionPane.CANCEL_OPTION;
      }
      
      public int getReturnValue() {
        return returnValue; 
      }
    
      public JComponent getComponent() {
        return mainPanel;
      }
    
    }
    Simpler still is to use a JOptionPane.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default great solution

    Quote Originally Posted by Fubarable View Post
    Java Code:
    ...
    Simpler still is to use a JOptionPane.

    Hi Fubarable

    thanks a lot for the code. not easy to understand, but i got the idea of your solution.

    bye
    tuttle

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by j2me64 View Post
    Hi Fubarable
    thanks a lot for the code. not easy to understand, but i got the idea of your solution.
    You're welcome. Please feel free to ask questions if you have any.

Similar Threads

  1. passing an object from one JDialog to Jframe
    By sinha in forum AWT / Swing
    Replies: 7
    Last Post: 12-16-2009, 04:56 PM
  2. Communicate JFrame with JDialog
    By BeRniTo in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2009, 02:07 PM
  3. Unable to display JDialog from JFrame
    By jv5 in forum NetBeans
    Replies: 2
    Last Post: 02-04-2009, 04:33 AM
  4. Returning data from a JFrame/JDialog?!
    By Joe2003 in forum AWT / Swing
    Replies: 6
    Last Post: 01-08-2009, 12:14 AM
  5. problems with jDialog in a JFrame
    By bbq in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 04:14 AM

Tags for this Thread

Posting Permissions

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