1

trying to solve a problem and i cant get why it wont work. I'm sorry if I confuse you with my norwegian comments and variables.

First, here is my form.java file.

import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;


public class Form implements ActionListener {

    String[] ansatt_type = {"Sjef","Mellomleder","Assistent"};
    String totlønn;

    // KOMPONENTER FOR GUI START
    JList ansatte;
    DefaultListModel model;

    JLabel label1 = new JLabel ();
    JComboBox ansatt_id = new JComboBox (ansatt_type);
    JButton add_me = new JButton ();
    JLabel lønn = new JLabel ();
    // KOMPONENTER FOR GUI SLUTT

    public Form () {

        // LAGER RAMME START
        JFrame ramme = new JFrame ();
        ramme.setBounds(0,0,275,400);
        ramme.setTitle("Ansatt kontroll");
        ramme.setLayout(new FlowLayout ());
        ramme.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // LEGGER TIL TEXT LABEL1
        label1.setText("Liste over ansatte: ");
        ramme.add(label1);

        // LEGGER TIL DEFAULTLISTMODEL
        model = new DefaultListModel();

        ansatte = new JList(model);
        ansatte.setBounds(0, 0, 200, 200);
        model.addElement("KU");
        ramme.add(ansatte);


        // LEGGER TIL DROPDWON LIST;
        ramme.add(ansatt_id);

        // LEGGER TIL ANSATT KNAPP
        add_me.setText("Legg til ny ansatt");
        ramme.add(add_me);
        add_me.addActionListener(this);

        // LEGGER TIL SAMLEDE LØNNSKOSTNADER
        totlønn = "Totale lønnskostnader er : eksempeltall";
        lønn.setText(totlønn);
        ramme.add(lønn);

        ramme.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        JOptionPane.showMessageDialog(null, "Du har valgt: 

               "+ansatt_id.getSelectedItem()+"!" +  
               " Du blir nå videreført og kan legge til en ny ansatt");

        if(ansatt_id.getSelectedItem() == "Sjef"){
            System.out.println("Valgt Sjef");
            Sjef sj = new Sjef ();
            model.addElement(sj);    
            }

        if(ansatt_id.getSelectedItem() == "Mellomleder"){
            System.out.println("Valgt Mellomleder");
        }

        if(ansatt_id.getSelectedItem() == "Assistent"){
            System.out.println("Valgt Assistent");
        }
    } 
}

I also have a class file called Ansatt.java who several class fiels extends from. I'l show you one.

First my Ansatt.java file ;

import javax.swing.JOptionPane;


public class Ansatt extends Form {

    public String Navn;
    public int Lønn;
    public String Type;

    public Ansatt () {
    Navn = JOptionPane.showInputDialog(null, "Skriv inn navn på ny ansatt:  ");
    System.out.println("Ansatt lag til i liste");
    }

    public String toString(){
        return Navn + " " + Type;
    }
}

And the extended class Sjef.java

public class Sjef extends Ansatt {

    public Sjef () {
        super();
        this.Lønn = 40000;
        this.Type = "Sjef";


    }
}

Everything works, except the ModelList wont update, I have a working example, who is almost identical but it just doesent work in this one!

JonK
  • 2,097
  • 2
  • 25
  • 36
chriskvik
  • 1,281
  • 2
  • 10
  • 32
  • Don't use `setBounds()`; instead, `pack()` your chosen layout. Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. See [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html), as well as examples [here](http://stackoverflow.com/a/5760093/230513) and [here](http://stackoverflow.com/a/5372169/230513). – trashgod Nov 05 '12 at 21:00

1 Answers1

1

Your problem is the String comparison in your ActionListener:

ansatt_id.getSelectedItem() == "Sjef"

will most likely not return true. You should use

"Sjef".equals( ansatt_id.getSelectedItem() )

Same for the other comparisons.

Robin
  • 36,233
  • 5
  • 47
  • 99
  • Thanks, now the JList does appear when i load the program. But it still wont add new items. I deleting everything in the ActionListner, and just write: Sjef sj = new Sjef (); model.addElement(sj);` But still no luck, it appears that the JList dont update. – chriskvik Nov 07 '12 at 00:55