Active new Dialog event it do all command and after hiden active code
hi for all
i'm create a FontDialog class when use it in another class in normal
make this step:
FontChoicer fc=new FontChoicer(new test(),"Font");
fc.show(true);
Font f=fc.getFont();
the method getFont it return the font if user click OK and when user click
Cancel return null.
this program when i create object from FontChoicer and call getFont it return null whate can i do to stop all code untel user click Ok or Cancel
and this code of this class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
publicclass FontChoicerextends JDialog
{
String fontName,TempFontName="Times New Roman";
Integer fontType,TempFontType=Font.PLAIN;
Integer fontSize,TempFontSize=14;
boolean DialogRun;
/*object in Dialog*/
JPanel CenterPanel=new JPanel();
JPanel SouthPanel=new JPanel();
JPanel RightSouthPanel=new JPanel();
JPanel LeftSouthPanel=new JPanel();
JTextField Sample=new JTextField("Sample");
JPanel NorthPanel=new JPanel();
JPanel LeftPanel=new JPanel();
JPanel LeftCenterPanel=new JPanel();
JPanel RightCenterPanel=new JPanel();
JPanel UnderLeftPanel=new JPanel();
JPanel UperLeftPanel=new JPanel();
JPanel UperLeftCenterPanel=new JPanel();
JPanel UnderLeftCenterPanel=new JPanel();
JPanel UperRightCenterPanel=new JPanel();
JPanel UnderRightCenterPanel=new JPanel();
JList listFont=new JList();
JList listType=new JList();
JList listSize=new JList();
JButton okButton =new JButton("OK");
JButton cancelButton =new JButton("Cancel");
JScrollPane scrollListFont=new JScrollPane();
JScrollPane scrollListType=new JScrollPane();
JScrollPane scrollListSize=new JScrollPane();
String Title;
/*constractor*/
FontChoicer(JFrame frame,String Title)
{
String s[]=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
String font[]=new String[ s.length];
String size[]=new String[36];
String type[]={"Regular","Bold","Italic"};
for (int i=0;i < s.length ;i++)
{
font[i]=s[i];
}
for (Integer i=0;i <=35 ;i++)
{Integer d=i*2;
if (d==0)d=1;
size[i]=(d).toString();
}
Title=this.Title;
setTitle(Title);
setSize(400,300);
CenterPanel.setBorder(BorderFactory.createLineBorder (Color.DARK_GRAY, 1));
add(NorthPanel,BorderLayout.NORTH);
add(SouthPanel,BorderLayout.SOUTH);
add(CenterPanel,BorderLayout.CENTER);
CenterPanel.setLayout(new GridLayout(1,4));
SouthPanel.setLayout(new GridLayout(1,2));
Sample.setEditable(false);
RightSouthPanel.add(Sample);
SouthPanel.add(RightSouthPanel);
LeftSouthPanel.add(okButton);
LeftSouthPanel.add(cancelButton);
SouthPanel.add(LeftSouthPanel);
CenterPanel.add(LeftPanel);
CenterPanel.add(LeftCenterPanel);
CenterPanel.add(RightCenterPanel);
listFont.setListData(font);
listFont.setSelectedIndex(8);
listType.setListData(type);
listType.setSelectedIndex(0);
listSize.setListData(size);
listSize.setSelectedIndex(7);
System.out.println();
scrollListFont.getViewport().setView(listFont);
scrollListType.getViewport().setView(listType);
scrollListSize.getViewport().setView(listSize);
LeftPanel.setLayout(new BorderLayout());
UperLeftPanel.add(new JLabel("Font Name"));
UnderLeftPanel.add(new JLabel(""));
LeftPanel.add(UperLeftPanel,BorderLayout.NORTH);
LeftPanel.add(UnderLeftPanel,BorderLayout.SOUTH);
LeftPanel.add(scrollListFont,BorderLayout.CENTER);
UperLeftCenterPanel.add(new JLabel("Font Style"));
UnderLeftCenterPanel.add(new JLabel(""));
LeftCenterPanel.setLayout(new BorderLayout());
LeftCenterPanel.add(UperLeftCenterPanel,BorderLayout.NORTH);
LeftCenterPanel.add(UnderLeftCenterPanel,BorderLayout.SOUTH);
LeftCenterPanel.add(scrollListType,BorderLayout.CENTER);
UperRightCenterPanel.add(new JLabel("Font Size"));
UnderRightCenterPanel.add(new JLabel(""));
RightCenterPanel.setLayout(new BorderLayout());
RightCenterPanel.add(UperRightCenterPanel,BorderLayout.NORTH);
RightCenterPanel.add(UnderRightCenterPanel,BorderLayout.SOUTH);
RightCenterPanel.add(scrollListSize,BorderLayout.CENTER);
listFont.addListSelectionListener(new ListSelectionListener()
{
publicvoid valueChanged(ListSelectionEvent e)
{
TempFontName=(String)listFont.getSelectedValue();
Sample.setFont(new Font(TempFontName,TempFontType,TempFontSize));
}
});
listType.addListSelectionListener(new ListSelectionListener()
{
publicvoid valueChanged(ListSelectionEvent e)
{
if (((String)listType.getSelectedValue()).equals("Regular"))TempFontType=Font.PLAIN;
if (((String)listType.getSelectedValue()).equals("Bold"))TempFontType=Font.BOLD;
if (((String)listType.getSelectedValue()).equals("Italic"))TempFontType=Font.ITALIC;
Sample.setFont(new Font(TempFontName,TempFontType,TempFontSize));
}
});
listSize.addListSelectionListener(new ListSelectionListener()
{
publicvoid valueChanged(ListSelectionEvent e)
{
TempFontSize=Integer.parseInt((String)listSize.getSelectedValue());
Sample.setFont(new Font(TempFontName,TempFontType,TempFontSize));
}
});
okButton.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
fontName=TempFontName;
fontType=TempFontType;
fontSize=TempFontSize;
setVisible(false);
DialogRun=true;
}
});
cancelButton.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
}/* end constractor*/
public Font getFont()
{
if(DialogRun)returnnew Font(fontName,fontType,fontSize);
elsereturnnull;
}
publicint getFontSize()
{
if(DialogRun)return fontSize;
elsereturn 0;
}
public String getFontName()
{
if(DialogRun)return fontName;
elsereturnnull;
}
publicint getFontStyle()
{
if(DialogRun)return fontType;
elsereturn 0;
}
public String getFileName()
{
return Title;
}
publicvoid setFileName(String Title)
{
Title=Title;
}
}
[11738 byte] By [
MHanafya] at [2007-11-26 14:52:46]

# 2
ok here's my solution. Works really well but you're gonna need all sorts of my classes. See the showFontChooserDialog method
You are welcome to use and modify these classes but please do not change the packages on them or take credit for them as your own work
classes are:
FontChooser
StandardDialog
ModalOnParentDialog
/*
* Created on Nov 7, 2004 by @author Tom Jacobs
*
*/
package tjacobs.ui;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.*;
/**
* A font chooser panel. Pop it up and see what happens
*/
public class FontChooser extends JPanel {
public static final int[] DEFAULT_FONT_SIZES = new int[] {8, 10, 12, 14, 18, 22, 26, 32, 48, 72};
private JComboBox mFontFamily;
private JComboBox mFontSize;
JToggleButton bold = new JToggleButton("B");
JToggleButton italic = new JToggleButton("I");
JToggleButton underline = new JToggleButton("U");
public FontChooser() {
setLayout(new FlowLayout());
Toolkit kit = Toolkit.getDefaultToolkit();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
mFontFamily = new JComboBox();
mFontFamily.setEditable(false);
mFontSize = new JComboBox();
String fonts[] = ge.getAvailableFontFamilyNames();
for (int i = 0; i < fonts.length; i++) {
mFontFamily.addItem(fonts[i]);
}
for (int i = 0; i < DEFAULT_FONT_SIZES.length; i++) {
mFontSize.addItem("" + DEFAULT_FONT_SIZES[i]);
}
add(mFontFamily);
add(bold);
add(italic);
add(underline);
add(mFontSize);
//kit.getFontList();
}
public FontChooser(Font current) {
this();
setFontChoices(current);
}
private void setFontChoices(Font f) {
mFontFamily.setSelectedItem(f.getFamily());
mFontSize.setSelectedItem("" + f.getSize());
int styles = f.getStyle();
bold.setSelected((styles & Font.BOLD) != 0);
italic.setSelected((styles & Font.ITALIC) != 0);
underline.setSelected((styles & Font.CENTER_BASELINE) != 0 || (Font.HANGING_BASELINE & styles)!= 0);
}
public Font getChooserFont() {
String family = mFontFamily.getSelectedItem().toString();
int size = Integer.parseInt(mFontSize.getSelectedItem().toString());
int style = 0;
style = style | (bold.isSelected() ? Font.BOLD : 0);
style = style | (italic.isSelected() ? Font.ITALIC : 0);
style = style | (underline.isSelected() ? Font.CENTER_BASELINE : 0);
return new Font(family, style, size);
}
public static Font showFontChooserDialog(Frame f, Font current) {
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
final FontChooser fc = current == null ? new FontChooser() : new FontChooser(current);
final JLabel l = new JLabel("Font Style");
l.setFont(fc.getChooserFont());
pane.add(l, BorderLayout.NORTH);
pane.add(fc, BorderLayout.CENTER);
StandardDialog sd = new StandardDialog(f, "Choose Font", true) {
public void apply() {
l.setFont(fc.getChooserFont());
l.repaint();
}
public boolean showApplyButton() {
return true;
}
};
sd.setMainContent(pane);
sd.pack();
if (f.isVisible()) {
Point p = f.getLocationOnScreen();
sd.setLocation(p.x + 30, p.y + 30);
}
else {
sd.setLocation(100, 100);
}
sd.setVisible(true);
/*try {
sd.wait();
}
catch (InterruptedException ex) {
sd.dispose();
return null;
}*/
return fc.getChooserFont();
}
public static Font showFontChooserDialog(Dialog f, Font current) {
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
final FontChooser fc = current == null ? new FontChooser() : new FontChooser(current);
final JLabel l = new JLabel("Font Style");
l.setFont(fc.getChooserFont());
pane.add(l, BorderLayout.NORTH);
pane.add(fc, BorderLayout.CENTER);
StandardDialog sd = new StandardDialog(f, "Choose Font", true) {
public void apply() {
l.setFont(fc.getChooserFont());
l.repaint();
}
public boolean showApplyButton() {
return true;
}
};
sd.setMainContent(pane);
sd.pack();
if (f.isVisible()) {
Point p = f.getLocationOnScreen();
sd.setLocation(p.x + 30, p.y + 30);
}
else {
sd.setLocation(100, 100);
}
sd.setVisible(true);
/*synchronized(sd) {
try {
sd.wait();
}
catch (InterruptedException ex) {
sd.dispose();
return null;
}
}
*/
return fc.getChooserFont();
}
public static void main(String[] args) {
FontChooser fc = new FontChooser();
WindowUtilities.visualize(fc);
}
}
package tjacobs.ui;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.JDialog;
/**
* Standard Dialog is a basic dialog with Apply/Ok/Cancel buttons
* To implement it, you have to implement apply() which gets run
* when the user hits either apply or ok, and showApplyButton(),
* which says whether or not to show the apply button
*/
//public abstract class StandardDialog extends JDialog {
public abstract class StandardDialog extends ModalOnParentDialog {
public StandardDialog() throws HeadlessException {
super();
_init();
}
public StandardDialog(Dialog owner) throws HeadlessException {
super(owner);
_init();
}
public StandardDialog(Dialog owner, boolean modal) throws HeadlessException {
super(owner, modal);
_init();
}
public StandardDialog(Frame owner) throws HeadlessException {
super(owner);
_init();
}
public StandardDialog(Frame owner, boolean modal) throws HeadlessException {
super(owner, modal);
_init();
}
public StandardDialog(Dialog owner, String title) throws HeadlessException {
super(owner, title);
_init();
}
public StandardDialog(Dialog owner, String title, boolean modal)
throws HeadlessException {
super(owner, title, modal);
_init();
}
public StandardDialog(Frame owner, String title) throws HeadlessException {
super(owner, title);
_init();
}
public StandardDialog(Frame owner, String title, boolean modal)
throws HeadlessException {
super(owner, title, modal);
_init();
}
public StandardDialog(Dialog owner, String title, boolean modal,
GraphicsConfiguration gc) throws HeadlessException {
super(owner, title, modal, gc);
_init();
}
public StandardDialog(Frame owner, String title, boolean modal,
GraphicsConfiguration gc) {
super(owner, title, modal, gc);
_init();
}
/*
public void setVisible(boolean b) {
super.setVisible(b);
if (b) StandardFrame.Watch(this); else StandardFrame.RemoveWatch(this);
}
*/
public void setMainContent(Component c) {
mContent.add(c, BorderLayout.CENTER);
mMainPane = c;
}
protected void _init() {
mContent = getContentPane();
mContent.setLayout(new BorderLayout());
mButtonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
mContent.add(mButtonPane, BorderLayout.SOUTH);
mButtonListener = new ButtonListener();
if (showApplyButton()) {
mApply = new JButton("Apply");
mButtonPane.add(mApply);
mApply.addActionListener(mButtonListener);
}
mOk = new JButton("OK");
mButtonPane.add(mOk);
mOk.addActionListener(mButtonListener);
mCancel = new JButton("Cancel");
mButtonPane.add(mCancel);
mCancel.addActionListener(mButtonListener);
}
public Component getMainPane() {
return mMainPane;
}
public void ok (){
mCancelState = OK;
apply();
close();
}
public void cancel() {
mCancelState = CANCEL;
close();
}
public void close () {
dispose();
}
public abstract boolean showApplyButton();
public abstract void apply();
public void dispose() {
if (mCancelState != OK) {
mCancelState = CANCEL;
}
synchronized(this) {
notifyAll();
}
super.dispose();
}
protected class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
JButton b = (JButton) ae.getSource();
if (b == mApply) {
apply();
}
else if (b == mOk) {
ok();
}
else if (b == mCancel) {
cancel();
}
}
}
public int getCancelState() {
return mCancelState;
}
public static int showStandardDialog(StandardDialog sd) {
Window w = sd.getOwner();
if (w == null) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Dimension ps = sd.getPreferredSize();
return showStandardDialog(sd, (d.width - ps.width) / 2, (d.height - ps.height) / 2);
}
else {
Point p = w.getLocation();
Dimension d = w.getSize();
return showStandardDialog(sd, p.x + 100, p.y + 20);
}
}
public static int showStandardDialog(final StandardDialog sd, int x, int y) {
sd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
sd.cancel();
}
});
sd.setLocation(x, y);
sd.pack();
sd.setVisible(true);
synchronized(sd) {
try {
sd.wait();
}
catch (InterruptedException ex) {}
}
return sd.getCancelState();
}
public static final int OK = 1;
public static final int SHOWN = 0;
public static final int CANCEL = -1;
private int mCancelState = 0;
protected boolean mBlockInCode;
protected Container mContent;
protected Component mMainPane;
protected JButton mApply;
protected JButton mOk;
protected JButton mCancel;
protected JPanel mButtonPane;
protected ActionListener mButtonListener;
}
/*
* Created on Jul 12, 2006 by @author Tom Jacobs
*
*/
package tjacobs.ui;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import java.awt.Container;
public class ModalOnParentDialog extends JDialog implements ComponentListener,
WindowListener {
private Component mOwner;
private boolean mParentOnly = false;
public ModalOnParentDialog() throws HeadlessException {
super();
init(null);
}
public ModalOnParentDialog(Frame arg0) throws HeadlessException {
super(arg0);
init(arg0);
}
public ModalOnParentDialog(Frame arg0, boolean arg1)
throws HeadlessException {
super(arg0, arg1);
init(arg0);
}
public ModalOnParentDialog(Frame arg0, String arg1)
throws HeadlessException {
super(arg0, arg1);
init(arg0);
}
public ModalOnParentDialog(Frame arg0, String arg1, boolean arg2)
throws HeadlessException {
super(arg0, arg1, arg2);
init(arg0);
}
public ModalOnParentDialog(Frame arg0, String arg1, boolean arg2,
GraphicsConfiguration arg3) {
super(arg0, arg1, arg2, arg3);
init(arg0);
}
public ModalOnParentDialog(Dialog arg0) throws HeadlessException {
super(arg0);
init(arg0);
}
public ModalOnParentDialog(Dialog arg0, boolean arg1)
throws HeadlessException {
super(arg0, arg1);
init(arg0);
}
public ModalOnParentDialog(Dialog arg0, String arg1)
throws HeadlessException {
super(arg0, arg1);
init(arg0);
}
public ModalOnParentDialog(Dialog arg0, String arg1, boolean arg2)
throws HeadlessException {
super(arg0, arg1, arg2);
init(arg0);
}
public ModalOnParentDialog(Dialog arg0, String arg1, boolean arg2,
GraphicsConfiguration arg3) throws HeadlessException {
super(arg0, arg1, arg2, arg3);
init(arg0);
}
private void init(Component c) {
mOwner = c;
addComponentListener(this);
addWindowListener(this);
}
public void componentResized(ComponentEvent arg0) {}
public void componentMoved(ComponentEvent arg0) {}
public void componentShown(ComponentEvent arg0) {
doModalOp();
}
public void componentHidden(ComponentEvent arg0) {
doModalOp();
}
public boolean isParentOnly() {
return mParentOnly;
}
public void setParentOnly(boolean b) {
mParentOnly = b;
}
public void setModal(boolean b) {
super.setModal(b);
//mModal = b;
doModalOp();
}
private void doModalOp() {
if (mOwner != null) {
mOwner.setEnabled(!super.isModal() || !isVisible());
}
}
public boolean isModal() {
return super.isModal() && !mParentOnly;
}
//public boolean isParentModel() {
//return mModal;
//}
public void windowOpened(WindowEvent arg0) {
doModalOp();
}
public void dispose() {
//setVisible(false);
//doModalOp();
if (mOwner != null) {
mOwner.setEnabled(true);
}
removeComponentListener(this);
removeWindowListener(this);
super.dispose();
}
public void pack() {
super.pack();
addListeners();
}
private boolean addListeners() {
WindowListener[] listeners = this.getWindowListeners();
if (listeners != null) {
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == this) return false;
}
}
addComponentListener(this);
addWindowListener(this);
return true;
}
public void show() {
super.show();
addListeners();
if(super.isModal() && mOwner != null) {
mOwner.setEnabled(false);
}
}
public void show(boolean b) {
super.show(b);
addListeners();
if(super.isModal() && mOwner != null) {
mOwner.setEnabled(false);
}
}
public void windowClosing(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {
doModalOp();
}
public void windowIconified(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public static void main(String[] args) {
JFrame jf1 = new JFrame("1"), jf2 = new JFrame("2");
final ModalOnParentDialog d = new ModalOnParentDialog(jf1, "3");
d.setModal(true);
d.setParentOnly(true);
JButton b = new JButton("Show");
JButton b2 = new JButton("Show2");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
d.setVisible(!d.isVisible());
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
d.show();
}
});
jf1.setBounds(0,0,40,40);
jf1.setDefaultCloseOperation(jf1.DISPOSE_ON_CLOSE);
jf2.setDefaultCloseOperation(jf2.DISPOSE_ON_CLOSE);
d.setDefaultCloseOperation(d.DISPOSE_ON_CLOSE);
jf2.setBounds(40,40,40,40);
Container c = jf2.getContentPane();
c.setLayout(new FlowLayout());
c.add(b);
c.add(b2);
d.setBounds(80,80,40,40);
jf1.setVisible(true);
jf2.setVisible(true);
d.setVisible(true);
}
}