Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
Windows can hold various components (including other Panels and Windows). JPanel is the superclass for all JApplets. Window is the superclass for all JFrames and JDialogs.
The major difference between Frames and Dialogs is that Frames are always modal, but Dialogs can be modal or modalless. Frames are normally used to deal with new, stand-alone, tasks. Dialogs are normally used to get some user input in relation to a current task.
A Frame is a window that has a title and a border. A Frame can also have a menu bar. We can use a frame in the same way as a Panel, except that a Frame has a title, a border and (optionally) a menu bar. Normally the Frame class is extended to create a user Frame class.
By default all Frames are hidden. Use the setVisible(true) method to display a frame.
Event handling for the close icon on a Frame must be handled by the user.
Frame example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FrameDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { public View() { super(); final MyFrame frame = new MyFrame("Demo Frame"); frame.setVisible(true); } } class MyFrame extends JFrame implements ActionListener { private final JButton button1 = new JButton("Button 1"); private final JButton button2 = new JButton("Button 2"); private final JLabel message = new JLabel(); public MyFrame(String title) { super(title); this.setLayout(new GridLayout(1, 3)); this.setSize(600, 100); this.add(this.button1); this.add(this.message); this.add(this.button2); this.button1.addActionListener(this); this.button2.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); if (source == this.button1) { this.message.setText("Button 1 was hit."); } else if (source == this.button2) { this.message.setText("Button 2 was hit."); } } } }
A dialog differs from a frame in that a dialog can be either modal or modeless. If a dialog is a modal, it blocks all input to any other part of the applet until the dialog is exited. Normally the JDialog class is extended to create a user Dialog class.
Dialog visability and event handling are dealt with in the same manner as with frames.
The only valid JDialog constuctor that can be called from an applet is the empty constructor, as show below.
JDialog()
Once the JDialog has been constructed, the title and modal state can be set. These values can be passed into a user defined class that extends JDialog, as shown in the example below.
Dialog example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DialogDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements ActionListener { private MyDialog myDialog = new MyDialog(true, "My Dialog Title"); private JButton button = new JButton("Call Dialog"); private JTextArea textArea = new JTextArea("You can only type in this text area\nwhen the dialog has not been called."); private JLabel message = new JLabel("Dialog has not yet been called"); public View() { super(); setLayout(new GridLayout(3, 1)); setSize(500, 600); add(this.textArea); add(this.button); add(this.message); this.button.addActionListener(this); // give the textarea the keyboard focus and place the cursor at the end of the text. this.textArea.requestFocus(); this.textArea.setCaretPosition(this.textArea.getText().length()); } @Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == this.button) { this.myDialog.setVisible(true); if (this.myDialog.isAccepted()) { this.message.setBackground(Color.green); this.message.setText("The YES key was selected"); } else { this.message.setBackground(Color.red); this.message.setText("The NO key was selected"); } } } } public class MyDialog extends JDialog implements ActionListener { private final JButton ok = new JButton("YES"); private final JButton cancel = new JButton("NO"); private final JLabel message1 = new JLabel("The applet and the applet are disabled while\nthis dialog is open", JLabel.CENTER); private final JLabel message2 = new JLabel("The 'X' close icon is disabled in this dialog", JLabel.CENTER); private final JLabel message3 = new JLabel("Do you wish to continue?", JLabel.CENTER); private JPanel buttonsPanel = new JPanel(); // the variable 'yesButtonSelected' and the method 'isAccepted()' allow the user to check if "YES" button was selected private boolean yesButtonSelected; public boolean isAccepted() { return this.yesButtonSelected; } public MyDialog(boolean isModal, String title) { super(); this.setModal(isModal); this.setTitle(title); this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // disable the close button this.setSize(300, 200); // this.buttonsPanel this.buttonsPanel.setLayout(new GridLayout(1, 2)); this.buttonsPanel.add(this.ok); this.buttonsPanel.add(this.cancel); this.setLayout(new GridLayout(4, 1)); this.add(this.message1); this.add(this.message2); this.add(this.message3); this.add(this.buttonsPanel); this.ok.addActionListener(this); this.cancel.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); if (source == this.ok) { this.yesButtonSelected = true; } else if (source == this.cancel) { this.yesButtonSelected = false; } // close the dialog this.setVisible(false); } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.