Domanda

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!

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top