HI, I'm learning Java but I got a problem when I'm using JList - but I don't know what it is.
My Main-Class:
import javax.swing.JFrame; import GUI_JComboBox.Gui; public class Main { public static void main(String[] args) { Gui go = new Gui(); go.setSize(300, 200); go.setVisible(true); go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
My Gui Class:
import javax.swing.*; import java.awt.*; import javax.swing.event.*; @SuppressWarnings("serial") public class Gui extends JFrame{ private JList list; private static String[] colornames = {"black", "blue", "red", "white"}; private static Color[] colors = {Color.BLUE, Color.BLACK, Color.WHITE, Color.RED}; public Gui(){ super("JList"); setLayout(new FlowLayout()); list = new JList(colornames); //This is how many the user can see by default. list.setVisibleRowCount(4); //This is so the user only can select ONE. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); add(new JScrollPane(list)); list.addListSelectionListener( new ListSelectionListener(){ //This is need, when we use the ListSelectionListener. public void valueChanged(ListSelectionEvent event) { //This will set the background color. getContentPane().setBackground(colors[list.getSelectedIndex()]); } } ); } }
Please Help me, I wanna learn Java - so please be nice to me, and explain VERY NOOB-FRENDLY what I did worng, and how I can fix it - Thank You!