I'm very new to programming in general, but ive read half way through this book "Head first into java". It displays an example which i tried to write down and compile but i cant get past the compiler. My valueChanged method wont link the list from go() to "String selection = (String) list.getSelectedValue(); the "list" is underlined with a red line and i cant seem to get the correct syntax to link the main syntax with my listeners. Any help would be appriciated. `
package Test1;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
public class GUi extends JPanel implements ListSelectionListener {
public static void main(String [] args){
GUi gui = new GUi();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.DARK_GRAY);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JList list = new JList();
String[] listEntries = {"alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta"};
list = new JList(listEntries);
JScrollPane scroller = new JScrollPane(list);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
list.setVisibleRowCount(4);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
frame.setSize(500, 500);
frame.setVisible(true);
}
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
String selection = (String) list.getSelectedValue();
System.out.println(selection);
}
}
}
`