JAVA - JComboBox action fungerar ej
Hej!
Jag har skapat en JComboBox problemet är att actionEvent triggas inte första gången man väljer något ur listan utan man måste välja två gånger, någon som ser vad som är fel:
package start;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import java.awt.event.*;
import javax.swing.JTextArea;
import javax.swing.JFrame;
public class start {
private JFrame frame;
JTextPane txtpnTest = new JTextPane();
JTextArea tb;
JTextArea text;
/**
* Launch the application.
*/
public static void main(String[] args) {
start window = new start();
window.frame.setVisible(true);
}
/**
* Create the application.
*/
public start() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize(){
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] food = {"spaghetti","korv","tomat","potatis"};
JComboBox comboBox = new JComboBox(food);
frame.getContentPane().add(comboBox, BorderLayout.WEST);
text = new JTextArea("this is text 1");
text.setText("start");
frame.getContentPane().add(text, BorderLayout.CENTER);
tb = new JTextArea("this is text 2");
//add action listener
comboBox.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
tb.setText(petName);
frame.getContentPane().add(tb, BorderLayout.CENTER);
}
});
}
}