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

  • Home
  • SEARCH
  • 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 8295753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:43:25+00:00 2026-06-08T14:43:25+00:00

fairly new to java here. I’m writing a GUI program of which I want

  • 0

fairly new to java here. I’m writing a GUI program of which I want to be able to append the strings (rather than the index number) of selected list items to a text area using a JButton. I am unaware of which java method would allow me to do this. When I use the getSelectedIndex method, it only allows me to append the index number, rather than the string value of the list item to my text area. If it is still unclear what I am asking, here is my code:

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

public class FantasyInterface extends JFrame implements ActionListener{

private JList list1;
private JList list2;
private JLabel runningbacks;
private JButton addPlayer1;
private JButton addPlayer2;
private JTextArea text;
String lineSeparator = System.getProperty("line.separator");
private String[] rbs = {"Matt Forte", "Arian Foster", "Maurice Jones-Drew", "Adrian Peterson", "Ray Rice"};
FantasyTeam team1 = new FantasyTeam(5);
FantasyTeam team2 = new FantasyTeam(5);

public FantasyInterface(){
    super("Fantasy Football Simulator");

    // Set up listsPanel
    JPanel listsPanel = new JPanel();

    runningbacks = new JLabel("Running Backs:");
    listsPanel.add(runningbacks);
    list1 = new JList(rbs);
    list1.setVisibleRowCount(5);
    list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    listsPanel.add(list1);

    addPlayer1 = new JButton("Add To Team 1");
    listsPanel.add(addPlayer1);

    list2 = new JList(rbs);
    list2.setVisibleRowCount(5);
    list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    listsPanel.add(list2);

    addPlayer2 = new JButton("Add To Team 2");
    listsPanel.add(addPlayer2);
    // Add formatted JPanels to Content Pane
    getContentPane().add(listsPanel, BorderLayout.NORTH);

    // Set up textPanel, where info will appear
    JPanel textPanel = new JPanel();
    text = new JTextArea(20, 20);
    textPanel.add(text);
    // Add formatted JPanels to Content Pane
    getContentPane().add(text, BorderLayout.SOUTH);

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent listSelectionEvent) {
            //System.out.println("First index: " + listSelectionEvent.getFirstIndex());
            //System.out.println(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            //System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
              JList list = (JList) listSelectionEvent.getSource();
              int selections[] = list.getSelectedIndices();
              Object selectionValues[] = list.getSelectedValues();
              for (int i = 0, n = selections.length; i < n; i++) {
                if (i == 0) {
                  System.out.println(" Selections: ");
                }
                System.out.println(selections[i] + "/" + selectionValues[i] + " ");
              }
            }
          }
        };
     list1.addListSelectionListener(listSelectionListener);
     addPlayer1.addActionListener(this);
}


public void actionPerformed(ActionEvent event){
    Object srcObj = event.getSource();

    if (srcObj == addPlayer1){
        text.append(lineSeparator + list1.getSelectedIndex());
    }
}

}

The last part,

if (srcObj == addPlayer1){
        text.append(lineSeparator + list1.getSelectedIndex());
    }

is where I am wondering if there is a method to get the selected index in string form. Thanks to anyone who helps!

  • 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-08T14:43:26+00:00Added an answer on June 8, 2026 at 2:43 pm

    Use:

    if (!list1.isSelectionEmpty()) {
       text.append(lineSeparator + list1.getModel().getElementAt(list1.getSelectedIndex()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Java and am writing an app that needs an XML
I am fairly new to Java so I am probably missing something fundamental here
I find myself a little baffled here. I'm fairly new to android/java development so
I am fairly new to Java programming and was following a tutorial located here:
I'm fairly new to Java (been writing other stuff for many years) and unless
I am writing a piece of code in Java (I'm fairly new to Java)
I'm fairly new to Java and Android programming in general but I would like
I'm still fairly new to Java, but I'm fairly sure this shouldn't happen. There's
I am fairly new to Java, and recently I was reading some material about
I am fairly new to Java so am looking for a good way of

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.