-
Java GUI Help
I am working on this project with some friends, and am fairly new at Java, so need some help with this. This is my GUI so far...
Code:
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class CustomerView
{
public static void addComponentsToPane(Container pane)
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel tCarModel = new JLabel("Car Model");
// inserts (top, left, bottom, right)
c.insets = new Insets(10,40,10,7);
c.gridx = 0;
c.gridy = 0;
pane.add(tCarModel, c);
// temporary string of Audi Models to go into combobox
String[] carModels = {"A2", "A3", "A4"};
JComboBox iCarModel = new JComboBox(carModels);
c.insets = new Insets(10,10,10,60);
c.gridx = 1;
c.gridy = 0;
pane.add(iCarModel, c);
JLabel tFirstDayOfRental = new JLabel("First Day of Rental");
c.insets = new Insets(10,10,10,7);
c.gridx = 2;
c.gridy = 0;
pane.add(tFirstDayOfRental, c);
JFormattedTextField iFirstDayOfRental = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy"));
iFirstDayOfRental.setValue(new Date());
c.insets = new Insets(10,10,10,40);
c.gridx = 3;
c.gridy = 0;
pane.add(iFirstDayOfRental, c);
JLabel tCarType = new JLabel("Car Type");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 1;
pane.add(tCarType, c);
// temporary string of Car Types to go into combobox
String[] carTypes = {"Audi"};
JComboBox iCarType = new JComboBox(carTypes);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 1;
pane.add(iCarType, c);
JLabel tNumberOfDays = new JLabel("Number of Days");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 1;
pane.add(tNumberOfDays, c);
JTextField iNumberOfDays = new JTextField(4);
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 1;
pane.add(iNumberOfDays, c);
JLabel tLicence = new JLabel("Licence");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 2;
pane.add(tLicence, c);
String[] licence = {"International", "UK Licence"};
JList iLicence = new JList(licence);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 2;
pane.add(iLicence, c);
JLabel tTimeOfBooking = new JLabel("Time of Booking");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 2;
pane.add(tTimeOfBooking, c);
JFormattedTextField iTimeOfBooking = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy:HH:mm"));
iTimeOfBooking.setValue(new Date());
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 2;
pane.add(iTimeOfBooking, c);
JButton iOK = new JButton("OK");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 3;
pane.add(iOK, c);
JButton iCancel = new JButton("Cancel");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 3;
pane.add(iCancel, c);
}
private static void createAndShowGUI()
{
// create and set up the window
JFrame tJFrame = new JFrame("Car Rental Information Systems");
tJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up the content pane
addComponentsToPane(tJFrame.getContentPane());
// display the window
tJFrame.pack();
tJFrame.setVisible(true);
}
public static void main(String[] Args)
{
// schedule a job for the event-dispatching thread
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
I am trying to make the Cancel button close the frame... don't have a clue how to do it though. Any suggestions please?
-
add an actionlistener to the cancel button
example
cancel.addActionListener(new btnCancel());
then create a class called btnCancel, which has a method called actionPerfomed
example
class btnCancel implements ActionListener
{
public void actionPerfomed(ActionEvent e)
{
System.exit(0); //This will kill ur app
frame.dispose(); // this will dispose of ur frame, but u need to declare the frame in ur main class and not in the method.
}
}
we have a few examples at www.java.codeyourself.com, which you can download and compile, you may find some thing usefull. But plz show ur support by registering. If you need any more help let me know, or any one else here
-
Hi major,
I am really new to Java and have only got this far using examples I have seen in books and online.
I have tried doing what you said from what I understand, and this is what I have:
Code:
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class CustomerView
{
public static void addComponentsToPane(Container pane)
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel tCarModel = new JLabel("Car Model");
// inserts (top, left, bottom, right)
c.insets = new Insets(10,40,10,7);
c.gridx = 0;
c.gridy = 0;
pane.add(tCarModel, c);
// temporary string of Audi Models to go into combobox
String[] carModels = {"A2", "A3", "A4"};
JComboBox iCarModel = new JComboBox(carModels);
c.insets = new Insets(10,10,10,60);
c.gridx = 1;
c.gridy = 0;
pane.add(iCarModel, c);
JLabel tFirstDayOfRental = new JLabel("First Day of Rental");
c.insets = new Insets(10,10,10,7);
c.gridx = 2;
c.gridy = 0;
pane.add(tFirstDayOfRental, c);
JFormattedTextField iFirstDayOfRental = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy"));
iFirstDayOfRental.setValue(new Date());
c.insets = new Insets(10,10,10,40);
c.gridx = 3;
c.gridy = 0;
pane.add(iFirstDayOfRental, c);
JLabel tCarType = new JLabel("Car Type");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 1;
pane.add(tCarType, c);
// temporary string of Car Types to go into combobox
String[] carTypes = {"Audi"};
JComboBox iCarType = new JComboBox(carTypes);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 1;
pane.add(iCarType, c);
JLabel tNumberOfDays = new JLabel("Number of Days");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 1;
pane.add(tNumberOfDays, c);
JTextField iNumberOfDays = new JTextField(4);
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 1;
pane.add(iNumberOfDays, c);
JLabel tLicence = new JLabel("Licence");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 2;
pane.add(tLicence, c);
String[] licence = {"International", "UK Licence"};
JList iLicence = new JList(licence);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 2;
pane.add(iLicence, c);
JLabel tTimeOfBooking = new JLabel("Time of Booking");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 2;
pane.add(tTimeOfBooking, c);
JFormattedTextField iTimeOfBooking = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy:HH:mm"));
iTimeOfBooking.setValue(new Date());
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 2;
pane.add(iTimeOfBooking, c);
JButton iOK = new JButton("OK");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 3;
pane.add(iOK, c);
JButton iCancel = new JButton("Cancel");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 3;
iCancel.addActionListener(new btnCancel());
pane.add(iCancel, c);
}
class btnCancel implements ActionListener
{
public void actionPerfomed(ActionEvent e)
{
// kill application
System.exit(0);
// dispose frame
tJFrame.dispose();
}
}
private static void createAndShowGUI()
{
// create and set up the window
JFrame tJFrame = new JFrame("Car Rental Information Systems");
tJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up the content pane
addComponentsToPane(tJFrame.getContentPane());
// display the window
tJFrame.pack();
tJFrame.setVisible(true);
}
public static void main(String[] Args)
{
// schedule a job for the event-dispatching thread
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
Of course, I am getting errors when compiling
-
Hi,
You should consider changing your code to Object Oriented code instead on using static methods. Also, I notice that your variable are all declared in methods which means their scope only applies within those methods. Perhaps this might work: (i cannot compile code as i am in work)
Code:
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
// use inheritance to create your custom JFrame
public class CustomerView extends JFrame
{
// by declaring the variables here, their scope will apply to all methods in the class
// 'final' keyword means values cannot be changed (suitable for values in JComboBox etc.)
Container pane;
private JLabel tCarModel;
// temporary string of Audi Models to go into combobox
private final String[] carModels = {"A2", "A3", "A4"};
private JComboBox iCarModel;
private JLabel tFirstDayOfRental;
private JFormattedTextField iFirstDayOfRental;
private JLabel tCarType;
// temporary string of Car Types to go into combobox
private final String[] carTypes = {"Audi"};
private JComboBox iCarType;
private JLabel tNumberOfDays;
private JTextField iNumberOfDays;
private JLabel tLicence;
private final String[] licence = {"International", "UK Licence"};
private JList iLicence;
private JLabel tTimeOfBooking;
private JFormattedTextField iTimeOfBooking;
private JButton iOK;
private JButton iCancel;
// use a constructor to create the CustorerView object
public CustomerView(String title)
{
// first line must construct the parent class (JFrame)
super(title); // construct the JFrame
pane = getContentPane(); // get contentpane for this JFrame
addComponentsToPane(); // add GUI componentsc to contentpane
}
public void addComponentsToPane()
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
tCarModel = new JLabel("Car Model");
// inserts (top, left, bottom, right)
c.insets = new Insets(10,40,10,7);
c.gridx = 0;
c.gridy = 0;
pane.add(tCarModel, c);
iCarModel = new JComboBox(carModels);
c.insets = new Insets(10,10,10,60);
c.gridx = 1;
c.gridy = 0;
pane.add(iCarModel, c);
tFirstDayOfRental = new JLabel("First Day of Rental");
c.insets = new Insets(10,10,10,7);
c.gridx = 2;
c.gridy = 0;
pane.add(tFirstDayOfRental, c);
iFirstDayOfRental = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy"));
iFirstDayOfRental.setValue(new Date());
c.insets = new Insets(10,10,10,40);
c.gridx = 3;
c.gridy = 0;
pane.add(iFirstDayOfRental, c);
tCarType = new JLabel("Car Type");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 1;
pane.add(tCarType, c);
iCarType = new JComboBox(carTypes);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 1;
pane.add(iCarType, c);
tNumberOfDays = new JLabel("Number of Days");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 1;
pane.add(tNumberOfDays, c);
iNumberOfDays = new JTextField(4);
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 1;
pane.add(iNumberOfDays, c);
tLicence = new JLabel("Licence");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 2;
pane.add(tLicence, c);
iLicence = new JList(licence);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 2;
pane.add(iLicence, c);
tTimeOfBooking = new JLabel("Time of Booking");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 2;
pane.add(tTimeOfBooking, c);
iTimeOfBooking = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy:HH:mm"));
iTimeOfBooking.setValue(new Date());
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 2;
pane.add(iTimeOfBooking, c);
iOK = new JButton("OK");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 3;
pane.add(iOK, c);
iCancel = new JButton("Cancel");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 3;
iCancel.addActionListener(new btnCancel());
pane.add(iCancel, c);
}
class btnCancel implements ActionListener
{
public void actionPerfomed(ActionEvent e)
{
// dispose frame, 'this' keyword refers to this object(CustomerView)
this.dispose(); // if exit(0) is called first, this can never be called
// kill application
System.exit(0);
}
}
private static void createAndShowGUI()
{
// create and set up the window
CustomerView cv = new CustomerView("Car Rental Information Systems");
// CustomerView 'extends' all functionality of JFrame so we can call any
// JFrame method on it.
cv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up the content pane
// this is best called in the constructor
//addComponentsToPane(tJFrame.getContentPane());
// display the window
cv.pack();
cv.setVisible(true);
}
public static void main(String[] Args)
{
// schedule a job for the event-dispatching thread
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
I have added comments in the code, I hope they explain the changes I have made. If not, post any queries you have and Ill answer them for you.
Kind regards,
Noel
-
I get the following 2 errors when trying to compile:
www.hirenshah.co.uk/errors.PNG
-
Personally I prefer putting the action listening code in the same class so I have modified it to do this (this is just my preferred coding approach). There was a typo in the declaration of actionPerformed, it read actionPerfomed ('r' missing). Here is the code with my adjustments:
Code:
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
// use inheritance to create your custom JFrame
public class CustomerView extends JFrame implements ActionListener
{
// by declaring the variables here, their scope will apply to all methods in the class
// 'final' keyword means values cannot be changed (suitable for values in JComboBox etc.)
Container pane;
private JLabel tCarModel;
// temporary string of Audi Models to go into combobox
private final String[] carModels = {"A2", "A3", "A4"};
private JComboBox iCarModel;
private JLabel tFirstDayOfRental;
private JFormattedTextField iFirstDayOfRental;
private JLabel tCarType;
// temporary string of Car Types to go into combobox
private final String[] carTypes = {"Audi"};
private JComboBox iCarType;
private JLabel tNumberOfDays;
private JTextField iNumberOfDays;
private JLabel tLicence;
private final String[] licence = {"International", "UK Licence"};
private JList iLicence;
private JLabel tTimeOfBooking;
private JFormattedTextField iTimeOfBooking;
private JButton iOK;
private JButton iCancel;
// use a constructor to create the CustorerView object
public CustomerView(String title)
{
// first line must construct the parent class (JFrame)
super(title); // construct the JFrame
pane = getContentPane(); // get contentpane for this JFrame
addComponentsToPane(); // add GUI componentsc to contentpane
}
public void addComponentsToPane()
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
tCarModel = new JLabel("Car Model");
// inserts (top, left, bottom, right)
c.insets = new Insets(10,40,10,7);
c.gridx = 0;
c.gridy = 0;
pane.add(tCarModel, c);
iCarModel = new JComboBox(carModels);
c.insets = new Insets(10,10,10,60);
c.gridx = 1;
c.gridy = 0;
pane.add(iCarModel, c);
tFirstDayOfRental = new JLabel("First Day of Rental");
c.insets = new Insets(10,10,10,7);
c.gridx = 2;
c.gridy = 0;
pane.add(tFirstDayOfRental, c);
iFirstDayOfRental = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy"));
iFirstDayOfRental.setValue(new Date());
c.insets = new Insets(10,10,10,40);
c.gridx = 3;
c.gridy = 0;
pane.add(iFirstDayOfRental, c);
tCarType = new JLabel("Car Type");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 1;
pane.add(tCarType, c);
iCarType = new JComboBox(carTypes);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 1;
pane.add(iCarType, c);
tNumberOfDays = new JLabel("Number of Days");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 1;
pane.add(tNumberOfDays, c);
iNumberOfDays = new JTextField(4);
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 1;
pane.add(iNumberOfDays, c);
tLicence = new JLabel("Licence");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 2;
pane.add(tLicence, c);
iLicence = new JList(licence);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 2;
pane.add(iLicence, c);
tTimeOfBooking = new JLabel("Time of Booking");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 2;
pane.add(tTimeOfBooking, c);
iTimeOfBooking = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy:HH:mm"));
iTimeOfBooking.setValue(new Date());
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 2;
pane.add(iTimeOfBooking, c);
iOK = new JButton("OK");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 3;
pane.add(iOK, c);
iCancel = new JButton("Cancel");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 3;
iCancel.addActionListener(this);
pane.add(iCancel, c);
}
public void actionPerformed(ActionEvent e)
{
// dispose frame, 'this' keyword refers to this object(CustomerView)
this.dispose(); // if exit(0) is called first, this can never be called
// kill application
System.exit(0);
}
private static void createAndShowGUI()
{
// create and set up the window
CustomerView cv = new CustomerView("Car Rental Information Systems");
// CustomerView 'extends' all functionality of JFrame so we can call any
// JFrame method on it.
cv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up the content pane
// this is best called in the constructor
//addComponentsToPane(tJFrame.getContentPane());
// display the window
cv.pack();
cv.setVisible(true);
}
public static void main(String[] Args)
{
// schedule a job for the event-dispatching thread
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
Hope this gets you started!
Kind regards,
Noel
P.S. A great place to start learning Java is at the SUN Java tutorial:
http://java.sun.com/docs/books/tutorial/index.html
-
noelob, that code works great 
Just another quick question... what happens now, when I want to also have an event for the OK button?
-
You may also be able to help me with validation... for the following:
Licence
First Day of Rental
Number of Days
Time of Booking
I want to set up validation where if an incorrect value is entered, a message box pops up saying so...
I have already set the input type for the First Day of Rental and Time of Booking, which changes the input as soon as the user leaves the box, but I want the user to know the input type it has to be (by a message box)... also I need to make sure that the user MUST select the Licence and that the Number of Days is less than a 4 digit number.
I hope what Ive said makes sense, and any help guys is MUCH appreciated!
Last edited by hshah; 02-08-2006 at 02:57 PM.
-
Hi this is refering to your question regarding adding an action listener for the ok button. You would simple add an actionlistener to the okay button
example
iOK.addActionListener(this);
then in your action performed method which is this,
Code:
public void actionPerformed(ActionEvent e)
{
// dispose frame, 'this' keyword refers to this object(CustomerView)
this.dispose(); // if exit(0) is called first, this can never be called
// kill application
System.exit(0);
}
Above you need to have an if statment to check for, which button was clicked.
so you should chnage this method to the following.
Code:
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==iCancel)
{
object(CustomerView)
this.dispose();
System.exit(0);
}
else
{
//do your iOK process here
}
}
As you can see, this way of doing it means that you need if statments to get the source, as in what ever button was clicked. If you have more then a few buttons, I would personaly say that it would be better to create a class for each button. so instead of having
iOK.addActionListener(this);
you could create a new class rather than use the 'this' such as
iOK.addActionListener(new btniOK());
then you create a class which implements the actionlistener like this
Code:
class btniOK implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//do your iOK process here.
}
}
I hope that helps. As for your second question can you explain a little more. I dont quite understand.
-
I am doing pretty well with what I have to do at the moment... managed to adapt what I have learnt with other GUIs I am working on for this project.
Code:
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
// use inheritance to create custom JFrame
public class CustomerView extends JFrame implements ActionListener
{
Container pane;
private JLabel tCarModel;
// temporary string of Audi Models to go into combobox
private final String[] carModels = {"Audi"};
private JComboBox iCarModel;
private JLabel tFirstDayOfRental;
private JFormattedTextField iFirstDayOfRental;
private JLabel tCarType;
// temporary string of Car Types to go into combobox
private final String[] carTypes = {"A2", "A3", "A4"};
private JComboBox iCarType;
private JLabel tNumberOfDays;
private JTextField iNumberOfDays;
private JLabel tLicence;
private final String[] licence = {"International", "UK Licence"};
private JList iLicence;
private JLabel tTimeOfBooking;
private JFormattedTextField iTimeOfBooking;
private JButton iOK;
private JButton iCancel;
// use a constructor to create the CustomerView object
public CustomerView(String title)
{
// construct the JFrame
super(title);
// get content pane for this JFrame
pane = getContentPane();
// add GUI components to content pane
addComponentsToPane();
}
public void addComponentsToPane()
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
tCarModel = new JLabel("Car Model");
// inserts (top, left, bottom, right)
c.insets = new Insets(10,40,10,7);
c.gridx = 0;
c.gridy = 0;
pane.add(tCarModel, c);
iCarModel = new JComboBox(carModels);
c.insets = new Insets(10,10,10,60);
c.gridx = 1;
c.gridy = 0;
pane.add(iCarModel, c);
tFirstDayOfRental = new JLabel("First Day of Rental");
c.insets = new Insets(10,10,10,7);
c.gridx = 2;
c.gridy = 0;
pane.add(tFirstDayOfRental, c);
iFirstDayOfRental = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy"));
iFirstDayOfRental.setValue(new Date());
c.insets = new Insets(10,10,10,40);
c.gridx = 3;
c.gridy = 0;
pane.add(iFirstDayOfRental, c);
tCarType = new JLabel("Car Type");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 1;
pane.add(tCarType, c);
iCarType = new JComboBox(carTypes);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 1;
pane.add(iCarType, c);
tNumberOfDays = new JLabel("Number of Days");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 1;
pane.add(tNumberOfDays, c);
iNumberOfDays = new JTextField(4);
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 1;
pane.add(iNumberOfDays, c);
tLicence = new JLabel("Licence");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 2;
pane.add(tLicence, c);
iLicence = new JList(licence);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 2;
pane.add(iLicence, c);
tTimeOfBooking = new JLabel("Time of Booking");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 2;
pane.add(tTimeOfBooking, c);
iTimeOfBooking = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy:HH:mm"));
iTimeOfBooking.setValue(new Date());
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 2;
pane.add(iTimeOfBooking, c);
iOK = new JButton("OK");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.ipady = 15;
c.gridx = 0;
c.gridy = 3;
pane.add(iOK, c);
iCancel = new JButton("Cancel");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.ipady = 15;
c.gridx = 2;
c.gridy = 3;
iCancel.addActionListener(this);
pane.add(iCancel, c);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == iCancel)
{
// dispose frame
int result = JOptionPane.showConfirmDialog (CustomerView.this, "Are you sure you want to exit?", "Please Confirm", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
dispose();
}
else
{
// Do what the OK button does here
}
}
private static void createAndShowGUI()
{
// create and set up the window
CustomerView cV = new CustomerView("Car Rental Information Systems - Customer View");
cV.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
cV.setResizable(false);
// display the window
cV.pack();
cV.setVisible(true);
}
public static void main(String[] Args)
{
// schedule a job for the event-dispatching thread
// creating and showing this application's GUI
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
I am trying to put the same confirmation message box that is used for the Cancel button, on the X button of the frame, so it asks the user before closing the frame that way. I saw some examples on various websites about watching the window etc, but they always seemed to give me various errors when I tried using that method
What I meant about the validation is that when the user types "abc" into the Number of Days field, I want it to bring up a message saying "Sorry, only numbers allowed in here"... a similar thing for the two date fields, where it brings up messages when the user enters the date in the incorrect format, and forces them to change it... for the Licence I want it so that an option has to be selected... of course this will have to be done when the user presses the OK button. I hope it makes more sense now.
I would just like to say that thank you so much for your help, and that I owe you guys a lot
-
Hi,
Its not possible to add action listeners to the minimise, maximise and close buttons of a GUI window because these buttons are located in native code. You can however add a window listener to the GUI in the same way you added the actionlistener:
Code:
public class CustomerView extends JFrame implements ActionListener, WindowListener
A class can only ever extend one class (JFrame in this case) but it can implement as many interfaces as you want (ActionListener and WindowListener in this case). When you implement an interface, you must override ALL the methods it declares, whether you use them or not. I will leave it up to you as a learning exercise to find out the methods that need to be implemented using the javadocs (a java developers most critical tool)
http://java.sun.com/j2se/1.5.0/docs/api/
or if you get really stuck, try the sun tutorial at
http://java.sun.com/docs/books/tutor...vents/api.html
Learning how to use the javadocs if the most valueable lesson that new developers need to learn, as it is the blueprint for java language.
The validation issues you describe aren't really validation issues they are more like error catching issues. You need to use exception handling to catch such error as you describe. A brief lesson on exception should do the trick at:
http://java.sun.com/docs/books/tutor...ons/index.html
Is short, when the ok button is pressed you will want to check to see if the number in NumberOfDays is actually a number. The following code should do this:
Code:
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == iCancel)
{
......
}
else if(e.getSource() == iOK)
{
String value;
int numDays;
try
{
value = iNumberOfDays.getText();
numDays = Integer.parseInt(value);
}
catch(NumberFormatException nfe)
{
// do whatever needs to be done here when
// value is not a number, e.g.
numDays = -1 // indicate error?
}
System.out.println("The number of days is: "+numDays);
}
}
In the try block, you put the code that you expect will throw an exception. Then you declare a catch block for every exception the code in try can throw. (Looking at the javadocs, the only statement that can throw an exception is Integer.parseInt(). It thrws a NumberFormatException) In the catch block you put the error handling code. If there is no error, code execution resumes from the next statement after the catch block, but nothing in the catch block is executed,
Good Luck!
Kind regards,
Noel
Last edited by noelob; 02-10-2006 at 03:54 AM.
-
add a window listener to your frame like this
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEven e)
{
System.exit(0);
}
);
Hope that helps and works.
-
Its ok major, thanks to Noel's advice I have the windowlistener all working... and YAY the whole GUI part of this group project is coming together 
I just need to get the validation/error catching parts done, and I have sorted more than what I was supposed to do
Similar Threads
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Joe \Nuke Me Xemu\ Foster in forum Architecture and Design
Replies: 0
Last Post: 03-16-2001, 10:02 PM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 AM
-
Replies: 1
Last Post: 04-14-2000, 10:46 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks