Dialog Boxes - File

import java.awt.Color;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DialogDemo1 extends JFrame implements ActionListener {
	JPanel p1;
	JButton b1[];
	Dialog1 d1;

	public DialogDemo1() {
		super("win");
		setSize(500, 500);
		getContentPane().add(p1 = new JPanel());
		b1 = new JButton[10];
		for (int i = 0; i < b1.length; i++) {
			p1.add(b1[i] = new JButton("Dialog " + i));
			b1[i].addActionListener(this);
		}
		// button 1
		b1[0].setText("File Dialog");
		b1[1].setText("Modelless Dialog");
		b1[2].setText("Model Dialog");
		b1[3].setText("Color Chooser");
		b1[4].setText("Dialog Demo");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		d1 = new Dialog1();
		d1.b1.addActionListener(this);
		d1.b2.addActionListener(this);

		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		if (o == b1[0]) {
			FileDialog fc = new FileDialog(this);
			fc.setDirectory("C:\\");
			fc.setVisible(true);
			String s = fc.getDirectory();
			String fn = fc.getFile();
			if (fn == null)
				System.out.println("You cancelled the choice");
			else
				System.out.println("You chose " + s + fn);
		}
		if (o == b1[1]) {
			JDialog dialog1 = new JDialog(this, "Dialog1 - Modeless Dialog");
			dialog1.setBounds(200, 200, 300, 200);
			dialog1.setVisible(true);
		}
		if (o == b1[2]) {
			JDialog dialog = new JDialog(this, "Application-Modal Dialog",
					Dialog.ModalityType.APPLICATION_MODAL);
			dialog.setBounds(200, 150, 200, 150);
			dialog.setVisible(true);
		}
		if (o == b1[3]) {
			Color bgColor = JColorChooser.showDialog(this,
					"Choose Background Color", getBackground());
			if (bgColor != null)
				p1.setBackground(bgColor);
		}
		if (o == b1[4]) {
			d1.setVisible(true);
		}
		if (o == b1[5]) {
		}
		if (o == b1[6]) {
		}
		if (o == b1[3]) {
		}
		if (o == d1.b1) {

		}
		if (o == d1.b2) {
			d1.setVisible(false);
		}
	}

	public static void main(String args[]) {
		new DialogDemo1();
	}
}

class Dialog1 extends JDialog {
	JPanel p2;
	JButton b1, b2;

	public Dialog1() {
		super();
		setSize(200, 300);
		getContentPane().add(p2 = new JPanel());
		p2.add(b1 = new JButton("Save"));
		p2.add(b2 = new JButton("cancel"));
	}
}

Download

Swing Dialogboxes

Dialog Boxes - File, Modelless, Model, Color Dialog box
Dialog Box - java.awt.Dialog


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code