Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
here i added a dialogue box to the frame,now i want to add another button inside the opened dialogue box while clicking the button should open another window ..means aloop a three dialogue boxes,so that the top one should be enabled always
package test;

Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class test_dialogue_1
{
  static JFrame frame;
 
  public static void main(String[] args)
  {
    // schedule this for the event dispatch thread (edt)
    SwingUtilities.invokeLater(new Runnable()
    {
            @Override
      public void run()
      {
        displayJFrame();
      }
    });
  }
 
  static void displayJFrame()
  {
    frame = new JFrame("Our JDialog Center Example");
 
    // create our jbutton, then tell it what to do when
    // it is pressed
    JButton showDialogButton= new JButton ("Show dialog box");
   
    showDialogButton.addActionListener(new ActionListener()
    {
            @Override
      public void actionPerformed(ActionEvent e)
      {
        // display/center the jdialog when the button is pressed
        JDialog d = new JDialog(frame, "Hello", true); 
        d.setLocationRelativeTo(frame);
        d.setVisible(true);
      }
    });
 
    // put the button on the frame
    frame.getContentPane().setLayout(new FlowLayout());
    frame.add(showDialogButton);
 
    // set up the jframe, then display it
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(300, 200));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
Posted
Updated 10-Aug-14 20:32pm
v2
Comments
Member 10946695 8-Aug-14 7:41am    
please help me

1 solution

You are already using JDialog.

That's fine!

There are a lot of dialog configurations possible, so please take a look here:

How to Make Dialogs @ Oracle Tutorial[^]


But sometimes you want some more to be in such a dialog.
How about giving JDialog some more love:

Java
public class MyDialog extends JDialog{
  
  private final Component parent;

  public MyDialog(final Component parent){
     this.parent = parent;
     this.init();
  }

  private void init(){
     this.setLocationRelativeTo(parent);
     // add more fun here!
  }

}


you can then call that by using:

Java
static void displayJFrame()
  {
     JButton showDialogButton= new JButton ("Show dialog box");
   
    showDialogButton.addActionListener(new ActionListener()
    {
            @Override
      public void actionPerformed(ActionEvent e)
      {
        // display/center the jdialog when the button is pressed
        MyDialog d = new MyDialog(frame); // call own dialog
        d.setVisible(true);
      }
    });
}


Also please think about whether you need a dialog (modal) or more a second frame (non modal) or if you even want to create a Wizard[^] with multiple steps in dialogs.

EDIT:

Your code needs some improvement.
You need to break out of that static context.
That will make your coding much simpler.

compare this to your coding:
Java
public class test_dialogue_1
{
  public static void main(String[] args)
  {
       test_dialogue_1 dialog = new test_dialogue_1();
       dialog.displayJFrame();
  }
 
  private void displayJFrame(){
    // add fun here!
  }

You see, that is not even the static member variable any more. It's not used outside the main function, so there is no need to make a member.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900