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 7584957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:03:12+00:00 2026-05-30T19:03:12+00:00

We have a controller where we have a pre-declared JList and JLabel that we

  • 0

We have a controller where we have a pre-declared JList and JLabel that we are adding to a JPanel. Outside of the initial layout/adding code I can update a JLabel (e.g. change its text) but I can’t change the selection of the JList (e.g. jlist.setSelection(index) ) where it will update the UI. Code Below:

public class Test {
    private JPanel myPanel;
    private JList myList;
    private JLabel myLabel;

    public Test() {
         //Some init code here...
         this.myPanel = new JPanel();
         this.myPanel.setLayout(new GridBagLayout());

         GridBagConstraints gbc = new GridBagConstraints();

         String[] values = {"Value1", "Value2", "Value3", "Value4"}; //etc. etc.
         this.myList = new JList(values);
         this.myPanel.add(this.myList, gbc); //Add to panel

         this.myLabel = new JLabel("Label1");
         this.myPanel.add(this.myLabel, gbc); //Add to panel

         //Add some code here to add it to a frame or something to display
    }


    public void updateLabel(String workingNewLabel) {

         //The following works...
         this.myLabel.setText(workingNewLabel); 
          // as in the label component in the JPanel will 
          //now be updated to the new value of workingNewLabel
    }

    public void changeSelectionInListToSomeIndex(int nonWorkingNewIndex) {

         //The following does NOT update the JList in the panel... 
         //the selection remains whatever it was last set to.

         this.myList.setSelectedIndex(nonWorkingNewIndex);
    }
}

I’ve been able to get around this by iterating through all the components in myPanel looking for the JList component then set it to to myList eg.

//Code that goes after the line this.myPanel.add(this.myList, gbc);
for(Component component : this.myPanel.getComponents() ) {
     //Iterate through it all until...
     if (component.getClass() == this.myList.getClass()) {
         this.myList = (JList) component; //cast the component as JList
     }
 }

Why do I need to do this for a JList but not for the JLabel? This is a workaround but it seems extremely hack-ish.

Thanks in advance!
-Daniel

  • 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-05-30T19:03:13+00:00Added an answer on May 30, 2026 at 7:03 pm

    @JB’s right. Here’s a working sscce:

    test

    /** @see http://stackoverflow.com/q/9540263/230513 */
    public class Test {
    
        private static Test test = new Test();
        private JPanel myPanel;
        private JList myList;
        private JLabel myLabel;
    
        public Test() {
            myPanel = new JPanel();
            myPanel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            String[] values = {"Value1", "Value2", "Value3", "Value4"};
            myList = new JList(values);
            myPanel.add(this.myList, gbc);
            myLabel = new JLabel("Label1");
            myPanel.add(this.myLabel, gbc);
            myPanel.add(new JButton(new AbstractAction("Select Value3") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    test.updateList(2);
                }
            }));
        }
    
        public void updateLabel(String label) {
            myLabel.setText(label);
        }
    
        public void updateList(int index) {
            myList.setSelectedIndex(index);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(test.myPanel);
                    f.pack();
                    f.setLocationByPlatform(true);
                    f.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a /Register [GET] Action in the controller that pre-poluates a view-model with
When submitting one form from view, how can i also read or have controller
I have a controller action that is being executed by a link that was
I have multiple controller actions that takes an id public ActionResult Get(int? id) {
I have a controller with that I want to pass off some data to
I have a controller that contains a tableView that spawns another controller modally to
I am currently working on adding pre-validation to my website. So that pages that
I have a UITableViewController subclass that renders a list of pre-set categories and fetches
I have this code in my controller: [HttpGet] public ActionResult Register(UserRegistrationModel model) { return
Imagine that I have a Spring MVC controller something like this: @Controller @RequestMapping(/base-url) public

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.