Results 1 to 13 of 13
  1. #1
    vermicelli is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default problem with GUI

    Dear people,

    I have a problem with a certain problem in Java.
    A GUI is given and the assignment is as follows.
    A user types an appointment, f.e. with the dentist and write:
    ""dentist 10.00" in a textfield. The program must save the information in an array. But how can I put save the appointmens in the array? Here are the pictures:

    Now is my Java code:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class AgendaFrame extends JFrame implements ActionListener{
    
    	private JButton addButton= new JButton("VoegToe: ");
    	private JTextField textfield= new JTextField();
    	private JButton searchButton= new JButton("Afspraaknummer: ");
       	private JPanel panel = new JPanel(new GridLayout(2,2));
    
       	private String[] appointments= new String[10];
       	private int amount= 0;
    
    	public AgendaFrame(){
    
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100,100,350,100);
    		Container c = getContentPane();
    		panel.add(addButton);
    		panel.add(textfield);
    		panel.add(searchButton);
    		c.add(panel);
    		setVisible(true);
    	}
    	
    		
    	public void actionPerformed(ActionEvent e)
    	{
    	        if(e.getSource() == addButton)
    		{
    			String n = textfield.getText();
    			if(amount< appointments.length)
    			{
    				appointments[amount]= n;
    				amount++;
    				textfield.setText("");
    			}
    		}
    			
    	}
    			
    	public static void main(String[] args){
    		new AgendaFrame();
    	}
    }
    Last edited by vermicelli; 07-25-2010 at 08:49 AM.

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Does it just not work?

    What is aantal? You don't define it anywhere?
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Your code does not compile. Please correct the compile errors and respost the code

  4. #4
    vermicelli is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    First of all, thanks for your response.
    My bad, I have changed aantal into amount.
    Well, I have made an array called "appointments". And when someone types his appointment and the time in the textfield, and click on "VoegToe"(it means to add in Dutch), then the information should be saved into the array, and the textfield is supposed to be empty again. But as you see, when I run the program the appointment is not being saved into the array appointments, and the textfield wont go blank. So I wonder what have I done incorrect?
    Last edited by vermicelli; 07-24-2010 at 04:09 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vermicelli View Post
    But as you see, when I run the program the appointment is not being saved into the array appointments, and the textfield wont go blank. So I wonder what have I done incorrect?
    You haven't added an ActionListener to your addButton so you can click all you want but nothing is going to happen. Implementing an actionPerformed( ... ) method is not enough. Your frame should implement the ActionListener interface and it should be added as such to the button.

    kind regards,

    Jos

    ps. there are more errors in the code you've shown us ...

  6. #6
    vermicelli is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    ah! I've totally forgotten it! Thanks, that is a (big) step forwards :) But still have to figure out the other requirements

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vermicelli View Post
    ah! I've totally forgotten it! Thanks, that is a (big) step forwards :) But still have to figure out the other requirements
    Well, for one thing, your for-loop in your actionPerformed( ... ) method doesn't make any sense.

    kind regards,

    Jos

  8. #8
    vermicelli is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    why not? Because what I mean is that the the information(text.getText) must be saved into position i of the array appointments, as far as the number of appointments are smaller then the arraylength. So how can I save the information (text.getText) into the array?
    Last edited by vermicelli; 07-24-2010 at 10:45 PM.

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

    Default

    Quote Originally Posted by vermicelli View Post
    why not?
    There are many problems with it, perhaps too many to enumerate, but I can try. What is amount.length for one? And why would you want to take one unchanging String, text, and shove it into the elements of an array repeatedly with each button push, over-writing all array elements placed previously? Again, it just doesn't make any sense.

  10. #10
    vermicelli is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    ok, I have changed the ActionPerformed code, now it still wont work. Is it possible to set the textfield on blank by the way? Because it just doesnt want to be empty after putting elements in the array

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vermicelli View Post
    ok, I have changed the ActionPerformed code, now it still wont work. Is it possible to set the textfield on blank by the way? Because it just doesnt want to be empty after putting elements in the array
    Clearing the text in a JTextField is as easy as field.setText(""); you can stick that in your ActionListener and see if it is really called by the component.

    kind regards,

    Jos

  12. #12
    vermicelli is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You haven't added an ActionListener to your addButton so you can click all you want but nothing is going to happen. Implementing an actionPerformed( ... ) method is not enough. Your frame should implement the ActionListener interface and it should be added as such to the button.

    kind regards,

    Jos

    ps. there are more errors in the code you've shown us ...
    yes, it worked!! I didn't read your post very carefully so I made the stupid mistake to forget the .addActionListener(this); button!!
    I thank you all very much for your time(and patience :P)

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

Posting Permissions

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