Results 1 to 4 of 4
  1. #1
    mordinib is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Object-list to actionPerformed

    Here is what i do, i'm reading in objects from a objectfile and the field/list/array is called bilar[] (english = cars[]), and when i want to add a new car you hit the button spara (engish = save) whitch is the "Kspara" button.

    To know what place in the array i want to place the new car i have to use the field bilar[] to make sure the object is properly done or even done at all.
    I need to use it in the actionPerformed method or in a submethod and then i thought i'd read the object file in the actionPerformed-method but to do that i need a throws Exception in the actionPerformed-method.

    So my question is how do i get acces to the field bilar[] or reading the objectfile in the actionPerformed-method.

    Note that this is not the whole code so if u need more information/code, just tell me.


    Main and GUI
    Java Code:
    import java.awt.*;
    import java.awt.event.*; //innehŒller klasser fšr lyssnare
    import javax.swing.*;
    import java.io.*;
    import java.lang.*;
    
    
    
    
    public class flashback extends JFrame implements ActionListener {
    
      private JLabel lab = new JLabel("Info om bil. hehehe", JLabel.CENTER);
      private JButton Klaggtillbil = new JButton("Lägg Till Bil", new ImageIcon("flag_swe.gif"));
      private JButton Ksokbil = new JButton("Sök Bil", new ImageIcon("flag_eng.gif"));
      private JButton Ksoksald = new JButton("Sök Såld Bil");
      private JButton Kvisaalla = new JButton("Visa Alla Bilar");
      private JButton Kavsluta = new JButton("Avsluta");
      private JLabel lab2 = new JLabel("Här ska funktionerna vara!", JLabel.CENTER);
      private JLabel bosse = new JLabel("Bosses Bil", JLabel.CENTER);
      private JTextField namn = new JTextField("Namn", 10);
      private JTextField marke = new JTextField("Märke", 10);
      private JTextField modell = new JTextField("Modell",10);
      private JTextField regnummer = new JTextField("Regnummer",10);
      private JTextField pris = new JTextField("Pris",10);
      private JTextField personnr = new JTextField("Personummer",10);
      private JButton Kspara = new JButton("Spara");
    
    
    
    
    
    
      public flashback(){   // Konstruktor
    
        GridBagLayout m = new GridBagLayout();
        setLayout(m);
        GridBagConstraints con;
    
    
    
    
        //Lägg till bil-knappen i ruta 1,0
        con = new GridBagConstraints();
        con.gridy = 1; con.gridx = 0; // gridens placering rad 1 altså andra raden och kolumn 0
        con.ipadx=7;				  //utvidga knappen med 7 pixlar från sin orginalstorlek
        con.weightx = 1;
        con.fill = GridBagConstraints.BOTH;
        m.setConstraints(Klaggtillbil, con);
        add(Klaggtillbil);			  //lägger till knappen
    
    
        //Funktionsrutan 0,1
        JPanel skapa = new JPanel();        //skapar ny jpanel
        skapa.setBackground(Color.yellow);
        con = new GridBagConstraints();
        con.gridy = 1; con.gridx = 1;
        con.ipadx=100;
        con.weightx = 1;
        con.gridwidth = 1; con.gridheight =7;
        //con.ipadx=200;
        con.fill = GridBagConstraints.BOTH;     //Fyll ut allt utrymme
        m.setConstraints(skapa, con);
        add(skapa); skapa.add(marke); skapa.add(modell); skapa.add(regnummer); skapa.add(pris); skapa.add(personnr); skapa.add(Kspara);
    
    	//Färger, fonter etc
        getContentPane().setBackground(Color.white);
        lab.setFont(new Font("Georgia", Font.ITALIC, 10));
        lab2.setFont(new Font("SansSerif", Font.BOLD, 14));
    
        //Koppla lyssnare till knapparna
        Klaggtillbil.addActionListener(this);
    	marke.setVisible(false);
      	modell.setVisible(false);
      	regnummer.setVisible(false);
      	pris.setVisible(false);
      	personnr.setVisible(false);
      	Kspara.setVisible(false);
      	namn.setVisible(false);
    
    	//pack();
        // Old
        setSize(800,600);
    
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
      }
    
      // Lyssnarmetod
      public void actionPerformed(ActionEvent e) [COLOR="Red"]/* throws Exeption does not work here...*/[/COLOR] {
      	marke.setVisible(false);
      	modell.setVisible(false);
      	regnummer.setVisible(false);
      	pris.setVisible(false);
      	personnr.setVisible(false);
      	Kspara.setVisible(false);
      	namn.setVisible(false);
    
    
    
        if(e.getSource() == Klaggtillbil){
          // AnvŠndaren klickade på knappen lägg till bil
          lab2.setText("lägg till bil");
          namn.setVisible(true);
          marke.setVisible(true);
          modell.setVisible(true);
      	  regnummer.setVisible(true);
      	  pris.setVisible(true);
      	  personnr.setVisible(true);
      	  Kspara.setVisible(true);}
    
    		[COLOR="Red"]//clicked on spara (save)[/COLOR]
      	  if(e.getSource() == Kspara) {
      	  	String nn = namn.getText();
      	  	String me = marke.getText();
      	  	String ml = modell.getText();
      	  	String rr = regnummer.getText();
      	  	String price = pris.getText();
      	  	String pr = personnr.getText();
      	  	String allt = nn + me + ml + rr + price + pr;
    		[COLOR="Red"]//here is were i need the object-field/array/list[/COLOR]
    
      	  	nyBil(nn, me, ml, rr, price, pr);
    
      	  }
      }
    
    
    
    
    
    		public static void main (String[] Arg) throws Exception {
    
    			//Lägger in alla nu existerande bilar i minnet genom att läsa en objektfil
    			ObjectInputStream objIn = new ObjectInputStream
    				(new FileInputStream("utfil.data"));
    
     				[COLOR="Red"]//This field of objects, i want to use it in the actionPerformed method[/COLOR]
    				Bil [] bilar;
    				bilar = new Bil[100];
    				int raknare=0;
    					while (true){
    						try{
    							bilar[raknare] = (Bil)objIn.readObject();}
    						catch (EOFException e){
    							break;} //filen är slut}
    						raknare++;}
    
    				int k=0;
            		while(bilar[k]!=null){
            		k++;}
    			System.out.print(bilar[2].marke);
    			//Lägger till menyn (GUI)
        		flashback j = new flashback();
    		}
    
    
    public void nyBil(String nn, String rr, String me, String ml, String price, String pr)
    				/* [COLOR="Red"]throws Exception does work here but then i need it in the listener as well.. [/COLOR]*/{
    				//reading the object from file
    	
    				/*ObjectInputStream objIn = new ObjectInputStream
    				(new FileInputStream("utfil.data"));
    				Bil [] bilar;
    				bilar = new Bil[100];
    				int raknare=0;
    				while (true){
    				try{
    				bilar[raknare] = (Bil)objIn.readObject();}
    				catch (EOFException e){
    				break;} //filen är slut}
    				raknare++;}*/
    
    
    				System.out.println(nn + " " + rr);
    
    			}
      }
    Last edited by mordinib; 04-12-2011 at 08:28 PM.

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

    Default

    Since the bilar arrays are all local to methods, they are not visible to the class. You may wish to make the array a class field instead.

  3. #3
    mordinib is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Problem solved, thanks Fubarable!

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

    Default

    Quote Originally Posted by mordinib View Post
    Problem solved, thanks Fubarable!
    You're welcome! Best of luck with your project!

Similar Threads

  1. Replies: 1
    Last Post: 04-10-2011, 03:06 PM
  2. contents of List<object> array changed?
    By soichi in forum New To Java
    Replies: 2
    Last Post: 01-19-2011, 11:58 AM
  3. object list sorting
    By karq in forum Advanced Java
    Replies: 2
    Last Post: 10-08-2010, 06:45 PM
  4. List views, a type of object
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 04:07 AM
  5. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:26 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
  •