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

    Default pressing a button making a instance in another class

    Hey,

    I have a program with 3 classes. What I'm trying to do is that when my class Vindu is running and when I type in information on the
    window on three different text fields for Navn, Dato og innlegg (Name, Date, Post) and then press the button "Lagre innlegg" (which means Save Post) it will be made an instance of the class Innlegg (which means post) with the data information written in the text field in class Vindu.

    In the class Innlegg I have Name, Date and Post in get/set methods. My assignments is saying that I'm going to use ArrayList<Innlegg>

    So far my code using ArrayList is this :

    public class Innlegg {
    private ArrayList<Innlegg> al new ArrayList<Innlegg> ();
    public boolean add(Innlegg innlegg) { }
    public String toString ()

    {

    But errors is coming up, also I have no idea how to do with the Jbutton.

    Would be very thankful for any help,

    here is the full code in class Vindu :


    Java Code:
    public class Vindu extends JFrame{
    	
    	
    	
    	JTextArea jta = new JTextArea();
    	JTextField jtf = new JTextField(20);
    	
    	String alt ="";
    
    	JTextField jtaInnlegg = new JTextField();
    	JTextField jtaNavn = new JTextField();
    	JTextField jtaDato = new JTextField();
    	
    	
    	ArrayList<String> al;
    	
    	public class Innlegg {
    	private ArrayList<Innlegg> new ArrayList<Innlegg>();
    	public boolean add(Innlegg innlegg) { }
    	public String   toString () 
    	
    	{}
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	public Vindu(){
    		al= new ArrayList<String>();
    		
    
    		
    	
    		JMenuBar jmb = new JMenuBar(); //lag menybaren
    		JMenu jmiFile= new JMenu("File");//lag enkelthovedmenyene, File,Edit
    		JMenu jmiEdit = new JMenu("Edit");
    		this.setJMenuBar(jmb);//legg hovedmenyen til menybaren
    		jmb.add(jmiFile);//legg hovedmenyene til menybaren
    		jmb.add(jmiEdit);
    		JMenuItem Delete = new JMenuItem("Delete");//Lag menyvalget Delete
    		jmiEdit.add(Delete);//legge delete til Edit hovedmenyen
    		//Lag Center
    		
    		Container cp = this.getContentPane();//Hent innholdsfortegnelese
    		cp.add(jta,BorderLayout.SOUTH);
    		
    		
    		
    		JPanel jpWest = new JPanel();//lag panel
    		GridLayout gl = new GridLayout(0,2);
    		jpWest.setLayout(gl);
    		 
    		JLabel jlNavn = new JLabel("Navn:");
    		JTextField jtfNavn = new JTextField(20);
    		jpWest.add(jlNavn);
    		jpWest.add(jtfNavn);
    		
    		 
    		JLabel jlDato = new JLabel("Dato:");
    		JTextField jtfDato = new JTextField(20);
    		jpWest.add(jlDato);
    		jpWest.add(jtfDato);
    		
    		 
    		JLabel jlInnlegg = new JLabel("Innlegg:");
    		jpWest.add(jlInnlegg);
    	    JTextField jtfInlegg = new JTextField(20);
    		jpWest.add(jtaInnlegg);
    		
    		
    		 
    		final JButton jb = new JButton("Lagre innlegg");
    		jpWest.add(jb);
    		
    		
    
    		
    		jb.addActionListener
    		(
    		new ActionListener(){
    			public void actionPerformed(ActionEvent evt) {
    				System.out.println("Knappen virket");				  
    				String innlegg = jtaInnlegg.getText();
    				al.add(innlegg);
    				String alt="Antall Innlegg: " + al.size()+"\n";
    				for (int i=al.size(); i >0; i--){
    					alt+=al.get(i-1)+"\n";
    					
    					//for (int i=0;i<al.size();i++);
    					
    					
    					
    					
    					
    					 
    					}
    				jta.setText(alt);
    					 
    				
    
    				}
    		
    			
    			}
    		);
    	
    		
    		   
    
    			
    			//jpWest.add(jtaInnlegg);
    		
    			//jpWest.add(jb);
    			
    			
    			
    		
    			JPanel jpInner = new JPanel();
    			jpInner.add(jpWest);
    			cp.add(jpInner,BorderLayout.WEST);//legg panel i west
    
    			
    			this.setTitle("Blogg program");
    			this.setLocation(150, 150);
    			this.setSize(400, 400);
    			this.setVisible(true);	
    		}
    
    		public static void main(String[] args) {
    			new Vindu();
    			new Innlegg();
    
    		}
    
    
    	}
    }
    Last edited by SpicyElectricity; 02-18-2012 at 09:38 PM. Reason: updating code

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

    Default Re: pressing a button making a instance in another class

    Quote Originally Posted by SpicyElectricity View Post
    But errors is coming up, ...
    What errors? Can you post the complete error message(s)?


    ...also I have no idea how to do with the Jbutton.
    No idea how to do what? Please be as informative as possible when asking questions so we can better know what your exact problems are.

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

    Default Re: pressing a button making a instance in another class

    I did some updating for the code and the numbering didn't work again :/ I'm sorry I don't know how to get it numbered again

    But errors that is coming up is :

    private ArrayList<Innlegg> new ArrayList<Innlegg>();

    It says :
    Multiplie markers at this line
    -Syntax error on token ">", identifier expected after this token
    -Syntax error on this token "New",. expected

    The other errors did came up after I put the ArrayList<Innlegg> code


    this.setJMenuBar(jmb);

    Error says :

    The method setJmenuBar(JMenuBar) is undefinied for the type Vindu.innlegg

    Another one:

    Container cp = this.getContentPane();

    Error says :

    The method getContentPane() is undefined for the type Vindu.Innlegg

    For this ones here


    this.setTitle("Blogg program");
    this.setLocation(150, 150);
    this.setSize(400, 400);
    this.setVisible(true);

    Errors saying the methods is undefined for the type Vindu.Innlegg

    The last one is

    public static void main(String[] args) {

    Error says

    Multiple markers at this line
    - Method breakpoint:Vindu$Innlegg [entry] - main(String[])
    - The method main cannot be declared static; static methods can only be declared in a static or top


    About Jbutton, I don't know how to write the code so when using the button it will create an instance of information in the class Innlegg with the information i wrote in the class Vindu

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

    Default Re: pressing a button making a instance in another class

    You've got many compilation errors and many problems with your code including trying to nest a public class in another class, including mis-matching of your curly braces,.... etc, which suggests that you are breaking a cardinal rule of coding: adding code to bad code. Consider starting over but this time compiling early and often, probably after adding each single line of code, and not adding any new code until all compilation errors have been fixed.

    With regard to the JButton, start with what you do know and try to work step-wise from there. For instance, I assume that you know how to add an ActionListener to a JButton, right? then break down each step in the next process and in your button's ActionListener try to solve each step of the problem one at a time since it's much easier solving simple problems then big ones. So again, add the ActionListener, and in the ActionListener's actionPerformed try to create your Innlegg class. then try to pass the necessary information into the class, then...

    and if you get stuck, you'll be able to then ask a very specific question that we can better be able to help you with.
    Last edited by Fubarable; 02-18-2012 at 10:52 PM.

Similar Threads

  1. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  2. Draw shapes by pressing a button
    By SWEngineer in forum AWT / Swing
    Replies: 16
    Last Post: 05-14-2010, 05:26 AM
  3. Replies: 6
    Last Post: 12-23-2009, 09:10 AM
  4. Replies: 2
    Last Post: 04-12-2009, 10:39 AM
  5. Replies: 5
    Last Post: 01-17-2008, 09:01 PM

Posting Permissions

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