Results 1 to 4 of 4
Thread: Object-list to actionPerformed
- 04-12-2011, 07:14 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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 07:28 PM.
-
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.
- 04-13-2011, 01:07 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Problem solved, thanks Fubarable!
-
Similar Threads
-
adding component(object?) to JFrame after actionPerformed
By Shargath in forum New To JavaReplies: 1Last Post: 04-10-2011, 02:06 PM -
contents of List<object> array changed?
By soichi in forum New To JavaReplies: 2Last Post: 01-19-2011, 10:58 AM -
object list sorting
By karq in forum Advanced JavaReplies: 2Last Post: 10-08-2010, 05:45 PM -
List views, a type of object
By Leprechaun in forum New To JavaReplies: 2Last Post: 02-06-2008, 03:07 AM -
how to remove an object from the array list
By cecily in forum New To JavaReplies: 3Last Post: 08-02-2007, 02:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks