Search code examples
javaeclipseeclipse-neon

Cannot use declared JList - eclipse


I work with WindowBuilder and got a JFrame. I have already declared and put in a JList called dataset_list:

    JList<String> dataset_list = new JList<String>();
    dataset_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    dataset_list.setBounds(10, 245, 542, 106);
    panel.add(dataset_list);

in the main function of the program.

In a subfunction called query I want to add lines to that list, but cannot use the declared JList. I have also tried to use something like <mainfunction>.dataset_list but it wont work.

What am I missing (quiet new to GUI-Java)?


Solution

  • This answer gets posted so this question gets an answer! It is CW so I do not earn any rep for it.

    There were two things causing my problem:

    1. I had to declare the List outside of the main method to kame it visible to the subMethod.
    2. I had to set it static as well as well as the method or better as now that I worked further on the project this was for do what @HovercraftFullOfEels suggested, create an instance of my class and access it somewhere else and NOT from the main method.