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();

 }
}

Recommended Answers

All 7 Replies

what kind of 'Dialog' do you want to open? a class extending JFrame? a JOptionPane DialogBox?

Member Avatar for hfx642

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

commented: excelent suggestion +9

nja, but the question is, what does the OP need? and what does he want?

Member Avatar for hfx642

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?

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.

Member Avatar for hfx642

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 ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.