Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:24:36+00:00 2026-06-13T21:24:36+00:00

trying to solve a problem and i cant get why it wont work. I’m

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:24:37+00:00Added an answer on June 13, 2026 at 9:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve the problem here but I don't know why my code
I've been trying to solve this problem for a while now and I can't
I'm trying to solve Problem 13 of project euler, which involves the sum of
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I'm trying to solve this problem in a pure-functional way, without using set! .
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,
I was trying to solve this problem on SPOJ (http://www.spoj.pl/problems/REC/) F(n) = a*F(n-1) +
I am trying to solve this problem. I have a series of SELECT statements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.