Menu Item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lenin42001
    New Member
    • Jan 2007
    • 29

    #1

    Menu Item

    public void actionPerformed (ActionEvent e)
    {


    MenuItem choice = (MenuItem)e.get Source();
    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
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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)

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by lenin42001
      public void actionPerformed (ActionEvent e)
      {


      MenuItem choice = (MenuItem)e.get Source();
      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.

      Comment

      • lenin42001
        New Member
        • Jan 2007
        • 29

        #4
        import java.awt.*;
        import java.awt.event. *;
        import java.io.*;
        class FormDemo extends CloseableFrame implements ActionListener, ItemListener
        {
        private FileOutputStrea m 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,Ad d,Display,Clear ;
        TextField id,Start,Gender ,Address,NatIns ceNo,PhoneNo, Firstname,Surna me,title,Dob,Ag e, number, street, city, postCode;
        //Store[] myStore;
        Employee[] P;
        Employee P1;

        public FormDemo(String fname) throws IOException
        {
        outStream = new FileOutputStrea m(fname);
        outFile = new PrintWriter(out Stream);
        setTitle("Input Form");
        setLayout(new BorderLayout()) ;
        setSize(545,450 );
        Panel top = new Panel(new FlowLayout(Flow Layout.CENTER)) ;
        top.add(new Label("Applican t Details"));
        add("North",top );

        Panel mid = new Panel(new FlowLayout(Flow Layout.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(Firstna me);
        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(NatInsc eNo);
        mid.add(new Label ("PhoneNo")) ;
        PhoneNo = new TextField("",15 );
        mid.add(PhoneNo );
        addMenu();
        add("Center",mi d);

        Panel lower = new Panel(new GridLayout (2,3));
        closeButton = new Button("close") ;
        saveButton = new Button("save");
        saveButton.addA ctionListener(t his);
        lower.add(saveB utton);
        closeButton.add ActionListener( this);
        lower.add(close Button);
        add("South",low er);
        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.setTe xt("");
        Age.setText("") ;
        Gender.setText( "");
        Dob.setText("") ;
        Address.setText ("");
        NatInsceNo.setT ext("");
        PhoneNo.setText ("");
        }

        private void add()
        {
        if(storeOpen)
        {
        P1 =new Employee();
        id.getText( );
        Dob.getText();
        Start.getText() ;
        fileSaved =false;
        P[1]=P1;
        }
        }
        public void itemStateChange d (ItemEvent e)
        {

        }

        public void fileOut()
        {

        outFile.println (number.getText ()+" " + street.getText( ));
        outFile.println (city.getText() );
        outFile.println (postCode.getTe xt());


        }
        private void addMenu()
        {
        // creates each menu item with appropriate label

        New = new MenuItem("New") ;
        Save = new MenuItem("Save" );
        ReadIn = new MenuItem("Readi n");
        Add = new MenuItem("Add") ;
        Display = new MenuItem("Displ ay");
        Clear = new MenuItem("Clear ");

        // adds each item to menu1

        menu1.add(New);
        menu1.add(Save) ;
        menu1.add(ReadI n);
        menu2.add(Add);
        menu2.add(Displ ay);
        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.addActionLi stener(this);
        Save.addActionL istener(this);
        ReadIn.addActio nListener(this) ;
        Add.addActionLi stener(this);
        Display.addActi onListener(this );
        Clear.addAction Listener(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?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by lenin42001
          import java.awt.*;
          import java.awt.event. *;
          import java.io.*;
          class FormDemo extends CloseableFrame implements ActionListener, ItemListener
          {
          private FileOutputStrea m 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,Ad d,Display,Clear ;
          TextField id,Start,Gender ,Address,NatIns ceNo,PhoneNo, Firstname,Surna me,title,Dob,Ag e, number, street, city, postCode;
          //Store[] myStore;
          Employee[] P;
          Employee P1;

          public FormDemo(String fname) throws IOException
          {
          outStream = new FileOutputStrea m(fname);
          outFile = new PrintWriter(out Stream);
          setTitle("Input Form");
          setLayout(new BorderLayout()) ;
          setSize(545,450 );
          Panel top = new Panel(new FlowLayout(Flow Layout.CENTER)) ;
          top.add(new Label("Applican t Details"));
          add("North",top );

          Panel mid = new Panel(new FlowLayout(Flow Layout.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(Firstna me);
          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(NatInsc eNo);
          mid.add(new Label ("PhoneNo")) ;
          PhoneNo = new TextField("",15 );
          mid.add(PhoneNo );
          addMenu();
          add("Center",mi d);

          Panel lower = new Panel(new GridLayout (2,3));
          closeButton = new Button("close") ;
          saveButton = new Button("save");
          saveButton.addA ctionListener(t his);
          lower.add(saveB utton);
          closeButton.add ActionListener( this);
          lower.add(close Button);
          add("South",low er);
          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.setTe xt("");
          Age.setText("") ;
          Gender.setText( "");
          Dob.setText("") ;
          Address.setText ("");
          NatInsceNo.setT ext("");
          PhoneNo.setText ("");
          }

          private void add()
          {
          if(storeOpen)
          {
          P1 =new Employee();
          id.getText( );
          Dob.getText();
          Start.getText() ;
          fileSaved =false;
          P[1]=P1;
          }
          }
          public void itemStateChange d (ItemEvent e)
          {

          }

          public void fileOut()
          {

          outFile.println (number.getText ()+" " + street.getText( ));
          outFile.println (city.getText() );
          outFile.println (postCode.getTe xt());


          }
          private void addMenu()
          {
          // creates each menu item with appropriate label

          New = new MenuItem("New") ;
          Save = new MenuItem("Save" );
          ReadIn = new MenuItem("Readi n");
          Add = new MenuItem("Add") ;
          Display = new MenuItem("Displ ay");
          Clear = new MenuItem("Clear ");

          // adds each item to menu1

          menu1.add(New);
          menu1.add(Save) ;
          menu1.add(ReadI n);
          menu2.add(Add);
          menu2.add(Displ ay);
          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.addActionLi stener(this);
          Save.addActionL istener(this);
          ReadIn.addActio nListener(this) ;
          Add.addActionLi stener(this);
          Display.addActi onListener(this );
          Clear.addAction Listener(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
          Code:
           
          private [b]MenuItem File[/b],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

          Comment

          Working...