Results 1 to 7 of 7
  1. #1
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Stumped. Regarding getting values of JDialog from other Panels.

    Okay, I'll try to explain myself as clear as possible, because I'm honestly not quite sure how to put this.

    I have a Main Frame that calls many different panels.

    One of the Panels is a confirm submission panel. Inside this panel, there is a JDialog Box. This JDialogBox has a Yes or No Option.

    I wish to do it so, that if the user clicks on "Yes", the Main Frame will remove this panel, and add a new panel to its Frame.


    The problem I am having here is that I am not able to get my Main Frame to know that the Yes or No has been clicked. How should I go about doing this? My brain's a little fried from all the coding I've been doing the past week, so if it is a just a simple logic, excuse me. While waiting for a reply, I will continue trying out different ways. Thank you.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Stumped. Regarding getting values of JDialog from other Panels.

    Hi rhexis,

    It is a little difficult to bug fix without seeing the code but the following guide may give you some idea as to what you are doing wrong.
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Regards.

  3. #3
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Stumped. Regarding getting values of JDialog from other Panels.

    Quote Originally Posted by Ronin View Post
    Hi rhexis,

    It is a little difficult to bug fix without seeing the code but the following guide may give you some idea as to what you are doing wrong.
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Regards.
    Hi Ronin,

    thanks for the prompt reply.

    I do know how to create a Dialog Box, no problem with that. And I do know that they return an int value based on what has been clicked (Yes or No). My problem here is regarding the logic of it. I may create a simple Frame in a while and post the code up to give you a better understanding of my problem. But let me try again to explain it.

    Basically, I have a Main Frame, and several buttons on it that calls out different Panels.

    Inside one panel, I have a JDialogBox that is similar to this.

    Java Code:
    int n = JOptionPane.showConfirmDialog(
        frame,
        "Would you like green eggs and ham?",
        "An Inane Question",
        JOptionPane.YES_NO_OPTION);
    When I click on Yes, I want to JFrame to remove this Panel and change it to another Panel. The thing is, I'm not able to do it because I can't quite think of how to pass the int n to the JFrame as there is no action performed.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Stumped. Regarding getting values of JDialog from other Panels.

    Can you post the code so we can take a look at it.

    Regards.

  5. #5
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Stumped. Regarding getting values of JDialog from other Panels.

    Quote Originally Posted by Ronin View Post
    Can you post the code so we can take a look at it.

    Regards.
    Hi Ronin,

    thanks again for the prompt reply

    I did a very simple set of codes.

    Main Frame

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class MainFrame extends JFrame {
    
    	private JPanel contentPane;
    	private ConfirmPanel newConfirmPanel;
    	private AfterConfirmPanel newAfterConfirmPanel;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					MainFrame frame = new MainFrame();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public MainFrame() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    
    		JPanel panel = new JPanel();
    		panel.setBounds(0, 0, 434, 74);
    		contentPane.add(panel);
    		panel.setLayout(null);
    		newConfirmPanel = new ConfirmPanel();
    		newConfirmPanel.setBounds(0, 85, 434, 179);
    
    		newAfterConfirmPanel = new AfterConfirmPanel();
    		newConfirmPanel.setBounds(0, 85, 434, 179);
    
    		JButton btnNewButton = new JButton("Button 1");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				contentPane.add(newConfirmPanel);
    				contentPane.repaint();
    				contentPane.revalidate();
    			}
    		});
    		btnNewButton.setBounds(0, 0, 212, 74);
    		panel.add(btnNewButton);
    
    		JButton btnButton = new JButton("Button 2");
    		btnButton.setBounds(222, 0, 212, 74);
    		panel.add(btnButton);
    	}
    }
    Confirm Panel
    Java Code:
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class ConfirmPanel extends JPanel {
    
    	/**
    	 * Create the panel.
    	 */
    	public ConfirmPanel() {
    		setLayout(null);
    
    		JButton btnConfirm = new JButton("Confirm?");
    		btnConfirm.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				int yesNo = JOptionPane.showConfirmDialog(null, "INFORMATIONS",
    						"Title", JOptionPane.YES_NO_OPTION);
    			}
    		});
    		btnConfirm.setBounds(185, 136, 89, 23);
    		add(btnConfirm);
    
    	}
    }
    After Confirm Panel
    Java Code:
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import java.awt.Font;
    
    
    public class AfterConfirmPanel extends JPanel {
    
    	/**
    	 * Create the panel.
    	 */
    	public AfterConfirmPanel() {
    		setLayout(null);
    		
    		JLabel lblConfirmed = new JLabel("CONFIRMED!!!!!!!!");
    		lblConfirmed.setFont(new Font("Tahoma", Font.BOLD, 17));
    		lblConfirmed.setBounds(138, 56, 174, 14);
    		add(lblConfirmed);
    
    	}
    }

    Basically, I would like the Main Frame to know that Yes has been clicked in the Confirm Panel, and it would like to to remove the confirm panel and change it to after confirm panel.

    I'm kind of stumped on this part, because I can't seem to find a way to pass the yesNo value to the main frame after the yes have been clicked.

    I can create a method that allows the main frame to get the yesNo value, for example

    Java Code:
    public int confirmed(){
    return yesNo;
    }
    but that is not "live". I want the Main Frame to immediately remove the confirmPanel and change it to AfterConfirmPanel the moment Yes has been clicked.
    Last edited by rhexis; 04-02-2013 at 01:39 PM.

  6. #6
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Stumped. Regarding getting values of JDialog from other Panels.

    Hi Guys.

    Good news. I have managed to solve that portion regarding changing of JPanel.

    Basically, I just needed to check if Yes is clicked, then I'll call the parent to add the new panel, repaint and remove the old one.

    The new codes are like that

    MAIN FRAME

    Java Code:
    import java.awt.BorderLayout;
    
    public class MainFrame extends JFrame {
    
    	private JPanel contentPane;
    	private ConfirmPanel newConfirmPanel;
    	private AfterConfirmPanel newAfterConfirmPanel;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					MainFrame frame = new MainFrame();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public MainFrame() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    
    		JPanel panel = new JPanel();
    		panel.setBounds(0, 0, 434, 74);
    		contentPane.add(panel);
    		panel.setLayout(null);
    		newConfirmPanel = new ConfirmPanel();
    		newConfirmPanel.setBounds(0, 85, 434, 179);
    
    		newAfterConfirmPanel = new AfterConfirmPanel();
    		newConfirmPanel.setBounds(0, 85, 434, 179);
    
    		JButton btnNewButton = new JButton("Button 1");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				contentPane.add(newConfirmPanel);
    				contentPane.repaint();
    				contentPane.revalidate();
    			}
    		});
    		btnNewButton.setBounds(0, 0, 212, 74);
    		panel.add(btnNewButton);
    
    		JButton btnButton = new JButton("Button 2");
    		btnButton.setBounds(222, 0, 212, 74);
    		panel.add(btnButton);
    	}
    }
    Java Code:
    import javax.swing.JOptionPane;
    
    public class ConfirmPanel extends JPanel {
    
    	/**
    	 * Create the panel.
    	 */
    
    	private JPanel newAfterConfirmPanel;
    
    	public ConfirmPanel() {
    		setLayout(null);
    		newAfterConfirmPanel = new AfterConfirmPanel();
    		newAfterConfirmPanel.setBounds(0, 85, 434, 179);
    		JButton btnConfirm = new JButton("Confirm?");
    		btnConfirm.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				int yesNo = JOptionPane.showConfirmDialog(null, "INFORMATIONS",
    						"Title", JOptionPane.YES_NO_OPTION);
    				if (yesNo == 0) {
    					javax.swing.SwingUtilities.getWindowAncestor(
    							ConfirmPanel.this).add(newAfterConfirmPanel);
    					javax.swing.SwingUtilities.getWindowAncestor(
    							ConfirmPanel.this).repaint();
    					javax.swing.SwingUtilities.getWindowAncestor(
    							ConfirmPanel.this).remove(ConfirmPanel.this);
    				}
    			}
    		});
    		btnConfirm.setBounds(185, 136, 89, 23);
    		add(btnConfirm);
    
    	}
    }
    Java Code:
    import javax.swing.JPanel;
    
    
    public class AfterConfirmPanel extends JPanel {
    
    	/**
    	 * Create the panel.
    	 */
    	public AfterConfirmPanel() {
    		setLayout(null);
    		
    		JLabel lblConfirmed = new JLabel("CONFIRMED!!!!!!!!");
    		lblConfirmed.setFont(new Font("Tahoma", Font.BOLD, 17));
    		lblConfirmed.setBounds(138, 56, 174, 14);
    		add(lblConfirmed);
    
    	}
    }
    But I have a new question. How do I get the parent to disable both buttons after it's been confirmed? Since the values were not really passed to the Main Frame.

    While the above code solved my problem regarding changing of Panels when the Ok is clicked, it would be much simpler if I was able to get that value and pass it to the Main Frame, and if the Main Frame detected a change of value, it would remove the existing panel and change it to the After Confirm Panel.
    Last edited by rhexis; 04-02-2013 at 03:42 PM.

  7. #7
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Re: Stumped. Regarding getting values of JDialog from other Panels.

    I think I've got it. I just need to pass my main frame as a constructor to my panel and from there I can call it's methods. I'll be playing around with it a little longer.

Similar Threads

  1. infinite loop has me stumped
    By D.good in forum New To Java
    Replies: 12
    Last Post: 02-16-2012, 09:49 PM
  2. Stumped on unknown problem
    By stevenfriz in forum New To Java
    Replies: 9
    Last Post: 02-16-2012, 09:55 AM
  3. Stumped... any help would be appreciated!
    By gard007 in forum New To Java
    Replies: 3
    Last Post: 12-01-2011, 07:42 AM
  4. Need some help with panels inside panels
    By kakefjes in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2011, 11:36 AM
  5. stumped......
    By trueblue in forum New To Java
    Replies: 21
    Last Post: 07-13-2009, 04:16 PM

Posting Permissions

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