Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By OPTYMISTA

Thread: JFrame/array problem

  1. #1
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default JFrame/array problem

    Hi guys!
    I have some troubles during analyzing my program. To keep it simple I’ll just write a short summary of what I expect this program to do. So here we go, program should:
    • Let user input their name, last name etc. in my “Ansatte” JFrame window
    • When “Ny Ansatt” button is clicked it should create a new employee object, calling own Employee class and then add new employee in arraylist with all employees, which I called “ansatte”. An employee object has 5 input variables. Those are user inputs from JFrame window. Also tekstFelt, tekstFelt etc…
    Program is running correctly, until I want to add more than 1 employee. It’s saving all data to first employee I add, but then I try to add second one … all data to the first one are replaced by those new data from new employee (the first employee is gone now). I know that my explanation it’s kind of confusing, so I’ll illustrate it graphically. Sorry for my poor, broken English. I Hope You can help me out with this one. Thanks for Your response! ;)

    One employee: JFrame/array problem-1.png,
    and when i added second employee: JFrame/array problem-2.png

    Here's some piece of code, I start with this:

    Java Code:
    	private static final long serialVersionUID = 1;
    	static JPanel midtPanel, nedrePanel, nedrePanel2;
    	static JFrame ramme;
    	static JButton lagre, laste, nyAnsatt, ansattliste, soke, oppdater, fjernData;
    	static JTextField tekstFelt, tekstFelt2, tekstFelt3, tekstFelt4, tekstFelt5;
    	static JLabel etikett, etikett2, etikett3, etikett4, etikett5;
    	static ArrayList<Employee> ansatte = new ArrayList<>();
    	private static JFileChooser filvelger = null;
    	
    	public AnsatteLayout() {
    		
    		// Ramme av programmet
    		ramme = new JFrame("Ansatte");
    		ramme.setLayout(new BorderLayout());
    
    		midtPanel = new JPanel();
    		midtPanel.setLayout(new GridLayout(5,1));
    
    		// Etiketter
    		etikett = new JLabel("Fornavn");
    		tekstFelt = new JTextField(20);
    
    		etikett2 = new JLabel("Etternavn");
    		tekstFelt2 = new JTextField(20);
    
    		etikett3 = new JLabel("Epostadresse");
    		tekstFelt3 = new JTextField(20);
    
    		etikett4 = new JLabel("Telefonnummer");
    		tekstFelt4 = new JTextField(20);
    
    		etikett5 = new JLabel("Kontor");
    		tekstFelt5 = new JTextField(20);
    
    		// Setter opp 2 stk Panel-objekter som ska befinne seg nederst
    		nedrePanel = new JPanel();
    		nedrePanel.setLayout(new GridLayout(1,4));
    
    
    		nedrePanel2 = new JPanel();
    		nedrePanel2.setLayout(new GridLayout(1,3));
    
    		// BUTTONS
    		soke = new JButton("Søke");
    		oppdater = new JButton("Oppdater");
    		
    		nyAnsatt = new JButton("Ny Ansatt");
    		nyAnsattLytter handler = new nyAnsattLytter();
    		nyAnsatt.addActionListener(handler);
    
    		ansattliste = new JButton("AnsattListe");
    		ansattListeLytter handler2 = new ansattListeLytter();
    		ansattliste.addActionListener(handler2);
    ... and my 2 "Listeners"

    Java Code:
     private class nyAnsattLytter implements ActionListener {
    		Room rom;
    		
    		public void actionPerformed(ActionEvent event){
    
    			rom = new Room(tekstFelt4.getText());
    			Employee.r_number = rom;
    
    			Employee nyAnsatt = new Employee(tekstFelt.getText(), tekstFelt2.getText(), tekstFelt3.getText()
    					, Integer.valueOf(tekstFelt4.getText()), rom);
    			
    			ansatte.add(nyAnsatt);
    
    			JOptionPane.showMessageDialog(null, nyAnsatt.getName() + " " + nyAnsatt.getLastname()
    					+ " er lagret i ansattliste.");
    
    			
    		}
    	}
    	private class ansattListeLytter implements ActionListener {
    		
    		public void actionPerformed(ActionEvent event){
    			JOptionPane.showMessageDialog(null,ansatte);
    		}
    	}
    Last edited by OPTYMISTA; 03-24-2013 at 01:23 AM.

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

    Default Re: JFrame/array problem

    I wonder if your problem is in code not that you are not showing us. Could the Employee class be using static fields?

  3. #3
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: JFrame/array problem

    Yeah... You're definitely right! I didn't notice that before, but as you suggested my Employee class used static variables. Thanks for your help, some I totally appreciate!
    (+) for you!
    Fubarable likes this.

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

Similar Threads

  1. Passing an array to another class in a JFrame
    By George5432 in forum New To Java
    Replies: 27
    Last Post: 05-06-2012, 07:13 AM
  2. Replies: 0
    Last Post: 08-30-2011, 10:18 AM
  3. Help, JFrame Array with RadionButton
    By acat in forum New To Java
    Replies: 6
    Last Post: 04-11-2011, 11:08 PM
  4. (jframe) get a variable out of array list
    By joelharkes in forum New To Java
    Replies: 9
    Last Post: 07-08-2010, 11:38 PM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 AM

Posting Permissions

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