Re: Always on top JDialog or JWindow



On 28 Sep 2005 08:35:46 -0700, "blue" <fluyckx@xxxxxxxxxxxx> wrote or
quoted :

>Anyone know what the best implementation for this would be?

you could do a variant of this about box without a dismiss button and
strip nearly all the field out of it and replace them with your own.

The basic idea is you Extend Dialog.


package com.mindprod.common11;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* An AWT About box that truly tells you about he program, not just
the author's
* name.
*
* @author Roedy Green
*/
public class CMPAboutBox extends Dialog {

static final boolean DEBUGGING = false;

// Use our own colours so Symantec won't mess with them or create
dups.
private static final Color black = Color.black;

private static final Color blue = Color.blue;

private static final Color darkGreen = new Color( 0, 128, 0 );

private static final Color red = Color.red;

private static final Color white = Color.white;

/**
* Create an about box, with default author Roedy Green
*
* @param parent
* frame for this about box.
* @param progname
* Program name
* @param version
* Program version e.g. "1.3"
* @param purpose1
* what is this program for? line-1
* @param purpose2
* what is this program for? line-2. may be null, or "".
* @param status
* e.g. "unregistered shareware", "freeware", "commercial",
"company
* confidential"
* @param released
* Date released e.g. "1999-12-31"
* @param copyright
* e.g. "1996-2005"
* @param masterSite
* e.g. CONVERTER -- where to find most up to date ZIP
*/
public CMPAboutBox( Frame parent, String progname, String version,
String purpose1, String purpose2, String status, String
released,
String copyright, String masterSite )
{
this( parent, progname, version, purpose1, purpose2, status,
released,
copyright, "Roedy Green", masterSite );
}

/**
* Create an about box
*
* @param parent
* frame for this about box.
* @param progname
* Program name
* @param version
* Program version e.g. "1.3"
* @param purpose1
* what is this program for? line-1
* @param purpose2
* what is this program for? line-2. may be null, or "".
* @param status
* e.g. "unregistered shareware", "freeware", "commercial",
"company
* confidential"
* @param released
* Date released e.g. "1999/12/31"
* @param copyright
* e.g. "1996-2004"
* @param author
* e.g. "Roedy Green"
* @param masterSite
* e.g. CONVERTER -- where to find most up to date ZIP
*/
public CMPAboutBox( Frame parent, String progname, String version,
String purpose1, String purpose2, String status, String
released,
String copyright, String author, String masterSite )
{
super( parent, progname + " " + version, false /* not modal */);
guts( progname, version, purpose1, purpose2, status, released,
copyright,
author, masterSite );
}

/**
* Create an about box when don't have parent
*
* @param progname
* Program name
* @param version
* Program version e.g. "1.3"
* @param purpose1
* what is this program for? line-1
* @param purpose2
* what is this program for? line-2. may be null, or "".
* @param status
* e.g. "unregistered shareware", "freeware", "commercial",
"company
* confidential"
* @param released
* Date released e.g. "1999/12/31"
* @param copyright
* e.g. "1996-2004"
* @param author
* e.g. "Roedy Green"
* @param masterSite
* e.g. CONVERTER -- where to find most up to date ZIP
*/
public CMPAboutBox( String progname, String version, String
purpose1,
String purpose2, String status, String released, String
copyright,
String author, String masterSite )
{
this( new Frame( progname + " " + version ) /*
* dummy parent, won't
be
* disposed!!
*/, progname,
version, purpose1, purpose2, status, released, copyright,
author, masterSite );

}

/**
* common parts to all creation
*/
private void guts ( String progname, String version, String
purpose1,
String purpose2, String status, String released, String
copyright,
String author, String masterSite )
{
// basic layout
// 0 1
// 0 ---------------progname
version--------------------------------- 0
//
// 1
---------------------purpose1------------------------------------ 1
// 2
---------------------purpose2------------------------------------ 2
//
// 3
---------------------status-------------------------------------- 3
// 4 --------------released:
xxxxxxxxx-------------------------------- 4
//
// 5 copyright 2000 5
// 6 Roedy Green 6
// 7 Canadian Mind Products 7
// 8 #327 - 964 Heywood Avenue 8
// 9 Victoria, BC Canada V8V 2Y5 phone:(250) 361-9093 9
// 10 roedyg@xxxxxxxxxxxx
http://mindprod.com/products#CONVERTER 10
//
// 11 (Dismiss) 11
// 0 1

// leave room for warning this frame belongs to Java applet.
setSize( 500, 380 );
setLocation( 0, 0 );
setBackground( white );
GridBagLayout gridBagLayout;
gridBagLayout = new GridBagLayout();
GridBagConstraints gbc;

setLayout( gridBagLayout );

_prognameVersion = new Label( progname + " " + version,
Label.CENTER );
_prognameVersion.setFont( new Font( "Dialog", Font.BOLD, 15 )
);
_prognameVersion.setForeground( red );
_prognameVersion.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets( 15, 10, 5, 10 );
( (GridBagLayout)getLayout() ).setConstraints(
_prognameVersion, gbc );
add( _prognameVersion );

_purpose1 = new Label( purpose1, Label.CENTER );
_purpose1.setFont( new Font( "Dialog", Font.ITALIC, 12 ) );
_purpose1.setForeground( black );
_purpose1.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets( 5, 10, 0, 5 );
( (GridBagLayout)getLayout() ).setConstraints( _purpose1, gbc
);
add( _purpose1 );

if ( purpose2 != null && purpose2.length() > 0 )
{
_purpose2 = new Label( purpose2, Label.CENTER );
_purpose2.setFont( new Font( "Dialog", Font.ITALIC, 12 )
);
_purpose2.setForeground( black );
_purpose2.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets( 0, 10, 0, 10 );
( (GridBagLayout)getLayout() ).setConstraints( _purpose2,
gbc );
add( _purpose2 );
}

_status = new Label( status, Label.CENTER );
_status.setFont( new Font( "Dialog", Font.BOLD, 12 ) );
_status.setForeground( blue );
_status.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets( 10, 10, 0, 10 );
( (GridBagLayout)getLayout() ).setConstraints( _status, gbc );
add( _status );

_released = new Label( "released: " + released, Label.CENTER
);
_released.setFont( new Font( "Dialog", Font.PLAIN, 11 ) );
_released.setForeground( blue );
_released.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets( 0, 10, 10, 10 );
( (GridBagLayout)getLayout() ).setConstraints( _released, gbc
);
add( _released );

_copyright = new Label( "copyright " + copyright, Label.LEFT
);
_copyright.setFont( new Font( "Dialog", Font.ITALIC, 11 ) );
_copyright.setForeground( darkGreen );
_copyright.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets( 5, 10, 0, 5 );
( (GridBagLayout)getLayout() ).setConstraints( _copyright, gbc
);
add( _copyright );

_author = new Label( author, Label.LEFT );
_author.setFont( new Font( "Dialog", Font.BOLD + Font.ITALIC,
11 ) );
_author.setForeground( darkGreen );
_author.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 6;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets( 0, 10, 0, 5 );
( (GridBagLayout)getLayout() ).setConstraints( _author, gbc );
add( _author );

_cmp = new Label( "Canadian Mind Products", Label.LEFT );
_cmp.setFont( new Font( "Dialog", Font.BOLD + Font.ITALIC, 11
) );
_cmp.setForeground( darkGreen );
_cmp.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 7;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets( 0, 10, 0, 5 );
( (GridBagLayout)getLayout() ).setConstraints( _cmp, gbc );
add( _cmp );

_addr1 = new Label( "#327 - 964 Heywood Avenue", Label.LEFT );
_addr1.setFont( new Font( "Dialog", Font.ITALIC, 11 ) );
_addr1.setForeground( darkGreen );
_addr1.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 8;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets( 0, 10, 0, 5 );
( (GridBagLayout)getLayout() ).setConstraints( _addr1, gbc );
add( _addr1 );

_addr2 = new Label( "Victoria, BC Canada V8V 2Y5", Label.LEFT
);
_addr2.setFont( new Font( "Dialog", Font.ITALIC, 11 ) );
_addr2.setForeground( darkGreen );
_addr2.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 9;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets( 0, 10, 0, 5 );
( (GridBagLayout)getLayout() ).setConstraints( _addr2, gbc );
add( _addr2 );

_phone = new Label( "phone:(250) 361-9093", Label.RIGHT );
_phone.setFont( new Font( "Dialog", Font.ITALIC, 11 ) );
_phone.setForeground( darkGreen );
_phone.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 9;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets( 0, 5, 0, 10 );
( (GridBagLayout)getLayout() ).setConstraints( _phone, gbc );
add( _phone );

_mailto = new Label( "xxxxx@xxxxxxxxxxxx", Label.LEFT );
_mailto.setFont( new Font( "Dialog", Font.ITALIC, 11 ) );
_mailto.setForeground( darkGreen );
_mailto.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 10;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets( 0, 10, 0, 5 );
( (GridBagLayout)getLayout() ).setConstraints( _mailto, gbc );
add( _mailto );

_http = new Label( "http://mindprod.com/products.html#"; +
masterSite,
Label.RIGHT );
_http.setFont( new Font( "Dialog", Font.ITALIC, 10 ) );
_http.setForeground( darkGreen );
_http.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 10;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets( 0, 5, 0, 10 );
( (GridBagLayout)getLayout() ).setConstraints( _http, gbc );
add( _http );

_dismissButton = new Button( "Dismiss" );
_dismissButton.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
_dismissButton.setForeground( red );
_dismissButton.setBackground( white );
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 11;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets( 15, 10, 15, 10 );
( (GridBagLayout)getLayout() ).setConstraints( _dismissButton,
gbc );
_dismissButton.requestFocus();
add( _dismissButton );

// hook up About box listeners

this.addFocusListener( new FocusAdapter() {

/**
* Handle About Dialog getting focus, give to dismiss
Button.
*
* @param e
* event giving details
*/
public void focusGained ( FocusEvent e )
{
_dismissButton.requestFocus();
} // end focusGained
} // end anonymous class
); // end addFocusListenerline

this.addWindowListener( new WindowAdapter() {

/**
* Handle request to close about box
*
* @param e
* event giving details of closing.
*/
public void windowClosing ( WindowEvent e )
{
dismiss();
} // end WindowClosing
} // end anonymous class
); // end addWindowListener line

_dismissButton.addActionListener( new ActionListener() {

/**
* close down the About box when user clicks Dismiss
*/
public void actionPerformed ( ActionEvent event )
{
Object object = event.getSource();
if ( object == _dismissButton )
{
dismiss();
} // end if
} // end actionPerformed
} // end anonymous class
); // end addActionListener line

// The following code is only necessary if you want
// any keystroke while the Dismiss button has focus
// to simulate clicking the Dismiss button.

_dismissButton.addKeyListener( new KeyAdapter() {

/**
* Handle Dismiss button getting a function keystroke,
treat like a
* dismiss Button click
*
* @param e
* event giving details
*/
public void keyPressed ( KeyEvent e )
{
dismiss();
} // end keyPressed

/**
* Handle Dismiss button getting an ordinary keystroke,
treat like a
* dismiss Button click
*
* @param e
* event giving details
*/
public void keyTyped ( KeyEvent e )
{
dismiss();
} // end keyTyped

} // end anonymous class
); // end addKeyListener

this.validate();
this.setVisible( true );

} // end constructor

/**
* Shutdown the about box
*/
void dismiss ()
{
// close the about box
this.setVisible( false );
// tell AWT to discard all pointers to the Dialog box.
this.dispose();
} // end dismiss

/**
* bypass setBackground bug, by setting it over and over, every
time
* addNotify gets called.
*/
public void addNotify ()
{
super.addNotify();
setBackground( white );
}

Label _prognameVersion;

Label _purpose1;

Label _purpose2;

Label _status;

Label _released;

Label _copyright;

Label _author;

Label _cmp;

Label _addr1;

Label _addr2;

Label _phone;

Label _mailto;

Label _http;

Button _dismissButton;

/**
* sample test driver
*
* @param args
* not used
*/
public static void main ( String[] args )
{
if ( DEBUGGING )
{

final Frame frame = new Frame( "About box test" );
frame.setSize( 450, 400 );

MenuBar mb = frame.getMenuBar();
if ( mb == null )
{
mb = new MenuBar();
frame.setMenuBar( mb );
}

Menu help = mb.getHelpMenu();
if ( help == null )
{
help = new Menu( "Help", /* tearoff */false );
mb.setHelpMenu( help );
}

help.add( new MenuItem( "keyboard" ) );
help.add( new MenuItem( "command line" ) );
help.add( new MenuItem( "About" ) );
help.addActionListener( new ActionListener() {

/**
* Handle Menu Selection Request
*
* @param e
* event giving details of selection
*/
public void actionPerformed ( ActionEvent e )
{
String command = e.getActionCommand();
if ( command.equals( "About" ) )
{

CMPAboutBox about = new CMPAboutBox(
frame,
"Sample Amanuensis",
"1.3",
"Teaches you how to interconvert the 16
basic Java types,",
"e.g. String to int, Long to double.",
"freeware",
"2000/01/01", "1996-2005", "CONVERTER" );

} // end if

} // end ActionListener
} // end anonymous class
); // end addActionListener line

frame.addWindowListener( new WindowAdapter() {

/**
* Handle request to shutdown.
*
* @param e
* event giving details of closing.
*/
public void windowClosing ( WindowEvent e )
{
System.exit( 0 );
} // end WindowClosing
} // end anonymous class
); // end addWindowListener line

frame.validate();
frame.setVisible( true );

} // end if

} // end main

} // end class CMPAboutBox


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
.