Generating GUI for text based interface?
Hi,
Following on from my previous post, I needa little more help then I
think i should be good to run with it.
I have a program that simulates a call center.
Now I need to add a GUI to it but I don't really know how to use swing.
As far as I can tell a GUI program has 3 main sections, the section
itself that declares the app, adds the buttons, code etc, then the main
section which just calls the app code, then the action handler part.
Here's my code from the non-gui prog:
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class Main
{
public static void main(String[] args) //main section. reads in data
file and loads into vector. then displays menu
{
Calendar Cal = new GregorianCalendar(); //creates a new calendar
Cal.set(Calendar.YEAR, 2008); //sets the calendar year
Cal.set(Calendar.MONTH, Calendar.NOVEMBER); //sets the calendar
month
Cal.set(Calendar.DATE, 02); //sets the calendar day
Cal.set(Calendar.HOUR_OF_DAY, 11); //sets the calendar hour
Cal.set(Calendar.MINUTE, 0); //sets the calendar minute
Cal.set(Calendar.SECOND, 0); //sets the calendar second
Cal.set(Calendar.MILLISECOND, 0); //sets the calendar millisecond
SimpleDateFormat sdf = new SimpleDateFormat(); //sets the date
format to be read back
SimpleDateFormat sdf2 = new SimpleDateFormat(); //sets the date
format for call waiting
sdf2.applyPattern("mm:ss"); //sets the pattern for call
waiting date format
//variables
char Response; //variable for menu select
String str; //used for reading in the data file
String Nam; //used for Name from data file
String Room; //used for room from data file
String About; //used for about from data file
long Calltime; //used to store the program clock stamp
Random rand = new Random(); //random number for call list
Vector waitlist = new Vector(); //creates a new vector
for callers to be randomly selected into
Vector ProgrammingQueue = new Vector(); //creates a new
vector for programming department to be loaded into
Vector DatabaseQueue = new Vector(); //creates a new
vector for database department to be loaded into
Vector NetworkingQueue = new Vector(); //creates a new
vector for networking department to be loaded into
Vector GeneralQueue = new Vector(); //creates a new vector
for other departments to be loaded into
Caller NewCall = new Caller(); //creates a temporary
caller so the department can be tested
Caller OldCall = new Caller(); //creates a new caller to
be put back into the waitlist vector
Caller ProgCall = new Caller(); //creates a new caller
for the programming vector
Caller DataCall = new Caller(); //creates a new caller
for the database vector
Caller NetwCall = new Caller(); //creates a new caller
for the networking vector
Caller GeneCall = new Caller(); //creates a new caller
for the general vector
//Main program
try {
BufferedReader in = new BufferedReader(new
FileReader("nam.dat")); //read data file
while ((str = in.readLine()) != null) //read data file, line by
line, until a null value is returned (end of file)
{
Nam=str.substring(0,20); //split first 20 chars to be Nam
Room=str.substring(39,45); //split chars 39-45 to be Room
About=str.substring(49); //Split chars after 49 to be About
Calltime = Cal.getTimeInMillis(); //initialises
calltime to be 0
Caller person = new Caller(Nam, Room, About, Calltime);
//creates new person with the four values
waitlist.addElement(person); //add person with above
values into vector named waitlist
}
in.close();
} catch (IOException e)
{
// Do Nothing
}
do //display menu
{
System.out.println(""); // prints out a blank line.
System.out.println("Welcome User. Todays date is: "
+sdf.format(Cal.getTime())); // +prints out current date and time
System.out.println();
System.out.println("1. Step Program");
System.out.println("2. Print Details of Current Caller");
System.out.println("3. Details of Current Call Queue");
System.out.println("4. End All Calls");
System.out.println("5. Quit");
System.out.println();
System.out.print("Enter choice [1-5]: ");
//get Response
Response = EasyScanner.nextChar(); //captures input from
keyboard
System.out.println();
// process menu options
switch (Response)
{
case '1': double Q = rand.nextDouble(); //returns random double
between 0 and 1
Cal.set(Calendar.MILLISECOND, 60000); //adds 1min to
calendar on every step
//Section to simulate a person calling
if (waitlist.size() > 0) //error handler. checks to see if
the waitlist vector is greater than 0 before trying to move an element
{
if (Q < 0.6) //if the random number is less than 0.5
{
NewCall = (Caller)(waitlist.firstElement()); //create new
caller with the elements from the first caller in waitlist vector
//if caller is from programming department
if (NewCall.getAbout().equals("Programming")) //cant
compare two strings types using ==. Must use the string.equals method.
{
ProgCall = (Caller)(waitlist.firstElement());
//creates a new caller for the programming vector
ProgCall.setCalltime (Cal.getTimeInMillis()); //sets
the call time for new caller in the programming vector
ProgrammingQueue.addElement(ProgCall); //add the new
caller into the ProgrammingQueue vector
waitlist.removeElementAt(0); //remove the first
element from the waitlist vector
System.out.println("New caller from Programming
department added. Caller Name: "+ProgCall.getName()); //displays the
name of the new caller
}
//if caller is from database depertment
else if (NewCall.getAbout().equals("Database")) //cant
compare two strings types using ==. Must use the string.equals method.
{
DataCall = (Caller)(waitlist.firstElement());
//creates a new caller for the database vector
DataCall.setCalltime (Cal.getTimeInMillis()); //sets
the call time for new caller in the database vector
DatabaseQueue.addElement(DataCall); //add the new
caller into the DatabaseQueue vector
waitlist.removeElementAt(0); //remove the first
element from the waitlist vector
System.out.println("New caller from Database
department added. Caller Name: "+DataCall.getName()); //displays the
name of the new caller
}
//if caller is from networking department
else if (NewCall.getAbout().equals("Networking")) //cant
compare two strings types using ==. Must use the string.equals method.
{
NetwCall = (Caller)(waitlist.firstElement());
//creates a new caller for the newtworking vector
NetwCall.setCalltime (Cal.getTimeInMillis()); //sets
the call time for new caller in the networking vector
NetworkingQueue.addElement(NetwCall); //add the new
caller into the NetworkingQueue vector
waitlist.removeElementAt(0); //remove the first
element from the waitlist vector
System.out.println("New caller from Programming
department added. Caller Name: "+NetwCall.getName()); //displays the
name of the new caller
}
//if caller is not from one the 3 main departments
else {
GeneCall = (Caller)(waitlist.firstElement()); //creates
a new caller for the general vector
GeneCall.setCalltime (Cal.getTimeInMillis()); //sets
the call time for new caller in the general vector
GeneralQueue.addElement(GeneCall); //add the new caller
into the GeneralQueue vector
waitlist.removeElementAt(0); //remove the first
element from the waitlist vector
System.out.println("New caller from General Enquiries
added. Caller Name: "+GeneCall.getName()); //displays the name of the
new caller
}
}
System.out.println("Caller Chance: " +Q); //displays the
random number generated
System.out.println("size = " + waitlist.size());
//displays the current length of the queue
}
//section to simulate a call being ended
double R = rand.nextDouble(); //returns random double
between 0 and 1
if (R < 0.3) //if the random number is less than 0.35
{
if (ProgrammingQueue.size() > 0) //error handler. ensures
the vector has an element to move before trying to move it
{
OldCall = (Caller)(ProgrammingQueue.firstElement());
//create new caller with the elements from the first caller in
programming vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
ProgrammingQueue.removeElementAt(0); //remove the first
element from the ProgrammingQueue vector
System.out.println("Call Resolved. Called Removed.
Caller Name: "+OldCall.getName()); //displays the name of the previous
caller
}
if (DatabaseQueue.size() > 0) //error handler. ensures the
vector has an element to move before trying to move it
{
OldCall = (Caller)(DatabaseQueue.firstElement());
//create new caller with the elements from the first caller in database
vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
DatabaseQueue.removeElementAt(0); //remove the first
element from the database vector
System.out.println("Call Resolved. Called Removed.
Caller Name: "+OldCall.getName()); //displays the name of the previous
caller
}
if (NetworkingQueue.size() > 0) //error handler. ensures
the vector has an element to move before trying to move it
{
OldCall = (Caller)(NetworkingQueue.firstElement());
//create new caller with the elements from the first caller in
networking vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
NetworkingQueue.removeElementAt(0); //remove the first
element from the networking vector
System.out.println("Call Resolved. Called Removed.
Caller Name: "+OldCall.getName()); //displays the name of the previous
caller
}
if (GeneralQueue.size() > 0) //error handler. ensures the
vector has an element to move before trying to move it
{
OldCall = (Caller)(GeneralQueue.firstElement());
//create new caller with the elements from the first caller in general
vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
GeneralQueue.removeElementAt(0); //remove the first
element from the general vector
System.out.println("Call Resolved. Called Removed.
Caller Name: "+OldCall.getName()); //displays the name of the previous
caller
}
System.out.println("Call Resolution Chance: " +Q);
//displays the random number generated.
}
break;
case '2': // Display current caller
if (ProgrammingQueue.size() > 0) //error handler. makes sure
there is somebody in the list before attempting to display details
{
System.out.println("Current Caller from Programming Department");
System.out.println("Current Caller : "
+ProgrammingQueue.get(0)); //displays current caller
System.out.println("Current Caller Time Added To The Queue:
" +sdf.format(ProgCall.getCalltime())); //displays when the caller was
added to the queue.
System.out.println("Current Caller Waiting Time: "
+sdf2.format(ProgCall.getCallWaitTime(Cal.getTimeInMillis())));
//displays the time the caller has been waiting in the list
System.out.println("");
}
if (DatabaseQueue.size() > 0) //error handler. makes sure
there is somebody in the list before attempting to display details
{
System.out.println("Current Caller from Database Department");
System.out.println("Current Caller : "
+DatabaseQueue.get(0)); //displays current caller
System.out.println("Current Caller Time Added To The Queue:
" +sdf.format(DataCall.getCalltime())); //displays when the caller was
added to the queue.
System.out.println("Current Caller Waiting Time: "
+sdf2.format(DataCall.getCallWaitTime(Cal.getTimeInMillis())));
//displays the time the caller has been waiting in the list
System.out.println("");
}
if (NetworkingQueue.size() > 0) //error handler. makes sure
there is somebody in the list before attempting to display details
{
System.out.println("Current Caller from Networking Department");
System.out.println("Current Caller : "
+NetworkingQueue.get(0)); //displays current caller
System.out.println("Current Caller Time Added To The Queue:
" +sdf.format(NetwCall.getCalltime())); //displays when the caller was
added to the queue.
System.out.println("Current Caller Waiting Time: "
+sdf2.format(NetwCall.getCallWaitTime(Cal.getTimeInMillis())));
//displays the time the caller has been waiting in the list
System.out.println("");
}
if (GeneralQueue.size() > 0) //error handler. makes sure there
is somebody in the list before attempting to display details
{
System.out.println("Current Caller from Other Departments");
System.out.println("Current Caller : " +GeneralQueue.get(0));
//displays current caller
System.out.println("Current Caller Time Added To The Queue:
" +sdf.format(GeneCall.getCalltime())); //displays when the caller was
added to the queue.
System.out.println("Current Caller Waiting Time: "
+sdf2.format(GeneCall.getCallWaitTime(Cal.getTimeInMillis())));
//displays the time the caller has been waiting in the list
System.out.println("");
}
else System.out.println("Currently, there is nobody in any
other queue");
break;
case '3': // Display current caller queue
if (ProgrammingQueue.size() > 0) //ensures the user will recieve
a message to inform them if nobody is in the queue
{
for (int i = 0; i < ProgrammingQueue.size(); i++) //loops until
the last element in the ProgrammingQueue vector is found
System.out.println("Current Programming Caller Queue: "
+ProgrammingQueue.get(i)); //displays current caller
System.out.println("");
}
if (DatabaseQueue.size() > 0) //ensures the user will recieve
a message to inform them if nobody is in the queue
{
for (int i = 0; i < DatabaseQueue.size(); i++) //loops until the
last element in the DatabaseQueue vector is found
System.out.println("Current Database Caller Queue: "
+DatabaseQueue.get(i)); //displays current caller
System.out.println("");
}
if (NetworkingQueue.size() > 0) //ensures the user will
recieve a message to inform them if nobody is in the queue
{
for (int i = 0; i < NetworkingQueue.size(); i++) //loops until
the last element in the NetworkingQueue vector is found
System.out.println("Current Networking Caller Queue: "
+NetworkingQueue.get(i)); //displays current caller
System.out.println("");
}
if (GeneralQueue.size() > 0) //ensures the user will recieve a
message to inform them if nobody is in the queue
{
for (int i = 0; i < GeneralQueue.size(); i++) //loops until the
last element in the GeneralQueue vector is found
System.out.println("Current General Caller Queue: "
+GeneralQueue.get(i)); //displays current caller
System.out.println("");
}
else System.out.println("There is currently nobody in any of
the other call queues");
case '4': if (ProgrammingQueue.size() > 0) //error handler. ensures
the vector has an element to move before trying to move it
{
OldCall = (Caller)(ProgrammingQueue.firstElement());
//create new caller with the elements from the first caller in
programming vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
ProgrammingQueue.removeElementAt(0); //remove the first
element from the ProgrammingQueue vector
System.out.println("Call Resolved. Called Removed. Caller
Name: "+OldCall.getName()); //displays the name of the previous caller
}
if (DatabaseQueue.size() > 0) //error handler. ensures the
vector has an element to move before trying to move it
{
OldCall = (Caller)(DatabaseQueue.firstElement()); //create
new caller with the elements from the first caller in database vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
DatabaseQueue.removeElementAt(0); //remove the first
element from the database vector
System.out.println("Call Resolved. Called Removed. Caller
Name: "+OldCall.getName()); //displays the name of the previous caller
}
if (NetworkingQueue.size() > 0) //error handler. ensures the
vector has an element to move before trying to move it
{
OldCall = (Caller)(NetworkingQueue.firstElement());
//create new caller with the elements from the first caller in
networking vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
NetworkingQueue.removeElementAt(0); //remove the first
element from the networking vector
System.out.println("Call Resolved. Called Removed. Caller
Name: "+OldCall.getName()); //displays the name of the previous caller
}
if (GeneralQueue.size() > 0) //error handler. ensures the
vector has an element to move before trying to move it
{
OldCall = (Caller)(GeneralQueue.firstElement()); //create
new caller with the elements from the first caller in general vector
waitlist.addElement(OldCall); //add the new caller into
the waitlist vector
GeneralQueue.removeElementAt(0); //remove the first
element from the general vector
System.out.println("Call Resolved. Called Removed. Caller
Name: "+OldCall.getName()); //displays the name of the previous caller
}
else System.out.println("Currently, there is nobody in any
other queue");
break;
case '5': break; // quits program
default: System.out.println("Invalid entry. Please press
1-4."); //error handler
}
}
while (Response != '5');
}
}
Here's what I have so far for the GUI prog:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class CallGui extends JFrame implements ActionListener {
private JLabel label1;
private JButton StartButton = new JButton ("Start");
private JButton ExitButton = new JButton ("Exit");
/**
* Creates a new instance of <code>CallGui</code>.
*/
public CallGui() {
super( "Call Center");
Container container = getContentPane();
container.setLayout( new FlowLayout());
label1 = new JLabel( "Call Center");
label1.setToolTipText(" This is the call center program");
container.add( label1);
add(StartButton);
StartButton.setBorder(new BevelBorder(BevelBorder.RAISED));
StartButton.addActionListener(this);
add(ExitButton);
ExitButton.setBorder(new BevelBorder(BevelBorder.RAISED));
ExitButton.addActionListener(this);
JTextArea ProgrammingtextArea = new JTextArea( "Programmers", 5, 10);
container.add( ProgrammingtextArea);
JTextArea DatabasetextArea = new JTextArea( "Databases", 5, 10);
container.add( DatabasetextArea);
JTextArea NetworkingtextArea = new JTextArea( "Networkers", 5, 10);
container.add( NetworkingtextArea);
JTextArea GeneraltextArea = new JTextArea( "General", 5, 10);
container.add( GeneraltextArea);
JTextArea PotentialtextArea = new JTextArea( "Potential Callers", 5, 10);
container.add( PotentialtextArea);
setSize( 270, 370);
setVisible( true );
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
CallGui application = new CallGui();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == StartButton)
{
}
if (e.getSource() == ExitButton)
{
System.exit(0);
}
//handle button event
}
}
What I can't figure out is where to put what in order to start merging
the two together to achieve my goal. I've been hunting around on the web
and through my books but the answer escapes me.
No matter where I declare my variables etc other parts of the program
wont compile because they can't see them.
Also I know the GUI is horrendous atm, but I need to get the
functionality working before i start to jazz it up.
Thanks in advance.