Author Topic: Basic GUI with swing  (Read 4819 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Basic GUI with swing
« on: February 17, 2006, 03:04:14 PM »
A sample from Savitch's book with some modifications:

Code: [Select]

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()
{
setSize(WIDTH, HEIGHT);
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:  ");
textPanel.add(label1);

txtLines = new JTextField(5);
txtLines.setBackground(Color.WHITE);
textPanel.add(txtLines);

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

txtChars = new JTextField(5);
txtChars.setBackground(Color.WHITE);
textPanel.add(txtChars);

contentPane.add(textPanel);

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

JButton btnContinue = new JButton("Continue");
btnContinue.addActionListener(this);
buttonPanel.add(btnContinue);

contentPane.add(buttonPanel);

}

public static void main(String[] args)
{
myDialog = new Dialog();
myDialog.setVisible(true);
}

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() );

myDialog.setVisible(false);
MemoSaver2 myMemo = new MemoSaver2(lines, chars);
myMemo.setVisible(true);
}


}
}


The WindowDestroyer class

Code: [Select]

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)
  {
  System.exit(0);
  }
 }


Let's discuss this sample here. Please post questions/remarks as reply to this post.
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Basic GUI with swing
« Reply #1 on: February 18, 2006, 12:14:56 AM »
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.
Gillius
Gillius's Programming http://www.gillius.org/

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
remarks are most welcome
« Reply #2 on: February 18, 2006, 06:18:08 AM »
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.
Analyze. Design. Develop. Debug. Deploy. Maintain.