Results 1 to 9 of 9
  1. #1
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default menubutton to activate a function

    so i want to make this function to be inserted in my menu-button, "LäggTillKund"(which btw, is translated to new costumer).. anyway, i want public void läggtillkund to activate when i click läggtillkund-menubutton. i cant seem to figure out how


    Java Code:
    import java.io.*;
    
    import javax.swing.*;
    import java.util.*;
    
    public class Kund {
    	private String namn, efternamn, personnummer;
    	private String Kundfil="Kunder.txt";
    
    public void läggtillkund(String namn, String efternamn, String personnummer) throws IOException{
    		FileWriter x = new FileWriter(Kundfil, true);
    		
    		PrintWriter Y = new PrintWriter(x);
    		
    		namn=JOptionPane.showInputDialog("Ange nytt namn.");
    		efternamn=JOptionPane.showInputDialog("Ange nytt efternamn.");
    		personnummer=JOptionPane.showInputDialog("Ange nytt personnummer.");
    		Y.print("");
    		Y.println(namn);
    		Y.println(efternamn);
    		Y.println(personnummer);
    		Y.close();	
    	}
    Java Code:
    import java.awt.*;	
    		import java.awt.event.*;
    import java.io.IOException;
    
    import javax.swing.*;
    
    			public class Order extends JFrame{
    				private JMenuBar huvudmeny;
    				private JMenu Profiler, Meny;
    				private JMenuItem LäggTillKund, TagBortKund;
    				private JComboBox box;
    				private JPanel P, P2, P3;
    				
    				public Order(){
    					super("Huvudmeny");
    					huvudmeny = new JMenuBar();
    					Profiler = new JMenu("Profiler");
    					Meny = new JMenu("Meny");
    					LäggTillKund = new JMenuItem("Lägg till kund");
    					TagBortKund = new JMenuItem("Tag bort kund");
    					
    					
    					LäggTillKund.addActionListener(new LyssnareNYKUND());
    					TagBortKund.addActionListener(new Lyssnare2());
    					Profiler.add(LäggTillKund);
    					Profiler.add(TagBortKund);
    
    setJMenuBar(huvudmeny);
    					huvudmeny.add(Meny);
    					Meny.add(Profiler);
    					setSize(300, 300);
    					setDefaultCloseOperation(EXIT_ON_CLOSE);
    					setLocationRelativeTo(null);
    					setVisible(true);
    					}
    				public class LyssnareNYKUND implements ActionListener{
    					public void actionPerformed(ActionEvent e){
    						if(e.getSource()==LäggTillKund)
    							getContentPane().set
    						Kund X = new Kund();
    						
    					
    						X.läggtillkund();
    							
    						}
    					}
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    					if(e.getSource()==LäggTillKund)
    							getContentPane().set
    						Kund X = new Kund();
    						
    					
    						X.läggtillkund();
    							
    						}
    :eek:
    getContentPane().set <-- whats that and where is the brace to open the if-statement?

    X.läggtillkund() : the method expected three parameter!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

  4. #4
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    Java Code:
    					if(e.getSource()==LäggTillKund)
    							getContentPane().set
    						Kund X = new Kund();
    						
    					
    						X.läggtillkund();
    							
    						}
    :eek:
    getContentPane().set <-- whats that and where is the brace to open the if-statement?

    X.läggtillkund() : the method expected three parameter!
    well, if(e.getSource()==LäggTillKund)
    getContentPane().set is some half-done something i did.. not suppose to be in the code

  5. #5
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    thanks, but ive seen the page before, general stuff isnt very helpful if ur a newbie :/

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Quote Originally Posted by LennyKosmos View Post
    well, if(e.getSource()==LäggTillKund)
    getContentPane().set is some half-done something i did.. not suppose to be in the code
    We can't help you if you're not posting the actual code. I recommend you post an SSCCE based off the tutorial I linked and go from there.

  7. #7
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    alright, well check this out..
    Java Code:
    import java.io.*;
    
    import javax.swing.*;
    import java.util.*;
    
    public class Kund {
    	private String namn, efternamn, personnummer;
    	private String Kundfil="Kunder.txt";
    
    	
    	public void läggtillkund(String namn, String efternamn, String personnummer) throws IOException{
    		FileWriter x = new FileWriter(Kundfil, true);
    		PrintWriter Y = new PrintWriter(x);
    		
    		namn=JOptionPane.showInputDialog("Ange nytt namn.");
    		efternamn=JOptionPane.showInputDialog("Ange nytt efternamn.");
    		personnummer=JOptionPane.showInputDialog("Ange nytt personnummer.");
    		Y.print("");
    		Y.println(namn);
    		Y.println(efternamn);
    		Y.println(personnummer);
    		Y.close();	
    	}
    	
    	
    
    
    public static void main (String[] args)throws IOException{
    	new Order();	
    	
    }	}
    and the other class:

    Java Code:
    	import java.awt.*;	
    		import java.awt.event.*;
    import java.io.IOException;
    
    import javax.swing.*;
    
    			public class Order extends JFrame{
    				private JMenuBar huvudmeny;
    				private JMenu Profiler, Meny;
    				private JMenuItem LäggTillKund;
    				private JComboBox box;
    				private JPanel P, P2, P3;
    				
    				public Order(){
    					super("Huvudmeny");
    					huvudmeny = new JMenuBar();
    					Profiler = new JMenu("Profiler");
    					Meny = new JMenu("Meny");
    					LäggTillKund = new JMenuItem("Lägg till kund");
    					
    					
    					LäggTillKund.addActionListener(new LyssnareNYKUND());
    					Profiler.add(LäggTillKund);
    					
    					
    				
    					
    					
    					setJMenuBar(huvudmeny);
    					huvudmeny.add(Meny);
    					Meny.add(Profiler);
    					setSize(300, 300);
    					setDefaultCloseOperation(EXIT_ON_CLOSE);
    					setLocationRelativeTo(null);
    					setVisible(true);
    					}
    				public class LyssnareNYKUND implements ActionListener{
    					public void actionPerformed(ActionEvent e){
    					
    					//??? here, what to type to make the button work :(
    							
    			}
    			
    		}	
    	}
    hope this make sense :)

  8. #8
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by LennyKosmos View Post
    //??? here, what to type to make the button work :(
    i wrote it already..your problem is your läggtillkund method

    public void läggtillkund(String namn, String efternamn, String personnummer)

    change it to

    public void läggtillkund()

    or invoke the method with three strings !!!
    Last edited by eRaaaa; 10-28-2010 at 04:37 PM.

  9. #9
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    i wrote it already..your problem is your läggtillkund method

    public void läggtillkund(String namn, String efternamn, String personnummer)

    change it to

    public void läggtillkund()

    or invoke the method with three strings !!!
    oh yeah, thanks bud.. so this is what it looks like:

    Java Code:
    try {
    							X.läggtillkund();
    						} catch (IOException e1) {
    							e1.printStackTrace();
    						}
    i had to surround it with try-catch for some reason.. anyway, it works :D

Similar Threads

  1. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  2. Does this function cause lag?
    By Addez in forum New To Java
    Replies: 5
    Last Post: 12-06-2009, 10:53 AM
  3. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  4. [SOLVED] MenuButton
    By teo in forum New To Java
    Replies: 5
    Last Post: 06-19-2008, 12:33 PM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:56 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
  •