I have created a window containing only one button(park1). All I need is a new dialog(with
"OK" and "Cancel" buttons to open when I click on park1.

Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyWin extends JFrame implements ActionListener
{
 JButton park1;

 MyWin()
 {
   setSize(450, 500);
   setVisible(true);
   setDefaultCloseOperation(DISPOSE_ON_CLOSE);

   initComponents();
 }

 
 void initComponents()
 {
   park1 = new JButton("Add");
   setLayout(null);
   add(park1);
   park1.setBounds(130, 350, 100, 40);
   park1.addActionListener(this);
   
 }


 public void actionPerformed(ActionEvent e)
 {
   try
   {
     Object temp = e.getSource();
     if(temp.equals(park1))
     {
       //code for opening a new window
     }
   }
   catch(Exception ex)
   {
   }
 }

 public static void main(String args[])
 {

   MyWin mw1 = new MyWin();

 }
}

I use a JOptionPane.
You can make your own JPanel, to contain whatever you wish, and attach it to (display it with) your JOptionPane.

Comments
excelent suggestion

And... "All I need is a new dialog with "OK" and "Cancel" buttons to open when I click on park1."
...doesn't tell you what the OP needs?

Edited 5 Years Ago by hfx642: n/a

in an assignment, yes, in a vague description, not necessarily
given by what he said, I would assume you are right, and that using JOptionPane is indeed all he needs, but as far as I know, maybe he needs to put a company logo or anything in that, or needs his dialog to contain several input fields.

Which is why I said...
"You can make your own JPanel, to contain whatever you wish, and attach it to (display it with) your JOptionPane."

@hfx642 +1


I strongly recommend to use JDialog/JWindow as another Container, there are lots of common issues with usage two (or more) JFrames in same time, starting with FocusRecycle, toFront ....

Edited 5 Years Ago by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.