Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:40:30+00:00 2026-06-07T10:40:30+00:00

Writing a program for a project. I need to use a ListSelectionListener in order

  • 0

Writing a program for a project. I need to use a ListSelectionListener in order to grab the current selection from a JList in order to determine the current card for the CardLayout utilized by the other panel. I’d like to be able to at the very least have a String with the name of the selection be created by the Listener, if not outright using the CardLayout class’s show method to change the current card from within the Listener. What would be the simplest way to do this that would ultimately result in the current card being changed? Thank you so much for any assistance!

Source:

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ClientApp extends JFrame
{
    public static void main(String[] args)
    {
        new ClientApp();
    }


    public ClientApp()
    {
        this.setSize(750,380);
        this.setTitle("Honeydukes Muggle Ordering System");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel infoPanel = new JPanel(new CardLayout());
        JPanel invntryPanel = new JPanel();


        //Creating the array for the invntryPanel Jlist

        String[] candy = {"Acid Pops", "Bat's Blood Soup",
                          "Bertie Bott's Every Flavour Beans",
                          "Blood-flavoured Lollipops",
                          "Cauldron Cakes", "Charm Choc",
                          "Chocoballs", "Chocolate Cauldrons",
                          "Chocolate Frogs", "Chocolate Skeletons",
                          "Chocolate Wands", "Choco-Loco", "Cockroach Clusters",
                          "Nougat", "Crystallised Pineapple",
                          "Drooble's Best Blowing Gum", "Exploding Bonbons",
                          "Toffees", "Fizzing Whizzbees",
                          "Fudge Flies", "Ice Mice",
                          "Jelly Slugs", "Liquourice Wands",
                          "Pepper Imps", "Peppermint Toads",
                          "Pink Coconut Ice", "Pixie Puffs",
                          "Pumpkin Fizz", "Salt Water Taffy",
                          "Shock-o-Choc", "Skeletal Sweets",
                          "Splindle's Lick'O'Rish Spiders",
                          "Sugar Quills", "Sugared Butterfly Wings",
                          "Toothflossing Stringmints", "Tooth-Splintering Strongmints",
                          "Treacle Fudge", "Chocolates", "Wizochoc"};
        JList candyList = new JList(candy);
        candyList.setVisibleRowCount(18);
        candyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        ListSelectionListener sl = new ListSelectionListener() {
           public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                CardLayout.show(infoPanel, (String)candyList.getSelectedValue());
            }
           }
        };

        candyList.addListSelectionListener(sl);

        //Creating a scrollpane for the JList
        JScrollPane scroll = new JScrollPane(candyList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        invntryPanel.add(scroll);


        //Creating the cards
        JPanel startCard = new JPanel(new BorderLayout());
        JPanel acidPopsCard = new JPanel();
        JPanel batsBloodSoupCard = new JPanel();
        JPanel bertieBottsCard = new JPanel();
        JPanel bloodPopsCard = new JPanel();
        JPanel cauldronCakesCard = new JPanel();
        JPanel charmChocCard = new JPanel();
        JPanel chocoballsCard = new JPanel();
        JPanel chocCauldronsCard = new JPanel();
        JPanel chocFrogsCard = new JPanel();
        JPanel chocSkeleCard = new JPanel();
        JPanel chocWands = new JPanel();
        JPanel chocolocoCard = new JPanel();
        JPanel roachClustersCard = new JPanel();
        JPanel nougatCard = new JPanel();
        JPanel crystalPineappleCard = new JPanel();
        JPanel droobleGumCard = new JPanel();
        JPanel explodeBonbonsCard = new JPanel();
        JPanel toffeesCard = new JPanel();
        JPanel fizzWhizCard = new JPanel();
        JPanel fudgeFliesCard = new JPanel();
        JPanel iceMiceCard = new JPanel();
        JPanel jellySlugsCard = new JPanel();
        JPanel liquorWandsCard = new JPanel();
        JPanel pepImpsCard = new JPanel();
        JPanel pepToadsCard = new JPanel();
        JPanel pinkCocoIceCard = new JPanel();
        JPanel pixiePuffsCard = new JPanel();
        JPanel pumpkFizzCard = new JPanel();
        JPanel saltTaffeyCard = new JPanel();
        JPanel shockChocCard = new JPanel();
        JPanel skeleSweetsCard = new JPanel();
        JPanel spindleSpidersCard = new JPanel();
        JPanel sugarQuillsCard = new JPanel();
        JPanel sugarWingsCard = new JPanel();
        JPanel flossMintsCard = new JPanel();
        JPanel splintMintsCard = new JPanel();
        JPanel treacleFudgeCard = new JPanel();
        JPanel chocolatesCard = new JPanel();
        JPanel wizochocCard = new JPanel();

        //Adding the cards to the infoPanel
        infoPanel.add(startCard, "Start");
        infoPanel.add(acidPopsCard, "Acid Pops");
        infoPanel.add(batsBloodSoupCard, "Bat's Blood Soup");
        infoPanel.add(bertieBottsCard, "Bertie Bott's Every Flavour Beans");
        infoPanel.add(bloodPopsCard, "Blood-flavoured Lollipops");
        infoPanel.add(cauldronCakesCard, "Cauldron Cakes");
        infoPanel.add(charmChocCard, "Charm Choc");
        infoPanel.add(chocoballsCard, "Chocoballs");
        infoPanel.add(chocCauldronsCard, "Chocolate Cauldrons");
        infoPanel.add(chocFrogsCard, "Chocolate Frogs");
        infoPanel.add(chocSkeleCard, "Chocolate Skeletons");
        infoPanel.add(chocWands, "Chocolate Wands");
        infoPanel.add(chocolocoCard, "Choco-Loco");
        infoPanel.add(roachClustersCard, "Cockroach Clusters");
        infoPanel.add(nougatCard, "Nougat");
        infoPanel.add(crystalPineappleCard, "Crystallised Pineapple");
        infoPanel.add(droobleGumCard, "Drooble's Best Blowing Gum");
        infoPanel.add(explodeBonbonsCard, "Exploding Bonbons");
        infoPanel.add(toffeesCard, "Toffees");
        infoPanel.add(fizzWhizCard, "Fizzing Whizzbees");
        infoPanel.add(fudgeFliesCard, "Fudge Flies");
        infoPanel.add(iceMiceCard, "Ice Mice");
        infoPanel.add(jellySlugsCard, "Jelly Slugs");
        infoPanel.add(liquorWandsCard, "Liquourice Wands");
        infoPanel.add(pepImpsCard, "Pepper Imps");
        infoPanel.add(pepToadsCard, "Peppermint Toads");
        infoPanel.add(pinkCocoIceCard, "Pink Coconut Ice");
        infoPanel.add(pixiePuffsCard, "Pixie Puffs");
        infoPanel.add(pumpkFizzCard, "Pumpkin Fizz");
        infoPanel.add(saltTaffeyCard, "Salt Water Taffy");
        infoPanel.add(shockChocCard, "Shock-o-Choc");
        infoPanel.add(skeleSweetsCard, "Skeletal Sweets");
        infoPanel.add(spindleSpidersCard, "Splindle's Lick'O'Rish Spiders");
        infoPanel.add(sugarQuillsCard, "Sugar Quills");
        infoPanel.add(sugarWingsCard, "Sugared Butterfly Wings");
        infoPanel.add(flossMintsCard, "Toothflossing Stringmints");
        infoPanel.add(splintMintsCard, "Tooth-Splintering Strongmints");
        infoPanel.add(treacleFudgeCard, "Treacle Fudge");
        infoPanel.add(chocolatesCard, "Chocolates");
        infoPanel.add(wizochocCard, "Wizochoc");

        //startCard building
        JLabel startLbl = new JLabel("<html><center>Welcome to the Honeydukes Muggle Ordering System!<br />Please select from one of our products to the left to begin!</center></html>");
        startCard.add(startLbl, BorderLayout.CENTER);

        this.add(invntryPanel, BorderLayout.LINE_START);
        this.add(infoPanel, BorderLayout.CENTER);
        this.setVisible(true);
    }
}

Errors:

ClientApp.java:54: error: local variable infoPanel is accessed from within inner
 class; needs to be declared final
                CardLayout.show(infoPanel, (String)candyList.getSelectedValue())
;
                                ^
ClientApp.java:54: error: local variable candyList is accessed from within inner
 class; needs to be declared final
                CardLayout.show(infoPanel, (String)candyList.getSelectedValue())
;
                                                   ^
ClientApp.java:54: error: non-static method show(Container,String) cannot be ref
erenced from a static context
                CardLayout.show(infoPanel, (String)candyList.getSelectedValue())
;
                          ^
Note: ClientApp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:40:31+00:00Added an answer on June 7, 2026 at 10:40 am

    I can think of a few ways. First, make the reference of the candyList final

    final JList candyList = new JList(candy);
    

    This should allow the inner class to see the list. Personally, I don’t like this approach, but that’s just me.

    The other thing you might try is to reference the list via the event object

    ListSelectionListener sl = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            JList candyList = (JList)e.getSource();
            if (e.getValueIsAdjusting() == false) {
                CardLayout.show(infoPanel, (String)candyList.getSelectedValue());
            }
           }
        };
    

    Or construct a new inner class that can have the list past to it as parameter

    public class MyInnerListener implements ListSelectionListener {
    
        private JList list;
    
        public MyInnerListener(JList list) {
    
            this.list = list;
    
        }
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                CardLayout.show(infoPanel, (String)list.getSelectedValue());
            }
        }
    
    }
    

    Just some ideas 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a console program in C# and I need to use a
As a pet project, I was thinking about writing a program to migrate applications
I'm writing a program on Linux in C to analyze core files produced from
I am writing a program that needs to run a java.jar server. I need
I'm about to start writing a program which will attempt to extract data from
I'm starting a new project which would greatly benefit from program add-ons. The program
(I Use Ubuntu) I'm looking for some pointers for writing a firewall program which
I'm writing a program in C, and I need to known the mime-type of
I'm writing a program in Python that needs to use a site's advanced search
Situation: I am writing a program to solve for primes. I need to solve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.