accessors in class extending JPanel - java

I'm writing a clone of Risk in Java, and am having some trouble with my code. When I create a new game, I create a JPanel with a JTextField (for the player name) and a JComboBox(for the player color), one panel for each player the user wants to create. Instances of this panel are created dynamically based on a second JComboBox which lets the user select a number of players from three to eight.
My problem is that when I want to create the player objects from the data entered into the player creation panel, each player object retrieves the data from the most-recently-created player creation panel. I have a functional solution, but I can't seem to figure out why what seems to be the 'proper' solution () won't work.
This is the code that I have working right now:
Creation panel class:
package risk;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PlayerCreatePanel extends JPanel{
public static ImageIcon[] playerColors = {Resources.red,Resources.green,Resources.blue,Resources.cyan,Resources.magenta,Resources.yellow,Resources.orange,Resources.gray};
public JComboBox playerColor;
public JTextField playerName;
public PlayerCreatePanel(int index){
this.setPreferredSize(new Dimension(360, 30));
JLabel numberLabel = new JLabel ("Player " + index + ": ");
JLabel nameLabel = new JLabel("Name: ");
JLabel colorLabel = new JLabel(" Color: ");
playerName = new JTextField("");
playerName.setColumns(13);
playerColor = new JComboBox(playerColors);
this.add(numberLabel);
this.add(nameLabel);
this.add(playerName);
this.add(colorLabel);
this.add(playerColor);
}
}
And for the new game class, the part that creates players:
package risk;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
public class NewGame {
private static PlayerCreatePanel [] panels = { new PlayerCreatePanel(1), new PlayerCreatePanel(2),
new PlayerCreatePanel(3), null, null, null, null, null};
private static int playerCount = 3;
public static void createPlayers(){
Resources.players = new Player[playerCount];
switch(playerCount){
case 8: Resources.players[7] = new Player (panels[7].playerName.getText(), Resources.colors[panels[7].playerColor.getSelectedIndex()], 8);
case 7: Resources.players[6] = new Player (panels[6].playerName.getText(), Resources.colors[panels[6].playerColor.getSelectedIndex()], 7);
case 6: Resources.players[5] = new Player (panels[5].playerName.getText(), Resources.colors[panels[5].playerColor.getSelectedIndex()], 6);
case 5: Resources.players[4] = new Player (panels[4].playerName.getText(), Resources.colors[panels[4].playerColor.getSelectedIndex()], 5);
case 4: Resources.players[3] = new Player (panels[3].playerName.getText(), Resources.colors[panels[3].playerColor.getSelectedIndex()], 4);
case 3: Resources.players[2] = new Player (panels[2].playerName.getText(), Resources.colors[panels[2].playerColor.getSelectedIndex()], 3);
Resources.players[1] = new Player (panels[1].playerName.getText(), Resources.colors[panels[1].playerColor.getSelectedIndex()], 2);
Resources.players[0] = new Player (panels[0].playerName.getText(), Resources.colors[panels[0].playerColor.getSelectedIndex()], 1); break;
default: break;
}
}
}
Now, what I've learned is the proper way to do this is to make my JTextField and JComboBox private, and write accessors, like so:
private JComboBox playerColor;
private JTextField playerName;
//...same method as above
public static String getName(){
return playerName.getText();
}
public static int getColorIndex(){
return playerColor.getSelectedIndex();
}
and change the creation lines in the new game method to read something like this:
Resources.players [0] = new Player (panels[0].getName(), panels[0].getColorIndex(), 1);

Related

Varaible in If statement in ActionListener

I'm making an app, with multiple classes, and in one class i have some code to assign a random number from 1 - 6 to a variable. In another class i have a JLabel showing this variable. And in another class i have a JButton, with a ActionListener, within this is an IF Statement, with the variable with the random number in.
The variable is in Class1.
package Dice;
public class DiceRoll {
public int DICEONE;
public int DICETWO;
private int max = 6;
private int min = 1;
public void DiceRollMethod() {
DICEONE = (int) (Math.floor(Math.random() * (max - min + 1)) + min);
DICETWO = (int) (Math.floor(Math.random() * (max - min + 1)) + min);
System.out.println(DICEONE);
System.out.println(DICETWO);
}
}
The variable is 'DICEONE'
The JLabel is in Class2 (which rolls the dice).
package Dice;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DiceButton extends JPanel{
private static final long serialVersionUID = 1L;
DiceRoll roll = new DiceRoll();
JButton btnRoll;
public JLabel DB1, DB2;
public DiceButton() {
DB1 = new JLabel("Dice 1: " + roll.DICEONE);
DB2 = new JLabel("Dice 2: " + roll.DICETWO);
DB1.setVisible(true);
DB2.setVisible(true);
btnRoll = new JButton("Roll Dice");
btnRoll.setVisible(true);
btnRoll.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
roll.DiceRollMethod();
DB1.setText("Dice 1: " + roll.DICEONE);
DB2.setText("Dice 2: " + roll.DICETWO);
}
});
add(DB1);
add(DB2);
add(btnRoll);
}
}
When the button is clicked, the random number is assigned to the variable and then displayed.
Finally the variable is used in the ActionListener in Class3 (this is the bit i have a problem with).
package Tiles;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import Dice.DiceButton;
import Dice.DiceRoll;
public class SpaceOne extends JPanel{
private static final long serialVersionUID = 1L;
DiceRoll roll = new DiceRoll();
DiceButton dButton = new DiceButton();
JButton btn1;
JLabel lbl1;
Font lblFont = new Font("Helvetica", Font.BOLD, 90);
Border lblBorder = BorderFactory.createLineBorder(Color.BLACK, 3);
public SpaceOne() {
setSize(50,100);
setLayout(new GridLayout(1,2));
lbl1 = new JLabel("1");
lbl1.setVisible(true);
lbl1.setFont(lblFont);
lbl1.setBorder(lblBorder);
lbl1.setLocation(1, 1);
btn1 = new JButton("1");
btn1.setVisible(true);
btn1.setLocation(1, 2);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(roll.DICEONE == 1) {
lbl1.setText(".");
System.out.println("DICEONE");
}else if(roll.DICETWO == 1) {
lbl1.setText(".");
System.out.println("DICETWO");
}
}
});
add(btn1);
add(lbl1);
}
}
The idea is that when the button is clicked, it will check if the variable is equal to a number, and if it is, to change the JLabel text.
But when I click the button nothing changes. The button to roll the dice works fine.

How to make the "proximo"(next) button work and move the rocket(foguete)

I need to make my rocket move accordingly to the correct asnwer, if he(she) get it right, it will move(from the planet (left) to the (right) planet, else, it will do nothing, i have a counter for the right question, but i don't know what to do next.
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.HashMap;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Jogo0 extends JFrame implements ActionListener{
my variables
JPanel QUIZ;
JRadioButton opcao1;//choice1
JRadioButton opcao2;//choice2
JRadioButton opcao3;//choice3
ButtonGroup escolha;//buttongroup
JLabel questao; //question
JButton proximo; //next button
String [] [] alternativa; //alternatives
String [] [] correta; //correct answer
int acerto; //my counter
int posifogx = 0;
int posifogy = 300;
the images to the code, rocket, background, planets
ImageIcon imagfogut = new v ImageIcon(getClass().getResource("Foguetao2.gif"));
ImageIcon imagespac = new ImageIcon(getClass().getResource("Background.jpg"));
ImageIcon imageplantale = new ImageIcon(getClass().getResource("giphy.gif"));
ImageIcon imageterra = new ImageIcon(getClass().getResource("terra.gif"));
JLabel fog = new JLabel (imagfogut);
JLabel background = new JLabel (imagespac);
JLabel planetale = new JLabel (imageplantale);
JLabel terra = new JLabel (imageterra);
my method jogo, that will run the whole game
public Jogo0 (){
Janela(); //window
Imagens(); //img
QuizMetodo(); //my quizmethod
}
public void Imagens (){ //imgs
background.setBounds(0, 0, 1920, 1080);
fog.setBounds(posifogx, posifogy, 300, 200);
planetale.setBounds(-150, 300, 259, 259);
terra.setBounds(1000, 0, 379, 379);
}
public void Janela()//window {
setTitle("Game");
setLocation(0,0);
setVisible(true);
setSize(1920,1080);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(fog);
add(planetale);
add(terra);
add(background);
}
public void foguetex(int moverfoguetex) { //position of the rocket
this.posifogx = moverfoguetex;
moverfoguetex = moverfoguetex + 100;
}
public void foguetey(int moverfoguetey) {
this.posifogy = moverfoguetey;
moverfoguetey = moverfoguetey + 100;
}
public synchronized void QuizMetodo(){
questoes();
Container cont=getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);
escolha=new ButtonGroup();
opcao1=new JRadioButton("Opção1",true);
opcao2=new JRadioButton("Opção2",false);
opcao3=new JRadioButton("Opção3",false);
escolha.add(opcao1);
escolha.add(opcao2);
escolha.add(opcao3);
questao= new JLabel("Salve seu planeta!");
questao.setForeground(Color.WHITE);
questao.setFont(new Font("tahoma", Font.BOLD, 14));
proximo=new JButton("Proximo");
proximo.setForeground(Color.BLACK);
proximo.addActionListener(aa);
opcao1.addActionListener(aa);
opcao2.addActionListener(aa);
opcao3.addActionListener(aa);
QUIZ=new JPanel();
QUIZ.setBackground(Color.DARK_GRAY);
QUIZ.setLocation(250,530);
QUIZ.setSize(800,150);
QUIZ.setLayout(new GridLayout(6,2));
QUIZ.add(questao);
QUIZ.add(opcao1);
QUIZ.add(opcao2);
QUIZ.add(opcao3);
QUIZ.add(proximo);
cont.add(QUIZ);
setVisible(true);
acerto = 0;
i = acerto;
lerqr(acerto);
}
public String getSelection(){
String selectedChoice=null;
Enumeration<AbstractButton> buttons=escolha.getElements();
while(buttons.hasMoreElements())
{
JRadioButton temp=(JRadioButton)buttons.nextElement();
if(temp.isSelected())
{
selectedChoice=temp.getText();
}
}
return(selectedChoice);
}
ActionListener aa = new ActionListener(){ //**I DONT KNOW WHAT TO DO HERE**
public void actionPerformed(ActionEvent e) {
}
};
public void questoes() { //questions
alternativa = new String [10][4];
alternativa[0][0] ="Qual é o composição química da água?";
alternativa[0][1] = "(2) Hidrogênio e (1) Carbono";
alternativa[0][2] = "(1) Hidroênio e (2) Oxigênio";
alternativa[0][3] = "(2) Hidrogênio e (1) Oxigênio";
alternativa[1][0] = "Pra que serve o Protocolo de Kyoto?";
alternativa[1][1] = "Defesa dos animais";
alternativa[1][2] = "Proteção contra emissão de gases";
alternativa[1][3] = "Codigo da reciclagem";
correta = new String [10] [2];
correta[0][0] = "Qual é o composição química da água?";
correta[0][1] = "(2) Hidrogênio e (1) Oxigênio";
correta [1][0] = "Pra que serve o Protocolo de Kyoto?";
correta [1][1] = "Proteção contra emissão de gases";
}
public void lerqr(int id){
questao.setText(" "+alternativa[id][0]);
opcao1.setText(alternativa[id][1]);
opcao2.setText(alternativa[id][2]);
opcao3.setText(alternativa[id][3]);
opcao1.setSelected(true);
}
public static void main(String[] args) { //my main method
new Jogo0();
}
I don't exactly know if you need the methods foguetex(); and foguetey(); in the way you implemented them but the last line in both methods doesn't seem to affect the program in any way.
To make the rocket move and calculate the movement you could simply modify the foguetex(); method like this (or write a method with a different name if you also need foguetex(); for something else in the code):
public void foguetex(int moverfoguetex) { // position of the rocket
this.posifogx = moverfoguetex + 100;
}
This will cause the value posifogx to get updated to the new x-value where the rocket should be (you can also modify foguetey(); if needed). After doing this you want to update the position of your JLabel displaying the rocket.
Therefore you reassign the bounds of the JLabel in the ActionListener similar to this (you possibly want to adapt it to the ActionListener you wrote yourself):
proximo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
foguetex(posifogx); //calculating new x-position
fog.setBounds(posifogx, posifogy, 300, 200); //updating the label's position
}
});
Hope this helps you.

Code refactoring for java swing

I want to know is it possible to re factor my code in such a way to remove duplication of almost identical statements, bar the variable name and what it is initialized to by using methods. Here is the code in question:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class Menu extends JFrame {
private JButton jbtChoc1 = new JButton("cross.gif");
private JButton jbtChoc2 = new JButton("nought.gif");
private JButton jbtChoc3 = new JButton("cross.gif");
private JButton jbtChoc4 = new JButton("nought.gif");
private JButton jbtChoc5 = new JButton("cross.gif");
private JButton jbtChoc6 = new JButton("nought.gif");
private JLabel foodLabelChoice = new JLabel("Main Dishes");
private ImageIcon food1Image = new ImageIcon("cross.gif");
private ImageIcon food2Image = new ImageIcon("nought.gif");
private ImageIcon food3Image = new ImageIcon("cross.gif");
private ImageIcon food4Image = new ImageIcon("nought.gif");
private ImageIcon food5Image = new ImageIcon("cross.gif");
private ImageIcon food6Image = new ImageIcon("nought.gif");
/**
* Constructor for the Menu.
*/
public Menu() {
Container cont = getContentPane();
cont.setLayout(new BorderLayout(5, 5));;
cont.setBackground(Color.white);
cont.add(foodLabelChoice, BorderLayout.NORTH);
JPanel girdSetup = new JPanel(new GridLayout(2, 3, 5, 5));
jbtChoc1.setIcon(food1Image);
girdSetup.add(jbtChoc1);
jbtChoc1.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc1.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc2.setIcon(food2Image);
girdSetup.add(jbtChoc2);
jbtChoc2.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc2.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc3.setIcon(food3Image);
girdSetup.add(jbtChoc3);
jbtChoc3.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc3.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc4.setIcon(food4Image);
girdSetup.add(jbtChoc4);
jbtChoc4.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc4.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc5.setIcon(food5Image);
girdSetup.add(jbtChoc5);
jbtChoc5.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc5.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc6.setIcon(food6Image);
girdSetup.add(jbtChoc6);
jbtChoc6.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc6.setHorizontalTextPosition(AbstractButton.CENTER);
cont.add(girdSetup, BorderLayout.CENTER);
}
/**
* Main method for test.
*
* #param args Initial setup.
*/
public static void main(String[] args) {
Menu frame = new Menu();
frame.setTitle("Menu");
frame.setSize(950, 400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I want to remove the duplication of the JButton and ImageIcon declaration and their settings within the constructor so that I simply call a single method instead of copy/paste of code.
You can do that by wrapping them in arrays, like this:
private static final String[] imageNames = {"cross.gif",
"nought.gif",
"cross.gif",
"nought.gif",
"cross.gif",
"nought.gif"};
private JButton[] jbtChocs = new JButton[imageNames.length];
private ImageIcon[] foodImages = new ImageIcon[imageNames.length];
public Menu() {
/* ... */
for(int i = 0; i < imageNames.length; i++){
jbtChocs[i] = new JButton(imageNames[i]);
foodImages[i] = new ImageIcon(imageNames[i]);
jbtChocs[i].setIcon(foodImages[i]);
girdSetup.add(jbtChocs[i]);
jbtChocs[i].setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChocs[i].setHorizontalTextPosition(AbstractButton.CENTER);
}
}
Since the behavior is the same for all of them, this lets you simply iterate over each object, applying the same actions to each one.

Passing variable from button press to another class

So I am trying to pass a variable from a button press in one class to another class, and can't quite figure it out. The button press creates a random number to simulate a dice roll, adds it to a variable which then is suppose to be passed to the board class which has the game board built on it and will then use said variable to determine which space on the board the player is on. Thank you in advance.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Game extends JPanel{
private JLabel lblP1Name, lblP2Name, lblRules, lblDiceRoll;
private JTextField txtP1Name, txtP2Name;
private JButton diceRoll;
private JRadioButton rdP1, rdP2;
private int dice;
private static int countP1;
private int countP2;
private JPanel panelNorth;
private void groupButton( ) {
ButtonGroup bg1 = new ButtonGroup( );
bg1.add(rdP1);
bg1.add(rdP2);
}
public Game() throws FileNotFoundException {
setLayout (new BorderLayout());
rdP1 = new JRadioButton("Player 1");
rdP2 = new JRadioButton("Player 2");
ButtonListener listener = new ButtonListener();
Player1 player1 = new Player1(countP1);
Player2 player2 = new Player2(countP2);
Board board = new Board();
Rules rules = new Rules();
JButton diceRoll = new JButton("Roll the dice!");
panelNorth = new JPanel();
panelNorth.setLayout(new GridLayout(1,3));
lblRules = new JLabel(rules.toString());
add(panelNorth, BorderLayout.NORTH);
panelNorth.add(rdP1);
panelNorth.add(diceRoll);
panelNorth.add(rdP2);
Card card = new Card();
add(player1, BorderLayout.WEST);
add(player2, BorderLayout.EAST);
add(lblRules, BorderLayout.SOUTH);
add(board, BorderLayout.CENTER);
diceRoll.addActionListener(listener);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent diceRoll){
Random random = new Random();
dice = random.nextInt(6)+1;
if(rdP1.isSelected()){
countP1 = countP1+dice;
if(countP1>48){
countP1=countP1-48;
}
}else if(rdP2.isSelected()){
countP2 = countP2+dice;
if(countP2>48){
countP2=countP2-48;
}
}
}
}
}
It's simple; just use references.
Instance your classes and pass the reference:
Board board = new Board();
YourClass yourClass = new YourClass(board);
This way you can set Board attributes from the class YourClass.
It's really easy, you could have learned how to do this just by reading basic Java books.

Update DefaultListModel with JList, using extends on subclass

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!
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.

Categories

Resources