Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Creating a Button in a JDialog

Zulfi KhanOct 12 2016 — edited Oct 13 2016

Hi,

I have created a menuItem & when i press the menuitem a JOptionPane dialog box opens. I want to create a button in a dialog box when menuitem is clicked. I am not interested in JOptionPane. But my example code JOptionPane. Can somebody please guide me how to create a button in a dialog box?

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

public class MainMenu2 implements ActionListener {

  JFrame f;

  JMenuBar jmb;

  JMenu jmFile;

  JMenuItem jmiOpen;

  MainMenu2() {

    f = new JFrame("Menu Demo");

    f.setSize(220, 200);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jmb = new JMenuBar();

    jmFile = new JMenu("File");

    jmiOpen = new JMenuItem("Open");

    jmFile.add(jmiOpen);

    jmb.add(jmFile);

    jmiOpen.addActionListener(this);

    f.setJMenuBar(jmb);

    f.setVisible(true);

}

public void actionPerformed(ActionEvent ae) {

    ShowDialogBox(jmiOpen);

  }

  public static void main(String args[]) {

    new MainMenu();

  }

   void ShowDialogBox(Component parent)

    {  //JFrame f = new JFrame();

     JOptionPane pane = new JOptionPane(

          "New label", JOptionPane.QUESTION_MESSAGE

       );

      pane.setWantsInput(true);

      JDialog dialog = pane.createDialog(parent, "Enter Text");

      // Uncomment line and comment out next

      //  to see application modal

       JButton btn= new JButton("Send");

       btn.addActionListener(this);

       //pane.add(btn);

       dialog.add(btn);

       dialog.setModalityType(

       Dialog.ModalityType.APPLICATION_MODAL);

      dialog.setModalityType(

         Dialog.ModalityType.DOCUMENT_MODAL);

      dialog.setVisible(true);

    }

}

Right now its creating a JOptionPane style dialog box with OK button. I want to put a "send" button also.

Zulfi.

This post has been answered by Zulfi Khan on Oct 13 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 10 2016
Added on Oct 12 2016
2 comments
1,288 views