0

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);
        }
    }

}

`

  • 3
    You need to learn about scope. See: http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean – azurefrog Jul 13 '16 at 21:05
  • @azurefrog alright. but how do i define a list which can be used in two methods at once such as go() and valueChanged() ? – Benjamin Rasmussen Jul 13 '16 at 21:21
  • 1
    This error suggests that you might be putting the cart before the horse as usually it's recommended to learn the basics of a language, such as variable scoping, before trying to tackle the advanced topics, such as GUI programming. Going through the introductory chapters of a Java text book or the intro tutorials of a Java tutorial would likely be time well spent and would help you in your future GUI programming. – Hovercraft Full Of Eels Jul 13 '16 at 22:23
  • 1
    @HovercraftFullOfEels I always cringe when newbies tag something with Swing yet are asking basic OO/attribute access questions. 'Putting the cart before the horse' certainly sums it up! – Andrew Thompson Jul 14 '16 at 02:46

1 Answers1

0

just add the list as a class field so it's visible to both method like this way

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;

public class GUi extends JPanel implements ListSelectionListener {

    JList list;
    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));
        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.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scroller);

        list.setVisibleRowCount(4);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.addListSelectionListener(this);

        frame.add(panel);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void valueChanged(ListSelectionEvent lse) {
        if (!lse.getValueIsAdjusting()) {
            String selection = (String) list.getSelectedValue();
            System.out.println(selection);
        }
    }

}
Romeo Sheshi
  • 901
  • 5
  • 7
  • That makes alot of sense, i tired to declare list in the main method but it still failed. Putting it right over the main method certainly did the trick! Thanks alot :) – Benjamin Rasmussen Jul 13 '16 at 22:15