Hosted Forum: West Visayas State University - Philippines > Object Oriented Programming with Java Class Forum

Basic GUI with swing


A sample from Savitch's book with some modifications:

--- Code: ---
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Dialog extends JFrame implements ActionListener
public static final int WIDTH = 300;
public static final int HEIGHT = 200;

private static JTextField txtLines, txtChars;
private static Dialog myDialog;

public Dialog()
addWindowListener( new WindowDestroyer() );

setTitle("Dialog Demo");
Container contentPane = getContentPane();
contentPane.setLayout( new FlowLayout() );

JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(2,2) );

JLabel label1 = new JLabel("No. of lines:  ");

txtLines = new JTextField(5);

JLabel label2 = new JLabel("No. of characters:  ");

txtChars = new JTextField(5);


JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new FlowLayout() );

JButton btnContinue = new JButton("Continue");



public static void main(String[] args)
myDialog = new Dialog();

public void actionPerformed(ActionEvent e)
//since only one button is involved,
//only one event is defined
String actionCommand = e.getActionCommand();

if (actionCommand.equals("Continue"))
int lines = Integer.parseInt( txtLines.getText() );
int chars = Integer.parseInt( txtChars.getText() );

MemoSaver2 myMemo = new MemoSaver2(lines, chars);


--- End code ---

The WindowDestroyer class

--- Code: ---
import java.awt.*;
import java.awt.event.*;

 *  If you register an object of this class as a listener to any
 * object of the class JFrame, then if the user clicks the
 * close-window button of the JFrame, the object of this class
 * will end the program and close the JFrame.
 public class WindowDestroyer extends WindowAdapter
  public void windowClosing(WindowEvent e)

--- End code ---

Let's discuss this sample here. Please post questions/remarks as reply to this post.

If I am being too pedantic, let me know...

Calling a class Dialog when it is a type of a frame is awkward when dialogs and frames are very different in Swing and there is actually a class java.awt.Dialog.

Instead of the WindowDestroyer class, there exists the method JFrame.setDefaultOperationOnClose( JFrame.EXIT_ON_CLOSE ); that does the same thing.  You can also choose dispose, hide, or no action.

Having a defined WIDTH and HEIGHT is probably not preferrable in a Java environment that is supposed to be DPI-independent.  Maybe a better option is to pack() the frame first?  Perhaps packing doesn't work well for this example -- I didn't run this code :(.

Also as a random tip, Java version 1.4 has setLocationRelativeTo( null ); which places the window in the center of the screen instead of at the upper-left corner.  Java version 1.5 also adds the ability to call setLocationByPlatform( true ); which lets the operating system decide where to put the window, which is likely the best option since the operating system knows about things like multi-monitor displays and such.

BTW: if this is distracting to the real lesson here, I apologize, as what I was discussing is probably not beginner's topics except for the pack and setDefaultCloseOperation.

While it may intimidate the timid and the weak of heart, your remarks are actually very much welcome!

For the absolute beginners, which is majority in this class, some of the details can be a bit too much at this stage.  Still, it is helpful to everyone because it reveals how much more there is to be learned.

Inputs from someone with broad industry experience are always valuable.  The class should strive for better quality of output and avoid the pitfalls you pointed out.


[0] Message Index

Go to full version