Forum Stats

  • 3,769,960 Users
  • 2,253,036 Discussions
  • 7,875,253 Comments

Discussions

Creating a Button in a JDialog

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Oct 13, 2016 2:22PM in New To Java

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.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 13, 2016 2:22PM Accepted Answer

    Hi,

    Thanks for your response. Sorry in the tutorial, i just found JOptionPane Dialogs. It shows limited features of Java. I thought we cant add Radio buttons  in the dialog box. Then i check it in the book & i got a program which i modified. If possible kindly add this program in the tutorial.

    import java.awt.*;

    import java.awt.event.*;

    import java.util.*;

    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;

    import javax.swing.*;

    public class MenuFrameD extends JFrame  {

      MenuFrameD() {

        JFrame f = new JFrame("Menu Demo");

        f.setSize(220, 200);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar jmb = new JMenuBar();

       

        JMenu file = new JMenu("File");

        JMenuItem item1= new JMenuItem("New...");

        file.add(item1);

        jmb.add(file);

        f.setVisible(true);

        f.setJMenuBar(jmb);

        MyMenuHandler handler = new MyMenuHandler();

        item1.addActionListener(handler);

       

        //MyWindowAdapter adapter = new MyWindowAdapter(this);

        //addWindowListener(adapter);

       

       

      }

       public static void main(String args[]) {

        new MenuFrameD();

      }

    }

    class SampleDialog extends  JDialog implements ActionListener{

      JTextField tf;

       SampleDialog(JFrame parent, String title){

       super(parent, title, false);

       setLayout(new FlowLayout());

       setSize(300, 200);

       add(new JLabel("Press the button"));

       add(new JRadioButton("Yellow"));

       add(new JRadioButton("Green"));

       add(new JRadioButton("Orange"));

       tf = new JTextField();

        tf.setPreferredSize( new Dimension( 200, 24 ) );

       add(tf);

       JButton b;

       add(b= new JButton("Send"));

       b.addActionListener(this);

      }

      public void actionPerformed(ActionEvent ae){

         String t = tf.getText();

         //System.out.println(t);

         JOptionPane.showMessageDialog(null, t);

         dispose();

      }

    }

    /*class MyWindowAdapter extends WindowAdapter{

       MenuFrame menuFrame;

      

       public MyWindowAdapter(MenuFrame menuFrame){

          this.menuFrame = menuFrame;

       }

       public void WindowClosing(WindowEvent we) {

         meuFrame.setVisible(false);

       }

    }*/

    class MyMenuHandler implements ActionListener/*, ItemListener*/ {

      MenuFrameD obj;

      public void actionPerformed(ActionEvent ae) {

        String comStr = ae.getActionCommand();

        if(comStr.equals("New...")){

           SampleDialog d = new SampleDialog(obj , "New Dialog Box");

           d.setVisible(true);

        }

       

      }

    }

    I dont know why WindowAdapter is not working??

    Zulfi.

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Oct 12, 2016 2:09PM
    2973483 wrote: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?Right now its creating a JOptionPane style dialog box with OK button. I want to put a "send" button also.Zulfi.

    Oracle has a complete trial on Dialogs in its tutorial collection:

    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    bye

    TPD

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 13, 2016 2:22PM Accepted Answer

    Hi,

    Thanks for your response. Sorry in the tutorial, i just found JOptionPane Dialogs. It shows limited features of Java. I thought we cant add Radio buttons  in the dialog box. Then i check it in the book & i got a program which i modified. If possible kindly add this program in the tutorial.

    import java.awt.*;

    import java.awt.event.*;

    import java.util.*;

    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;

    import javax.swing.*;

    public class MenuFrameD extends JFrame  {

      MenuFrameD() {

        JFrame f = new JFrame("Menu Demo");

        f.setSize(220, 200);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar jmb = new JMenuBar();

       

        JMenu file = new JMenu("File");

        JMenuItem item1= new JMenuItem("New...");

        file.add(item1);

        jmb.add(file);

        f.setVisible(true);

        f.setJMenuBar(jmb);

        MyMenuHandler handler = new MyMenuHandler();

        item1.addActionListener(handler);

       

        //MyWindowAdapter adapter = new MyWindowAdapter(this);

        //addWindowListener(adapter);

       

       

      }

       public static void main(String args[]) {

        new MenuFrameD();

      }

    }

    class SampleDialog extends  JDialog implements ActionListener{

      JTextField tf;

       SampleDialog(JFrame parent, String title){

       super(parent, title, false);

       setLayout(new FlowLayout());

       setSize(300, 200);

       add(new JLabel("Press the button"));

       add(new JRadioButton("Yellow"));

       add(new JRadioButton("Green"));

       add(new JRadioButton("Orange"));

       tf = new JTextField();

        tf.setPreferredSize( new Dimension( 200, 24 ) );

       add(tf);

       JButton b;

       add(b= new JButton("Send"));

       b.addActionListener(this);

      }

      public void actionPerformed(ActionEvent ae){

         String t = tf.getText();

         //System.out.println(t);

         JOptionPane.showMessageDialog(null, t);

         dispose();

      }

    }

    /*class MyWindowAdapter extends WindowAdapter{

       MenuFrame menuFrame;

      

       public MyWindowAdapter(MenuFrame menuFrame){

          this.menuFrame = menuFrame;

       }

       public void WindowClosing(WindowEvent we) {

         meuFrame.setVisible(false);

       }

    }*/

    class MyMenuHandler implements ActionListener/*, ItemListener*/ {

      MenuFrameD obj;

      public void actionPerformed(ActionEvent ae) {

        String comStr = ae.getActionCommand();

        if(comStr.equals("New...")){

           SampleDialog d = new SampleDialog(obj , "New Dialog Box");

           d.setVisible(true);

        }

       

      }

    }

    I dont know why WindowAdapter is not working??

    Zulfi.

This discussion has been closed.