package java_forum.word_list;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class test {
private word[] words = new word[]{
new word(1, "wort1"),
new word(2, "wort2"),
new word(3, "wort 3"),
new word(4, "wort4"),
new word(5, "abwechslung"),
new word(6, "wort6"),
new word(7, "wort10020")
};
private JList list = new JList(words);
private final JLabel id = new JLabel("noch nichts ausgewählt");
public test() {
JFrame frame= new JFrame ("word-list");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel main = new JPanel();
main.setLayout(new BorderLayout());
frame.setContentPane(main);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
id.setText(((word)list.getSelectedValue()).getWordID()+"");
}
});
main.add(list, BorderLayout.CENTER);
main.add(id, BorderLayout.SOUTH);
}
private class word{
private int wordID;
private String word;
public word(int id, String string) {
wordID = id;
word = string;
}
public int getWordID() {
return wordID;
}
public void setWordID(int wordID) {
this.wordID = wordID;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
@Override
public String toString() {
return word;
}
}
public static void main(String[] args) {
new test();
}
}