Results 1 to 9 of 9
  1. #1
    angelovers07 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Lightbulb i had a problem in adding a value on an array using actionperformed(actionevent)

    my head is really aching since last monday because i cant add a value to an array because it keeps telling me that "incompatible types: java.awt.String to void" :mad:. . . and i had a problem on editing it because i cant even add a single value to it. .. :( :confused:

    can someone help me??

    i kinda using my old gui right now because im still trying to add and edit an array using gui, so sorry for the inconvinience:

    Java Code:
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JScrollPane;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JPasswordField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
    import java.util.*;
    
    
    
    
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    
    
    public class GUI extends JFrame implements ActionListener
    {
    
    
    
    //	JScrollPane scroll;
    
    
    
    //	GUI b = new GUI();
    	String [] a = new String[50];
        //Vector a = new Vector();
        JButton logn, logut, getbuddy, getmes, sendmes, clear1, clear2, clear3;
        JTextField user, pass, suces, inpuid, inpumes, inpuname;
        JTextArea olbuds, newmes;
        JPanel x;
      	TextArea sentmes;  
    public GUI()
        {
            setTitle("My Instant Messenger");
            setSize(1050,500);
            setLocation(500,300);
            //setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(null);
            
          
            
            JLabel title = new JLabel ("Username:");
            title.setSize (70,20);
            title.setLocation(2,2);
              add(title);
              
              JLabel title2 = new JLabel ("Password:");
            title2.setSize(70,20);
            title2.setLocation(2,24);
              add(title2);
              
              JLabel title3 = new JLabel ("Online Buddies:");
              title3.setSize(140,20);
              title3.setLocation(2,48);
              add(title3);
              
              JLabel title4 = new JLabel ("New Messages:");
              title4.setSize (140,20);
              title4.setLocation(217,48);
              add(title4);
              
              JLabel title5 = new JLabel ("Send Message to[UID]:");
              title5.setSize(140,20);
              title5.setLocation (575,48);
              add(title5);
              
              JLabel title6 = new JLabel ("Send Message to[Name]:");
              title6.setSize(180,20);
              title6.setLocation(575,24);
              add(title6);
              
              JLabel title7 = new JLabel ("Sent Messages:");
              title7.setSize(180,20);
              title7.setLocation(575,140);
              add(title7);
              
              user = new JTextField();
              user.setSize(140,20);
              user.setLocation(70,2);
            user.setForeground(Color.white);
            user.setBackground(Color.black);
            add(user);
            
            pass = new JTextField();
            pass.setSize(140,20);
            pass.setLocation(70,24);
            pass.setForeground(Color.white);
            pass.setBackground(Color.black);
            add(pass);
            
            suces = new JTextField();
            suces.setSize (110,20);
            suces.setLocation(313 ,2);
            suces.setForeground(Color.white);
            suces.setBackground(Color.black);
            add(suces);
            
            inpuid = new JTextField();
            inpuid.setSize(184,20);
            inpuid.setLocation(705, 48);
            inpuid.setForeground(Color.white);
            inpuid.setBackground(Color.black);
            add(inpuid);
            
            inpumes = new JTextField();
            inpumes.setSize(415,40);
            inpumes.setLocation(570,72);
            inpumes.setBackground(Color.black);
            inpumes.setForeground(Color.white);
            add(inpumes);
            
            inpuname = new JTextField();
            inpuname.setSize(170,20);
            inpuname.setLocation(720,24);
            inpuname.setBackground(Color.black);
            inpuname.setForeground(Color.white);
            add(inpuname);
                    
            logn = new JButton("Login");
            logn.setSize(100,20);
            logn.setLocation(211,2);
            logn.addActionListener(this);
            logn.setForeground(Color.white);
            logn.setBackground(Color.black);
            add(logn);
            
            getmes = new JButton ("Get Messages");
            getmes.setSize(140, 20);
            getmes.setLocation(320, 375);
            getmes.addActionListener(this);
            getmes.setForeground(Color.white);
            getmes.setBackground(Color.black);
            add(getmes);
            
            sendmes = new JButton ("Send Message");
            sendmes.setSize(140,20);
            sendmes.setLocation(570, 115);
            sendmes.addActionListener(this);
            sendmes.setBackground(Color.black);
            sendmes.setForeground(Color.white);
            add(sendmes);
            
            olbuds = new JTextArea();
            olbuds.setSize(210,300);
            olbuds.setLocation(2 ,72);
            olbuds.setBackground(Color.black);
            olbuds.setForeground(Color.white);
            add(olbuds);
            
            newmes = new JTextArea();
            newmes.setSize(350,300);
            newmes.setLocation(217,72);
            newmes.setBackground(Color.black);
            newmes.setForeground(Color.white);
            add(newmes);
            
            sentmes = new TextArea();
            sentmes.setSize(415,207);
            sentmes.setLocation(570,165);
            sentmes.setBackground(Color.black);
            sentmes.setForeground(Color.white);
            add(sentmes);
           
            
            getbuddy = new JButton ("Display Buddies");
            getbuddy.setSize(140,20);
            getbuddy.setLocation(35, 375);
            getbuddy.addActionListener(this);
            getbuddy.setForeground(Color.white);
            getbuddy.setBackground(Color.black);
            add(getbuddy);
            
            logut = new JButton("Logout");
            logut.setSize(100,20);
            logut.setLocation(211,24);
            logut.addActionListener(this);
            logut.setForeground(Color.white);
            logut.setBackground(Color.black);
            add(logut);
            
            clear1 = new JButton("Clear");
            clear1.setSize(80,43);
            clear1.setLocation(890,24);
            clear1.addActionListener(this);
            clear1.setForeground(Color.white);
            clear1.setBackground(Color.black);
            add(clear1);
            
            clear2 = new JButton("Clear");
            clear2.setSize(100,20);
            clear2.setLocation(713,115);
            clear2.addActionListener(this);
            clear2.setForeground(Color.white);
            clear2.setBackground(Color.black);
            add(clear2);
            
            clear3 = new JButton("Clear Messages");
            clear3.setSize(140,20);
            clear3.setLocation(710, 375);
            clear3.addActionListener(this);
            clear3.setForeground(Color.white);
            clear3.setBackground(Color.black);
            add(clear3);
            
            setVisible(true);
         }
    
    
    
    
    
    
    public svoid action(ActionEvent e)
    {
    	
    	
    	if(e.getSource ()== sendmes)
    	{
    		String value4 = inpumes.getText();
    		 String value5 = inpuname.getText();
    	}
    	
    }
    
    public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == logn)
                {
                    String value = user.getText();
                    String value2 = pass.getText();
    
    		
                    //boolean login = a.login(value,value2);
                    //if(login)
                        suces.setText("Login Successful!");
                            //else
                                //suces.setText("Login Failed!");
                   }
    
    
               if (e.getSource() == getbuddy)
                   {
                      // f.refreshBuddyList(); 
       
                    //boolean buds = f.hasNextBuddy();
                    //if(buds)
                    //while(buds == true)
                      //  {    
                        //FacebookUser u = f.getNextBuddy();
                        //String g = u.getName();
                        //String h = u.getUid();
                        //olbuds.setText(g+" - "+h);
                        //}    
                    //else
                      //  olbuds.setText("No Online Buddies");
                   }
               if (e.getSource() == logut)
                   {
                       //f.logout();
                           suces.setText("Logout Successful!");
                           user.setText("");
                           pass.setText("");
                           inpuid.setText("");
                           inpumes.setText("");
                           inpuname.setText("");
                           olbuds.setText("");
                           newmes.setText("");
                           sentmes.setText("");
                   }    
        //       if (e.getSource() == getmes)
      //             {
                      // boolean mes = f.hasNewMessage();
                       //if (mes)
                       //while (mes == true)
                       //{
                       //FacebookMessage m = f.getNextMessage();
                       //String m2 = m.getFromName();
                       //String m3 = m.getText();
                       //newmes.setText(m2+": " +m3);
                       //}
                       //else 
                         //  newmes.setText("No New Messages");    
    //               }
    
    
    
    
    	if (e.getSource() == sendmes)
                   {
    		
    	
    		
    //		x = b.Array(value4,value5);
    		
          String value4 = inpumes.getText();
    		 String value5 = inpuname.getText();
    		
    		
    		for(int i=0;i<=a.length-1;i++)
    
    		if(a[i]==null)
    {
    	
    	
    	sentmes.append(value5+": "+value4+"\n");
    	
    	//a[i]=sentmes.append(value5+": "+value4+"\n");
    	
    break;	 
    	
    }
    
    
    else
       sentmes.append("memory is full");
    
    
    
    
    		
    
    
    
    
    
    
    
    
    
    
    //if(panel .getSize().height > scroll.getSize().height)
    //{ 
    //	scroll.createVerticalScrollBar(); 
    //} 
    //if(panel.getSize().width>scroll.getSize().width)
    //{ 
    //	scroll.createHorizontalScrollBar(); 
    //}
    
    
    
    
    
    
    
    
    		
    
    		//int index = i;
    
    		//a[index]= sentmes.setText(value5+": "+value4);
    
    		
    
                      
                      // String value3 = inpuid.getText();
                       
                    //f.postMessage(value3, value4);
                    //sentmes.setText(value5+": "+value4);
    
    //sentmes.addElement(inpumes.getText());
                }
    
    
    
    
    
               
            if (e.getSource() == clear1)
                {
                    inpuid.setText("");
                    inpuname.setText("");    
                }    
            if (e.getSource() == clear2)
                {
                    inpumes.setText("");
                }    
            if (e.getSource() == clear3)
                {
                    sentmes.setText("");
                }                        
        }
    
    	
    	public static void main(String[] args)
    	{
    
    	GUI window = new GUI();
    	}
    }
    Moderator edit: code tags added
    Last edited by Fubarable; 02-27-2010 at 12:53 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) I added code tags to your post above to allow the code to be readable. To learn to do this yourself in your next post, please look at my signature below.

    2) When and where does your error occur? What is the exact error message you see. Without the details, we're kind of flying blind here.

    3) Please try to cut down on the volume of code posted. Post just enough code to compile and run and show us the error. Any more just gets in the way of our understanding your problem.

    Much luck!

  3. #3
    angelovers07 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Unhappy sorry my fault. . .

    oh im sorry i'm kinda new to this. . well actually theirs no error i just had a problem on how to add a value to my array[] data which is String [] a = new String[50]; which i set to 50 because i wan't to make a music track library. . .

    the thing is because ActionPerformed is "void" i kinda had error like:
    :mad::mad::mad:
    Java Code:
    C:\Users\Lenovo_G450\Desktop\GUI.java:239: non-static variable sendmes cannot be referenced from a static context
        if(e.getSource ()== sendmes)
                            ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:241: non-static variable inpumes cannot be referenced from a static context
            String value4 = inpumes.getText();
                            ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:242: non-static variable inpuname cannot be referenced from a static context
             String value5 = inpuname.getText();
                             ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:327: incompatible types
    found   : void
    required: java.lang.String
        a[i]=sentmes.append(value5+": "+value4+"\n");
                           ^
    when i do this:

    Java Code:
    public class GUI extends JFrame implements ActionListener
    {
    String [] a = new String[50]; 
    
    . . . . .
    
    public void actionPerformed(ActionEvent e)
    {
    
    if (e.getSource() == sendmes)
    {		
          String value4 = inpumes.getText();
          String value5 = inpuname.getText();		
          for(int i=0;i<=a.length-1;i++)
          if(a[i]==null)
    {
    	
       a[i]=sentmes.append(value5+": "+value4+"\n");
       break;	 
    	
    }
    
    
    else
    
       sentmes.append("memory is full");
    }



    inpumes and inpuname are JTextField and sentmes is JTextArea and sendmes is a JButton. . did you get it? i mean what i am trying to say?

    And even i got this i dont know how to display it: I mean all the values that is already in the array would be sorted by Title or Artist or Album or etc. .:(

    why does programming have to be hard??:confused::(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    a[] is an array of String, and so a[i] is a String variable. sentmes on the other hand is a JTextArea. Calling JTextArea#append(...) returns void. So when you do this:
    Java Code:
    a[i]=sentmes.append(value5+": "+value4+"\n");
    You seem to be trying to put a void into a String variable which simply won't work. You have to place a String and only a String into a[i]. What String? I have no idea since I don't have the time to look at all of your code.

    Best of luck and HTH.

  5. #5
    angelovers07 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Question confious

    Java Code:
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JScrollPane;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JPasswordField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class GUI extends JFrame implements ActionListener
    {
    String [] a = new String[50]; 
     JButton sendmes;
        JTextField inpuid, inpumes, inpuname;
        JTextArea newmes;
        TextArea sentmes;
    
    
    
    public GUI()
        {
    
            setTitle("My Instant Messenger");
            setSize(1050,500);
            setLocation(500,300);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(null);
            
           
              
              JLabel title6 = new JLabel ("Send Message to[Name]:");
              title6.setSize(180,20);
              title6.setLocation(575,24);
              add(title6);
              
              JLabel title7 = new JLabel ("Sent Messages:");
              title7.setSize(180,20);
              title7.setLocation(575,140);
              add(title7);
    
            inpuid = new JTextField();
            inpuid.setSize(184,20);
            inpuid.setLocation(705, 48);
            inpuid.setForeground(Color.white);
            inpuid.setBackground(Color.black);
            add(inpuid);
    
            sendmes = new JButton ("Send Message");
            sendmes.setSize(140,20);
            sendmes.setLocation(570, 115);
            sendmes.addActionListener(this);
            sendmes.setBackground(Color.black);
            sendmes.setForeground(Color.white);
            add(sendmes);
    
            inpumes = new JTextField();
            inpumes.setSize(415,40);
            inpumes.setLocation(570,72);
            inpumes.setBackground(Color.black);
            inpumes.setForeground(Color.white);
            add(inpumes);
            
            inpuname = new JTextField();
            inpuname.setSize(170,20);
            inpuname.setLocation(720,24);
            inpuname.setBackground(Color.black);
            inpuname.setForeground(Color.white);
            add(inpuname);
    
            sentmes = new TextArea();
            sentmes.setSize(415,207);
            sentmes.setLocation(570,165);
            sentmes.setBackground(Color.black);
            sentmes.setForeground(Color.white);
            add(sentmes);
    
    }
    
    public void actionPerformed(ActionEvent e)
    {
    
    if (e.getSource() == sendmes)
    {		
          String value4 = inpumes.getText();
          String value5 = inpuname.getText();		
          for(int i=0;i<=a.length-1;i++)
          if(a[i]==null)
    {
    	
       sentmes.append(value5+": "+value4+"\n");
       break;	 
    }
    
    else
    
       sentmes.append("memory is full");
    
    }
    
    }
    
    public static void main(String[] args)
    	{
    
    	GUI window = new GUI();
    	}
    
    }


    Sorry if i didn't specify it. . . thanks for the information but thats my problem i dont know how to make a String listener, all i know is void ActionPerformed(ActionListener). And i made another

    Java Code:
    public static void action(ActionEvent e)
    {
    		
    if(e.getSource ()== sendmes)
    {
    	String value4 = inpumes.getText();
    	String value5 = inpuname.getText();
    }
    	
    }
    it would only lead to

    Java Code:
    C:\Users\Lenovo_G450\Desktop\GUI.java:237: non-static variable sendmes cannot be referenced from a static context
        if(e.getSource ()== sendmes)
                            ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:239: non-static variable inpumes cannot be referenced from a static context
            String value4 = inpumes.getText();
                            ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:240: non-static variable inpuname cannot be referenced from a static context
             String value5 = inpuname.getText();
                             ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:242: non-static variable a cannot be referenced from a static context
             for(int i=0;i<=a.length-1;i++)
                            ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:244: non-static variable a cannot be referenced from a static context
            if(a[i]==null)
               ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:248: non-static variable a cannot be referenced from a static context
        a[i]=sentmes.setText(value5+": "+value4+"\n");
        ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:248: non-static variable sentmes cannot be referenced from a static context
        a[i]=sentmes.setText(value5+": "+value4+"\n");
             ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:248: incompatible types
    found   : void
    required: java.lang.String
        a[i]=sentmes.setText(value5+": "+value4+"\n");
                            ^
    C:\Users\Lenovo_G450\Desktop\GUI.java:258: non-static variable sentmes cannot be referenced from a static context
       sentmes.append("memory is full");

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java Code:
    public static void action(ActionEvent e)
    {
    		
    if(e.getSource ()== sendmes)
    {
    	String value4 = inpumes.getText();
    	String value5 = inpuname.getText();
    }
    	
    }
    1) Why is that method static? There's no need for static anything here other than the main method.

    2) You still aren't telling us just what it is you are trying to accomplish with this code.

    Much luck.

  7. #7
    angelovers07 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Question

    sorry another error again this was my actual theory code that i was hoping to compile since public void action(ActionEvent e) is only accepting void values and not java.awt.String; values.

    I did this because i thought if i add static to my ActionEvent it would compile the String value i want to store to my array[]. . . Which later i would use to display whenever the value aws already stored.

    Java Code:
    public static void action(ActionEvent e)
    {
    	
    	
    	if(e.getSource ()== sendmes)
    	{
    		String value4 = inpumes.getText();
    		 String value5 = inpuname.getText();
    		 
    		 for(int i=0;i<=a.length-1;i++)
    
    		if(a[i]==null)
    {
    	
    	
    	a[i]=sentmes.setText(value5+": "+value4+"\n");
    	
    	//a[i]=sentmes.append(value5+": "+value4+"\n");
    	
    break;	 
    	
    }
    
    
    else
       sentmes.append("memory is full");
    		 
    	}
    	
    }


    Its like adding a file which is:
    Java Code:
    String value4 = inpumes.getText();
    String value5 = inpuname.getText();
    to my music library which is set to a maximum of 50files ONLY:
    Java Code:
    String a[] = new String[50];

    is this clear enough?:confused:

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by angelovers07 View Post
    sorry another error again this was my actual theory code that i was hoping to compile since public void action(ActionEvent e) is only accepting void values and not java.awt.String; values.
    ???

    I did this because i thought if i add static to my ActionEvent it would compile the String value i want to store to my array[]. . . Which later i would use to display whenever the value aws already stored.
    ???

    Its like adding a file which is:
    Java Code:
    String value4 = inpumes.getText();
    String value5 = inpuname.getText();
    to my music library which is set to a maximum of 50files ONLY:
    Java Code:
    String a[] = new String[50];
    is this clear enough?:confused:
    No, not even close. The only thing clear to me is that static has no business being there. Period.

  9. #9
    angelovers07 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Unhappy

    :confused::confused: what i really trying to say is that can you help me made a method that would add a value to my array[] with the use of the JTextField, TextArea and JButton because this is as far as i can go:
    Java Code:
    //create a JTextField for the inputs(ex. Title of the song, Genre, Artist and etc )
    
     inpumes = new JTextField();
            inpumes.setSize(415,40);
            inpumes.setLocation(570,72);
            inpumes.setBackground(Color.black);
            inpumes.setForeground(Color.white);
            add(inpumes);
            
            inpuname = new JTextField();
            inpuname.setSize(170,20);
            inpuname.setLocation(720,24);
            inpuname.setBackground(Color.black);
            inpuname.setForeground(Color.white);
            add(inpuname);
    
    //create a JButton to implement my method
    
    sendmes = new JButton ("Send Message");
            sendmes.setSize(140,20);
            sendmes.setLocation(570, 115);
            sendmes.addActionListener(this);
            sendmes.setBackground(Color.black);
            sendmes.setForeground(Color.white);
            add(sendmes);
    
    //create a TextArea for my output
    
    sentmes = new TextArea();
            sentmes.setSize(415,207);
            sentmes.setLocation(570,165);
            sentmes.setBackground(Color.black);
            sentmes.setForeground(Color.white);
            add(sentmes);
    
    //and the method itself to add a value to my arrays which has an error
    
    public static void actionPerformed(ActionEvent e)
    {
    
    if (e.getSource() == sendmes)
    {		
          String value4 = inpumes.getText();
          String value5 = inpuname.getText();		
          for(int i=0;i<=a.length-1;i++)
          if(a[i]==null)
    {
    	
       sentmes.append(value5+": "+value4+"\n");
       break;	 
    }
    
    else
    
       sentmes.append("memory is full");
    
    }
    but everytime i do this it shows the value that i was inputting in it but the problem is that can printout the input from my JTextField but it doesnt follow the capacity or limits that i was hoping to implement when it is full.

    so another problem is i dont know how to store a value to my array because my method only do is scanning the array if their are files inside it and then just print it out in the TextArea();

Similar Threads

  1. adding in array String
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 04:28 PM
  2. adding to an array
    By mayhewj7 in forum New To Java
    Replies: 14
    Last Post: 02-19-2009, 05:41 AM
  3. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  4. [SOLVED] Actionevent problem
    By Cymro in forum New To Java
    Replies: 3
    Last Post: 04-04-2008, 07:11 AM
  5. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 06:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •