Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.2K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 437 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Creating a Button in a JDialog

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