Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Help with button to ArrayList

    Hey, I'm new with java and I have been working a while on this assignment and I come up to this question,

    "In the action of the button «Save Post» you will put the text from textfield to al"

    I understand the question but I'm not so sure how to do it, especially when it's about a button.

    If any of you guys would help me I would appreciate it so much!

    Here is my code , I have shortened it so it will be easier to read and just showing the important part

    Java Code:
    	public class Vindu extends JFrame{
    		
    		
        	ArrayList<String> al;
         	public Vindu(){
        	al= new ArrayList<String>();
    
    	final JButton jb = new JButton("Save Post");
    		
    	
    		
    
    			
    			JLabel jlNavn = new JLabel("Navn:");
    			JTextField jtfNavn = new JTextField(20);
    			JLabel jlInnlegg = new JLabel("Innlegg:");
    			JTextField jtaInnlegg = new JTextField();
    			
    		
    			
    			jb.addActionListener(new ActionListener() {
    			  public void actionPerformed(ActionEvent evt) {
    			    System.out.println("Knappen virket");
    			    
    			    String input = jb.getText();
    			    jb.setText(input);
    			  }
    			});
    			
    				
    			
    			
    			
    			//cp.add(jpWest,BorderLayout.WEST);
    			this.setTitle("Blogg program");
    			this.setLocation(150, 150);
    			this.setSize(400, 400);
    			this.setVisible(true);	
    		}
    		
    		public static void main(String[] args) {
    			new Vindu();
    
    		}
    		
    
    	}
    Last edited by Norm; 02-11-2012 at 02:37 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Help with button to ArrayList

    What happens when you compile and execute your code?
    Are any messages printed out?
    how to do it
    What part are you having problems doing?

  3. #3
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Re: Help with button to ArrayList

    If I get the question right (it's in norwegian so I'm translating to english) I'm gonna put the text from text field to the instance variable al,
    and it should be in the action button "Save Post".

    The question after this one is "Change the action in the button "Save Post" so that all entries in the array list will be posted in the text area"
    This meaning I will put ArrayList in the button? and if so how?

    I have tried a couple of stuff but I just delated it because I found out it was totally wrong, so now I'm back on nothing again.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Help with button to ArrayList

    I'm gonna put the text from text field to the instance variable al,
    I'm not sure where your problem is.
    What is printed on the console when you execute your program?

    Do you know how to get the data from a text field?
    Do you know how to add a String to an ArrayList?

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Re: Help with button to ArrayList

    When I run it on eclipse it prints "Knappen virket". Which is fine for now, but I'm suppose to put the data from a text field to al

    No I'm not sure how to do it so it get the data from a text field
    I thought I did add a String to an ArrayList?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Help with button to ArrayList

    get the data from a text field
    Read the API doc for the text field class. It has methods that will get the data from the text field.

  7. #7
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Re: Help with button to ArrayList

    The API doc where do I find that? It says how to put text field to an ArrayList?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Help with button to ArrayList

    The API doc where do I find that?
    Java Platform SE 6

    how to put text field to an ArrayList?
    You can put any object into an ArrayList.
    Are you sure you want to put the text field object into an array list?
    Of do you want to get the String contents of the text field and put that into the array list?

  9. #9
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Re: Help with button to ArrayList

    Thank you for your link.

    Hm actually reading the rest of the assignments, I think that I'm suppose to do is get the String contents of the text field and put that into an array list.
    Because the main goal with the assignment is that when I press "Save Post" button it will print out an overview of all the posts with posts, name and date.

    And to get that information through a button I need to that option right?

    The next thing I will do after this one is that I will change the action on "Save Post" button so all the posts in the ArrayList will be laid out in the text area.

    Does this make any sense to you?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Help with button to ArrayList

    Does this make any sense to you?
    There are a lot of different ways a program can work. You need to read the assignment and figure out what it is required and then write the code.

  11. #11
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    14
    Rep Power
    0

    Default Re: Help with button to ArrayList

    I figured out how to do it, thank you for your help :)

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Help with button to ArrayList

    Glad you got it working.

Similar Threads

  1. Replies: 7
    Last Post: 08-06-2011, 03:39 AM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 01:16 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

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
  •