Click to See Complete Forum and Search --> : Swing Question


sivad
December 8th, 2005, 12:44 PM
Ive got a GUI, I have copied the code in below. My Question is what do I need to put in my Submit Handler to close out the window if the data entered is valid. I've already got the valididity code enetered in just need to find a way to close the window, which will then launch the next GUI in line.

[code]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class PlaneGUI
{
int fn;
int pl;
int ps;
JPanel panel;
JLabel flightNumber, planePayload, seats;
JTextField flightNumberField, planePayloadField, seatsField;
JButton clear, exit, submit;
Container pane;
SubmitButtonHandler submitHandler;

public PlaneGUI()
{
}

public Component createContents()
{
flightNumber = new JLabel("Enter the flight number:",SwingConstants.RIGHT);
planePayload = new JLabel("Enter the total weight capacity:", SwingConstants.RIGHT);
seats = new JLabel("Enter the total number of available seats:", SwingConstants.RIGHT);

flightNumberField = new JTextField(5);
planePayloadField = new JTextField(5);
seatsField = new JTextField(5);

clear = new JButton("Clear");
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent c)
{
flightNumberField.setText(" ");
planePayloadField.setText(" ");
seatsField.setText(" ");
flightNumberField.requestFocus();
}
});

exit = new JButton("Exit");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});

submit = new JButton("Submit");
submitHandler = new SubmitButtonHandler();
submit.addActionListener(submitHandler);


panel = new JPanel(new GridLayout(0,2));

panel.add(flightNumber);
panel.add(flightNumberField);
panel.add(planePayload);
panel.add(planePayloadField);
panel.add(seats);
panel.add(seatsField);
panel.add(clear);
panel.add(submit);
panel.add(exit);


return panel;
}
private class SubmitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent s)
{

try
{
fn = Integer.parseInt(flightNumberField.getText());
pl = Integer.parseInt(planePayloadField.getText());
ps = Integer.parseInt(seatsField.getText());
}

catch (NumberFormatException er)
{
JOptionPane.showMessageDialog(null,
"All fields must be possitive numbers!","Error",
JOptionPane.ERROR_MESSAGE);

if (fn < 0)
{
flightNumberField.setText("");
flightNumberField.requestFocus();
}

else if (pl <= 0)
{
planePayloadField.setText("");
planePayloadField.requestFocus();
}

else if (ps <=0)
{
seatsField.setText("");
seatsField.requestFocus();
}
}

}

}
[\code]

sivad
December 9th, 2005, 05:40 PM
I've got it figured out. In the submit handler I have a boolean set to true if the information is valid. I use that boolean to set the frame to either visible or invisable.

cupofjoe
March 23rd, 2006, 10:53 AM
You should look at using the javax.swing
Class JFormattedTextField

Less typing and work once you learning how to use it.

Cheers,
CupOfJoe

PS
Some systems have a way to escape the dialog by hitting X so they don't hit YES, NO, OK or the like. Be careful with those. ;-) I got bit once and it still hurts thinking about it.