Author Topic: More GUI  (Read 5077 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
More GUI
« on: February 20, 2006, 07:15:33 PM »
Here's the recent demo used in the class lecture.  Let's discuss your questions here.

Code: [Select]

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CloseWindowDemo extends JFrame
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;

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

public CloseWindowDemo()
{
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new InnerDestroyer() );
setTitle("Close Window Demo");
Container content = getContentPane();
content.setLayout( new BorderLayout() );

JLabel message = new JLabel("Click the close button now.");
content.add(message, BorderLayout.CENTER );
}

private class InnerDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
ConfirmWindow askWindow = new ConfirmWindow();
askWindow.setVisible(true);
}
}

private class ConfirmWindow extends JFrame
implements ActionListener
{

public static final int WIDTH = 200;
public static final int HEIGHT = 100;

public ConfirmWindow()
{
setSize(WIDTH, HEIGHT);
setTitle("Confirm exit");

setLocationRelativeTo(null);

Container confirmContent = getContentPane();
confirmContent.setBackground(Color.WHITE);
confirmContent.setLayout( new BorderLayout() );

JLabel msgLabel = new JLabel("Are you sure you wan't to exit?");
confirmContent.add(msgLabel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new FlowLayout() );

JButton exitButton = new JButton("Yes");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);

JButton cancelButton = new JButton("No");
cancelButton.addActionListener(this);
buttonPanel.add(cancelButton);

confirmContent.add(buttonPanel, BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
if (action.equals("Yes") )
System.exit(0);
else if (action.equals("No") )
dispose();
else
System.out.println("Error in confirm window");
}
}

}
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
More GUI
« Reply #1 on: February 21, 2006, 08:51:42 AM »
If this were part of a full-sized program, a refactoring possibility would be to move the class InnerDestroyer to an external public class.  I'd probably call it ConfirmCloseAdapator or something, but the benefit is now you have a public class that if you add it to any frame anywhere, causes that frame to adopt the behavior that it will confirm the close.  Of course for this small example the current code is appropriate.

Also for the ConfirmWindow one could consider using one of the static JOptionPane methods which would do basically all of the work of ConfirmWindow.  The real problem with ConfirmWindow, if I am reading the code properly, is that it is a JFrame, which means that every time I click the close button I will get multiple confirmations, and also even when the confirmation is up I am able to interact with the window I am closing.  Is this really how it acts?  A solution to this problem would be to make the ConfirmWindow a JDialog instead.

Also the issue I mentioned with explicit setSize before still applies.  If the user has a different look'n'feel (because they are in a different OS) that might have a larger font then dialog won't look right, beside the fact that you have to guess what the size of the dialog is supposed to be as you are coding it.  But I think I've already stated this so I won't mention it again ;).
Gillius
Gillius's Programming http://www.gillius.org/

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
more GUI
« Reply #2 on: February 22, 2006, 10:05:43 AM »
We will postpone getting rid of the setSize() for now.  We do all our testing on Window XP machines and the simplicity of the coding is more desired at this time.

We can elimited the confirmWindow class completely and replace it with JoptionPane.showConfirmDialog().  We can modify the innerDestroyer class as follows:

Code: [Select]

private class InnerDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
int value;
value = JOptionPane.showConfirmDialog(null,
"Close this window?", "Please Confirm", JOptionPane.YES_NO_OPTION);
if (value == JOptionPane.YES_OPTION)
System.exit(0);
}
}
Analyze. Design. Develop. Debug. Deploy. Maintain.