Creating a new window from and action event
Hey I have a problem i would like most of my menu items to create a new window containing a set text and i was thinking of creating a new container with a JTextArea but for some reason its not working. if someone could help me that be great... so my question is how do create another window (TextArea) with my tokenized array info in it open up when Print File or Print Total is the event?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.util.StringTokenizer;
import javax.swing.plaf.*;
class PhoneProjectextends JFrameimplements ActionListener
{
privatestaticfinalint WIDTH = 260;
privatestaticfinalint HEIGHT = 160;
privatestaticfinalint X_ORIGIN = 402;
privatestaticfinalint Y_ORIGIN = 299;
ArrayList internalCalls =new ArrayList();
ArrayList externalCalls =new ArrayList();
PhoneCall internal;
PhoneCall external;
JMenu query =new JMenu("Query");
JMenu proccess =new JMenu("Proccess");
String inRecord;
int numExtension;
int numCallType;
int numSeconds;
int totalIntTime;
int totalExtTime;
publicstaticvoid main(String args[])
{
PhoneProject frame =new PhoneProject();
frame.setVisible(true);
}
publicvoid LoadArray(File myFile)throws IOException
{
FileReader fr =new FileReader(myFile);
BufferedReader br =new BufferedReader(fr);
while ((inRecord = br.readLine()) !=null)
{
StringTokenizer tokenizer =new StringTokenizer(inRecord);
String extension = tokenizer.nextToken();
String callType = tokenizer.nextToken();
String seconds = tokenizer.nextToken();
numExtension = Integer.parseInt(extension);
numCallType = Integer.parseInt(callType);
numSeconds = Integer.parseInt(seconds);
if (numCallType == 0)
{
internal=new PhoneCall(numExtension, numCallType, numSeconds);
totalIntTime = (totalIntTime + numSeconds);
//System.out.println(totalIntTime + "int");
internalCalls.add(internal);
}
if (numCallType == 1)
{
external =new PhoneCall(numExtension, numCallType, numSeconds);
totalExtTime = (totalExtTime + numSeconds);
//System.out.println(totalExtTime + "EXT");
externalCalls.add(external);
}
System.out.println(internal.getSeconds());
}
}
public PhoneProject()
{
Container contentPane;
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT);
setTitle("Phone Analyzer");
setResizable(true);
contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JMenu file =new JMenu("File");
JMenuItem item;
item =new JMenuItem("Open");
item.addActionListener(this);
file.add(item);
item =new JMenuItem("Exit");
item.addActionListener(this);
file.add(item);
proccess.setEnabled(false);
item =new JMenuItem("Print File");
item.addActionListener(this);
proccess.add(item);
item =new JMenuItem("Print Totals");
item.addActionListener(this);
proccess.add(item);
item =new JMenu("Low and High");
item.addActionListener(this);
proccess.add(item);
JMenuItem subItem =new JMenuItem("Compare");
subItem.addActionListener(this);
item.add(subItem);
query.setEnabled(false);
item =new JMenu("Average Total Utilization");
item.addActionListener(this);
query.add(item);
JMenuItem itemInt =new JMenuItem("Internal");
itemInt.addActionListener(this);
item.add(itemInt);
JMenuItem itemExt =new JMenuItem("External");
itemExt.addActionListener(this);
item.add(itemExt);
item =new JMenuItem("Highest Internal Utilization");
item.addActionListener(this);
query.add(item);
item =new JMenuItem("Highest Total Utilization");
item.addActionListener(this);
query.add(item);
JMenuBar menuBar =new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(file);
menuBar.add(proccess);
menuBar.add(query);
contentPane.add(new JTextArea("Phone Report"));
}
publicvoid actionPerformed(ActionEvent event)
{
String menuName;
menuName = event.getActionCommand();
if (menuName =="Open")
{
JFileChooser chooser =new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
File myFile = chooser.getSelectedFile();
this.LoadArray(myFile);
proccess.setEnabled(true);
query.setEnabled(true);
}
catch (Exception e)
{
}
}
}
if (menuName =="Print File")
{
JTextArea display =new JTextArea();
display.setText("Hello");testing to seeif it works
display.setVisible(true);
}
if (menuName =="Print Total")
{
JTextArea display =new JTextArea();
display.setText("Hello");//testing
display.setVisible(true);
}
if (menuName =="Exit")
{
System.exit(0);
}
}
}
Phone.txt
2000 0 300
2000 0 538
2000 1 305
2000 1 729
2005 0 205
2005 0 305
2005 1 592
2005 1 594
2010 0 364
2010 0 464
2010 1 904
2010 1 100
2020 0 234
2020 0 839
2020 1 999
2020 1 210
Assignment: Array Based GUI Assignment
Telephone call data has been collected from a company's telephone switch. You have been asked to analyze it and produce various statistics.
Input File
The input file is a sequential file. It is in no specific order. The file contains an extension number, type of call, and the length of call in seconds. A record is produced each time a call is made from that extension. You should create your own test file.
FieldTypeDescription
ExtensionIntegerExtension number. This is a 4 digit number. Valid Extensions are 2000 to 2020.
TypeIntegerValue of 1 means internal, any other value is an external call.
TimeLongLength of call in seconds
Example:
2000,1,60 : -->>>> Extension 2000 had an internal call that lasted 60 seconds
2000,1,356: -->>>> Extension 2000 had an internal call that lasted 356 seconds
2019,2,65: >>>> Extension 2019 had an external call that lasted 65 seconds
2001,1,355: -->>>> Extension 2001 had an internal call that lasted 355 seconds
Process
1.Use 2 arrays to accumulate the time of calls for each extension, both internal and external.
2.The reports and queries are to be produced from the arrays.
Hints:
Create 2 arrays: one for internal calls and one for external calls.
Load the arrays in Form Load: do not do any calculations here.
The report and queries can totally be produced from the arrays.
Output: Report
Telephone Useage Report
Extension Internal External
200045003500
20011935022981
20022333900
20033144122
Totals9999999999
Output: Queries
On the form add add query capability.
1.Average Total Utilization: Internal Calls: 9999 (total length of all internal calls / number extensions)
2.Average Total Utilization: External Calls: 9999
3.Extension with the highest internal call utilization: Ext xxxx; 9999 seconds.
4.Extension with the highest total utilization.
Form Design
The design of the form is up to you. However, use the following guidelines:
use menus (preferred) or command buttons
use a common dialog box to ask for the file name
use a list box or text box to display the output
the caption on the form should include your name

