// WindowingDemoApplet.java (c) 2007 Kari Laitinen // http://www.naturalprogramming.com // 2007-02-04 File created. // 2007-02-04 Latest modification. // This program demonstrates how a Java program can open additional // windows to the screen. // A JDialog-based dialog can be parented only by a another JDialog or // a JFrame-based window. This means that you cannot open a JDialog in // an applet. You can, however, use the static methods of class // JOptionPane to open dialogs in applets. import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; class PasswordDialog extends JDialog { JTextField user_name_field = new JTextField( "" ) ; JPasswordField password_field = new JPasswordField( "" ) ; TextWindow parent_window ; public PasswordDialog( JFrame given_parent_window, String given_title ) { super( given_parent_window, given_title ) ; parent_window = (TextWindow) given_parent_window ; setSize( 200, 130 ) ; getContentPane().setLayout( new GridLayout( 2, 2, 10, 10 ) ) ; add( new JLabel( "USER NAME:" ) ) ; add( user_name_field ) ; add( new JLabel( "PASSWORD:" ) ) ; add( password_field ) ; } } class TextWindow extends JFrame implements ActionListener { String text_inside_this_window = "This is default text" ; PasswordDialog password_dialog = new PasswordDialog( this, "THIS IS PasswordDialog" ) ; JButton change_password_button = new JButton( "Change Password" ) ; public TextWindow( String given_window_title ) { super( given_window_title ) ; change_password_button.addActionListener( this ) ; add( "South", change_password_button ) ; } public void change_text( String given_new_text ) { text_inside_this_window = given_new_text ; repaint() ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JButton ) { if ( event.getSource() == change_password_button ) { password_dialog.setVisible( true ) ; } repaint() ; } } public void paint( Graphics graphics ) { super.paint( graphics ) ; graphics.drawString( text_inside_this_window, 100, getHeight() / 2 ) ; } } public class WindowingDemoApplet extends JApplet implements ActionListener { JButton open_window_button = new JButton( "Open Window" ) ; JButton close_window_button = new JButton( "Close Window" ) ; JButton change_text_button = new JButton( "Change Text " ) ; int applet_width, applet_height ; int ball_position_x, ball_position_y ; TextWindow text_window = new TextWindow( "THIS IS TextWindow" ) ; public void init() { text_window.setSize( 300, 200 ) ; setLayout( new BorderLayout() ) ; JPanel operations_panel = new JPanel() ; open_window_button.addActionListener( this ) ; close_window_button.addActionListener( this ) ; change_text_button.addActionListener( this ) ; operations_panel.add( open_window_button ) ; operations_panel.add( close_window_button ) ; operations_panel.add( change_text_button ) ; add( "South", operations_panel ) ; // Let's get the size of the applet (defined in .html file) applet_width = getSize().width ; applet_height = getSize().height ; ball_position_x = applet_width / 2 - 40 ; ball_position_y = applet_height / 2 - 40 ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JButton ) { if ( event.getSource() == open_window_button ) { text_window.setVisible( true ) ; // show the window } else if ( event.getSource() == close_window_button ) { text_window.setVisible( false ) ; // hide the window } else if ( event.getSource() == change_text_button ) { String given_text = JOptionPane.showInputDialog( this, "Type new text for the TextWindow", "This is Input Dialog", JOptionPane.PLAIN_MESSAGE ) ; if ( given_text != null ) { text_window.change_text( given_text ) ; } } repaint() ; } } }