public void actionPerformed (ActionEvent e)
{
MenuItem choice = (MenuItem)e.getSource();
if (choice == Display)
display();
Please can someone help we've tried tons of solutions but none seem to work.
We've constructed a primitive GUI with input text such as name,address etc and are now trying to display the results of our file save to the screen. We've tried setText() & getText() but to no avail-just get about two pages of null exception in red on the blueJ terminal!!!!!! What code could you put in to display the results on screen upon pressing the menu item "Display" from the JMenu?
Thanks NIX
4 1738
I'm not sure there is enough information to pinpoint the exact problem....
What does display() do? what are some of the Exceptions that are appearing?
Please use [code] tags when you reply (use a / before the c to close the code tag)
public void actionPerformed (ActionEvent e)
{
MenuItem choice = (MenuItem)e.getSource();
if (choice == Display)
display();
Please can someone help we've tried tons of solutions but none seem to work.
We've constructed a primitive GUI with input text such as name,address etc and are now trying to display the results of our file save to the screen. We've tried setText() & getText() but to no avail-just get about two pages of null exception in red on the blueJ terminal!!!!!! What code could you put in to display the results on screen upon pressing the menu item "Display" from the JMenu?
Thanks NIX
Post the code like DeMan has said. We can't see the NullPointer from that code only.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FormDemo extends CloseableFrame implements ActionListener, ItemListener
{
private FileOutputStream outStream;
private PrintWriter outFile;
public boolean storeOpen;
public boolean fileOpen;
public boolean fileSaved;
Button closeButton, saveButton;
private MenuBar mb = new MenuBar();
private Menu menu1 = new Menu("File");
private Menu menu2 = new Menu("Record");
private MenuItem File,Record,New,Save,ReadIn,Add,Display,Clear;
TextField id,Start,Gender,Address,NatInsceNo,PhoneNo, Firstname,Surname,title,Dob,Age, number, street, city, postCode;
//Store[] myStore;
Employee[] P;
Employee P1;
public FormDemo(String fname) throws IOException
{
outStream = new FileOutputStream(fname);
outFile = new PrintWriter(outStream);
setTitle("Input Form");
setLayout(new BorderLayout());
setSize(545,450);
Panel top = new Panel(new FlowLayout(FlowLayout.CENTER));
top.add(new Label("Applicant Details"));
add("North",top);
Panel mid = new Panel(new FlowLayout(FlowLayout.LEFT));
mid.add(new Label ("id"));
Surname = new TextField("",20);
mid.add(id);
mid.add(new Label ("Start"));
Surname = new TextField("",20);
mid.add(Start);
mid.add(new Label ("Surname"));
Surname = new TextField("",20);
mid.add(Surname);
mid.add(new Label ("Firstname"));
Firstname = new TextField("",4);
mid.add(Firstname);
mid.add(new Label ("Age"));
Age = new TextField("",4);
mid.add(Age);
mid.add(new Label ("Gender"));
Gender = new TextField("",15);
mid.add(Gender);
mid.add(new Label("Dob"));
Dob = new TextField("",8);
mid.add(Dob);
mid.add(new Label ("Address"));
Address = new TextField("",15);
mid.add(Address);
mid.add(new Label ("NatInsceNo"));
NatInsceNo = new TextField("",15);
mid.add(NatInsceNo);
mid.add(new Label ("PhoneNo"));
PhoneNo = new TextField("",15);
mid.add(PhoneNo);
addMenu();
add("Center",mid);
Panel lower = new Panel(new GridLayout (2,3));
closeButton = new Button("close");
saveButton = new Button("save");
saveButton.addActionListener(this);
lower.add(saveButton);
closeButton.addActionListener(this);
lower.add(closeButton);
add("South",lower);
setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == Display)
if (e.getSource() == closeButton)
{
outFile.close();
System.exit(0);
}
if (e.getSource() == saveButton)
{
fileOut();
}
if(e.getSource()==saveButton)
{
Storeit();
}
}
public void Storeit()
{
if(!storeOpen && !fileSaved)
{
P = new Employee[100];
storeOpen = true;
}
}
private void clear()
{
Surname.setText("");
Firstname.setText("");
Age.setText("");
Gender.setText("");
Dob.setText("");
Address.setText("");
NatInsceNo.setText("");
PhoneNo.setText("");
}
private void add()
{
if(storeOpen)
{
P1 =new Employee();
id.getText( );
Dob.getText();
Start.getText();
fileSaved =false;
P[1]=P1;
}
}
public void itemStateChanged (ItemEvent e)
{
}
public void fileOut()
{
outFile.println(number.getText()+" " + street.getText());
outFile.println(city.getText());
outFile.println(postCode.getText());
}
private void addMenu()
{
// creates each menu item with appropriate label
New = new MenuItem("New");
Save = new MenuItem("Save");
ReadIn = new MenuItem("Readin");
Add = new MenuItem("Add");
Display = new MenuItem("Display");
Clear = new MenuItem("Clear");
// adds each item to menu1
menu1.add(New);
menu1.add(Save);
menu1.add(ReadIn);
menu2.add(Add);
menu2.add(Display);
menu2.add(Clear);
//adds menu1 and menu2 to menu bar and adds menu bar to frame
mb.add(menu1);
mb.add (menu2);
setMenuBar(mb);
// sets upp actionlistener for each menu item
New.addActionListener(this);
Save.addActionListener(this);
ReadIn.addActionListener(this);
Add.addActionListener(this);
Display.addActionListener(this);
Clear.addActionListener(this);
}
}
Here is code. The idea is that when you have entered some basic data into the GUI name, address, etc that this displays on the screen. We're confused though. Where does the displayed data actually show?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FormDemo extends CloseableFrame implements ActionListener, ItemListener
{
private FileOutputStream outStream;
private PrintWriter outFile;
public boolean storeOpen;
public boolean fileOpen;
public boolean fileSaved;
Button closeButton, saveButton;
private MenuBar mb = new MenuBar();
private Menu menu1 = new Menu("File");
private Menu menu2 = new Menu("Record");
private MenuItem File,Record,New,Save,ReadIn,Add,Display,Clear;
TextField id,Start,Gender,Address,NatInsceNo,PhoneNo, Firstname,Surname,title,Dob,Age, number, street, city, postCode;
//Store[] myStore;
Employee[] P;
Employee P1;
public FormDemo(String fname) throws IOException
{
outStream = new FileOutputStream(fname);
outFile = new PrintWriter(outStream);
setTitle("Input Form");
setLayout(new BorderLayout());
setSize(545,450);
Panel top = new Panel(new FlowLayout(FlowLayout.CENTER));
top.add(new Label("Applicant Details"));
add("North",top);
Panel mid = new Panel(new FlowLayout(FlowLayout.LEFT));
mid.add(new Label ("id"));
Surname = new TextField("",20);
mid.add(id);
mid.add(new Label ("Start"));
Surname = new TextField("",20);
mid.add(Start);
mid.add(new Label ("Surname"));
Surname = new TextField("",20);
mid.add(Surname);
mid.add(new Label ("Firstname"));
Firstname = new TextField("",4);
mid.add(Firstname);
mid.add(new Label ("Age"));
Age = new TextField("",4);
mid.add(Age);
mid.add(new Label ("Gender"));
Gender = new TextField("",15);
mid.add(Gender);
mid.add(new Label("Dob"));
Dob = new TextField("",8);
mid.add(Dob);
mid.add(new Label ("Address"));
Address = new TextField("",15);
mid.add(Address);
mid.add(new Label ("NatInsceNo"));
NatInsceNo = new TextField("",15);
mid.add(NatInsceNo);
mid.add(new Label ("PhoneNo"));
PhoneNo = new TextField("",15);
mid.add(PhoneNo);
addMenu();
add("Center",mid);
Panel lower = new Panel(new GridLayout (2,3));
closeButton = new Button("close");
saveButton = new Button("save");
saveButton.addActionListener(this);
lower.add(saveButton);
closeButton.addActionListener(this);
lower.add(closeButton);
add("South",lower);
setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == Display)
if (e.getSource() == closeButton)
{
outFile.close();
System.exit(0);
}
if (e.getSource() == saveButton)
{
fileOut();
}
if(e.getSource()==saveButton)
{
Storeit();
}
}
public void Storeit()
{
if(!storeOpen && !fileSaved)
{
P = new Employee[100];
storeOpen = true;
}
}
private void clear()
{
Surname.setText("");
Firstname.setText("");
Age.setText("");
Gender.setText("");
Dob.setText("");
Address.setText("");
NatInsceNo.setText("");
PhoneNo.setText("");
}
private void add()
{
if(storeOpen)
{
P1 =new Employee();
id.getText( );
Dob.getText();
Start.getText();
fileSaved =false;
P[1]=P1;
}
}
public void itemStateChanged (ItemEvent e)
{
}
public void fileOut()
{
outFile.println(number.getText()+" " + street.getText());
outFile.println(city.getText());
outFile.println(postCode.getText());
}
private void addMenu()
{
// creates each menu item with appropriate label
New = new MenuItem("New");
Save = new MenuItem("Save");
ReadIn = new MenuItem("Readin");
Add = new MenuItem("Add");
Display = new MenuItem("Display");
Clear = new MenuItem("Clear");
// adds each item to menu1
menu1.add(New);
menu1.add(Save);
menu1.add(ReadIn);
menu2.add(Add);
menu2.add(Display);
menu2.add(Clear);
//adds menu1 and menu2 to menu bar and adds menu bar to frame
mb.add(menu1);
mb.add (menu2);
setMenuBar(mb);
// sets upp actionlistener for each menu item
New.addActionListener(this);
Save.addActionListener(this);
ReadIn.addActionListener(this);
Add.addActionListener(this);
Display.addActionListener(this);
Clear.addActionListener(this);
}
}
Here is code. The idea is that when you have entered some basic data into the GUI name, address, etc that this displays on the screen. We're confused though. Where does the displayed data actually show?
1.) You forgot the code tags so I wont reply you with code yet.
2.) Consider using swing not awt
3.)You add actionListeners to components before you add them to panels or frames.
4.) Probably this line wont compile -
-
private MenuItem File,Record,New,Save,ReadIn,Add,Display,Clear;
-
5.) The following are code conventions that will make your programming life easier - variable names ()including method names) should begin with a lower case character e.g surname
- Indent your code so that both you and others can read the code better
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Yuk Cheng |
last post by:
<<<start index.htm>>>
<html>
<head>
<script>
function perform(action){
}
</script>
</head>
|
by: Jackson Yap |
last post by:
can someone kind enough to help me look at the attached html and js
file? Why is it that the javascript menu could not work at
www.apchosting.net but could work at...
|
by: tmaster |
last post by:
When I try to dynamically add a second sub menu item to this ContextMenu
item, I get an error 'Specified argument was out of the range of valid
values'.
Private Sub mnuTopics_Show_Select(ByVal...
|
by: Bob Homes |
last post by:
In VB6, I used a system, which I loved, whereby I assigned a "helpId" to each
menu item; that way, you could rest the cursor on the item (without actually
running it) and then press F1 to get...
|
by: gs |
last post by:
I was able to set tooltips on objects other than main menu.
I would like to get the effect of tooltip or microhelp in the bottom status
bar when the mouse is hovering over a submenu item.
How do...
|
by: Kayvine |
last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to...
|
by: Dave |
last post by:
Hello,
I'm working on a new site and i'd like to have a navigation menu on it.
Ideally i'm thinking:
home
first item
second item
etc
obviously with different names. I could use echo to...
|
by: stuckagain |
last post by:
Hi,
I have a page with a scrollable DIV, and within that DIV, I have a few asp.net menu controls. The idea being, the user mouses over the small icons and is presented with a list of options.
...
|
by: dharmbhav |
last post by:
Hello all,
I am trying to develop a roll-over menu effect on a page. It works fine with all other browsers except IE6. Can some one please help me?
HTML:
<div class="menu-item-wrap">...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |