Jump to content

JFrame holding variables

- - - - -

  • Please log in to reply
5 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
As you guys know I was learning how to print out different names , in two previous post.

At the moment I'm trying to implement those codes into my JFrame but I get this error...

Quote

run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at buildingbasicpanel.Buildingbasicpanel$Listener.actionPerformed(Buildingbasicpanel.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 9 seconds)

specifically in this area.
  private class Listener implements ActionListener

      {

          public void actionPerformed(ActionEvent e)

         {

       

            while(i<10 || name.equals("-1")==false){ // If -1 is enter print names.

                empty = new String ("");

                name[i] = box.getText();

                if (name[i].equals(empty)==false)

                {

                    helloMsg.setText("Nice to meet you,"+ name[i]);

                    i++;

                }

                else if (name[i].equals(empty)==true){

                helloMsg.setText("Invalid type name again");

                

                }

                else if (i==9){

                for (i = 0; i<10;i++){

                System.out.println(name[i]);

                        

                }

                }

            }

            

         }

       

      }


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You need to take the for loop out of the while loop. They are currently nested and for the problem you are trying to solve, they shouldn't be nested.

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
EDIT:
I got it to work with the same errors, weird it prints the names but if I press -1 it stops.
I really will just ask for help tomorrow.

I got this

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package buildingbasicpanel;


   import javax.swing.*;

   import java.awt.*;

   import java.awt.event.*;




    public class Buildingbasicpanel extends JPanel

   {

      JLabel helloMsg;

      String  empty;

      static int i = 0;

      static String[] name = new String[10];

      JTextField box;

      JButton Enter;

		

       public Buildingbasicpanel(){

         setLayout(new FlowLayout());

         helloMsg = new JLabel("Enter name below, and -1 when you finish");

         add(helloMsg);

           empty = new String("");

           

          

          

           

         

         

         box = new JTextField(20);

         box.setText("Name here");

         box.setHorizontalAlignment(SwingConstants.CENTER);

         add(box);

      

         Enter = new JButton("Enter");

         Enter.addActionListener(new Listener());

         Enter.setLocation(10,50);

         add(Enter);

      	

       

      

      }

       private class Listener implements ActionListener

      {

          public void actionPerformed(ActionEvent e)

         {

             empty =new String("");

                        

             while(i<10 ) 

             {

             name[i] = box.getText();

				 

							 

				 

          	if(name[i].equals(empty)==true){

               helloMsg.setText("Invalid Name");

					}

					else{

					helloMsg.setText("Hello, " + name[i]);

					i++;

					}

				

					

			  }

        

             

            }

      

                 

       }

    

       public static void main(String[] args)

      {

         JFrame frame = new JFrame("Assignment1");

         frame.setSize(250, 150);

         frame.setLocation(200, 100);

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         frame.setContentPane(new Buildingbasicpanel());

         frame.setVisible(true);

         

      }

    

   }


Edited by vaironl, 18 September 2011 - 04:27 PM.
Fixed code


#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
This isn't going to work with 1 for loop and 1 while loop requirements. I really think you're better of using the JOptionPane static methods instead.

With the code you have, the user presses the button and while loop sets every value of name[] to whatever name you entered.
Example: Name="lethalwire"
then every value inside of name[] is set to "lethalwire"

Here's a small example using 2 methods from the JOptionPane class.

	public static void main(String[] args) {

		String temporaryName;

		temporaryName = JOptionPane.showInputDialog("Please give me your name.");

		JOptionPane.showMessageDialog(null, "Hello " + temporaryName);

	}



#5
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

This isn't going to work with 1 for loop and 1 while loop requirements. I really think you're better of using the JOptionPane static methods instead.

With the code you have, the user presses the button and while loop sets every value of name[] to whatever name you entered.
Example: Name="lethalwire"
then every value inside of name[] is set to "lethalwire"

Here's a small example using 2 methods from the JOptionPane class.

	public static void main(String[] args) {

		String temporaryName;

		temporaryName = JOptionPane.showInputDialog("Please give me your name.");

		JOptionPane.showMessageDialog(null, "Hello " + temporaryName);

	}


I think I can work with this one, but some people did got it to work with a
brake;


#6
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

This isn't going to work with 1 for loop and 1 while loop requirements. I really think you're better of using the JOptionPane static methods instead.

With the code you have, the user presses the button and while loop sets every value of name[] to whatever name you entered.
Example: Name="lethalwire"
then every value inside of name[] is set to "lethalwire"

Here's a small example using 2 methods from the JOptionPane class.

	public static void main(String[] args) {

		String temporaryName;

		temporaryName = JOptionPane.showInputDialog("Please give me your name.");

		JOptionPane.showMessageDialog(null, "Hello " + temporaryName);

	}

Thanks for all of the help, I got it to work. The problem was that whenever the button enter was pressed it would reset the variables to 0.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users