return multiple selections Jlist as a string

boydena

I am very new to Java so I apologize if this is a bad question. I am using a JList to allow a user to select multiple Cohort designations from a supplied list. This is just a single part of a larger program, but it's the only piece I'm having trouble with. I can create the JList, but I can't return a string of the selected items.How can I pull out the selected strings to list or Array for further use? My code is below.

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

private JButton starsBackButton, starsFinishButton;
private JList starsList;

public listSelection()
{

    this.setDefaultCloseOperation(
        JFrame.EXIT_ON_CLOSE);
    this.setTitle("Generate Circos Image");
    this.setLocationRelativeTo(null);

JPanel mainStarsPanel = new JPanel();
            mainStarsPanel.setLayout(new BorderLayout());


JPanel starsPanel = new JPanel();
                Border starsBorder = BorderFactory.createTitledBorder("Stars Cohort (Hold Ctrl to select multiples)");
                starsPanel.setBorder(starsBorder);  
                String [] starsCohort = {   "_ASC-COMSC","_ASC-WKSHP","_LC-ALL","ADM-AACEA","ADM-HSCON","AE03","AE10","AG","ANTW",
                                            "AP-CA","AP-DA","AP-UT","APEXBR","APEXBR_CC","APEXBRBAK","ASC-COMSCH","ASC-DC","ASC-ISTART",
                                            "ASC-MTL","ASC-MTOL","ASC-ORIENT","ASC-RSS","ASC-SI","ASC-SS","ASC-TUT","ASC-WKSHOP","ASC-WKSHP",
                                            "AW_RES_UG","AWW","BAF","BAF1","BAF2","BAF3","BAF4","BFA_THR_NW","BIO1050","CBS","CBS-Bak",
                                            "CCGO","CCGR","CCPN","CR-CLUBSPT","DCE","DUS","FA-SAPWARN","FCUSHPCS","FTC","FTIAC_TEST",
                                            "FTP-REACH","FYS","GRS","GRS_BAK","GRS_RMV","GRSP709","GRSP809","HIGH","HON","HON_AW_TRN",
                                            "HON_BAK","HON_TRN_NS","HON_TRNSFR","IMSD_UG","KUWAIT","LAW","LC-350MAT","LC-350MF07","LC-ALL",
                                            "LC-ALL-AY","LC-ANTH","LC-ASTRON_","LC-ASTRONG","LC-AVSD","LC-BIO1050","LC-BIO1510","LC-C2C",
                                            "LC-CBS","LC-CHINESE","LC-COM","LC-COM-Bak","LC-COMERAC","LC-COMERIC","LC-COS","LC-COSC","LC-CYB",
                                            "LC-DCE","LC-DDIS3","LC-DESIGN","LC-DUS","LC-ED-FYE","LC-ED-KIN1","LC-ED-KINP","LC-ED-PATH",
                                            "LC-ED-SUCC","LC-ED-TC","LC-EDMORRI","LC-EME0900","LC-EME0993","LC-EME1050","LC-EME1800",
                                            "LC-EME2010","LC-EME2020","LC-ENBRIDG","LC-ESP1050","LC-ESP1800","LC-ESP2010","LC-ESP2020",
                                            "LC-ESPall","LC-FNP","LC-FOCOM","LC-FORENS","LC-FREQ","LC-FREQ10C","LC-FREQ10H","LC-FYCLIN","LC-FYT",
                                            "LC-HEALTH","LC-HISTORY","LC-HON.BIO","LC-HON.COM","LC-HONORS","LC-HONORSA","LC-HONORSB","LC-HONORSE",
                                            "LC-HONORSH","LC-HONORSM","LC-JOURNAL","LC-KHS-PE","LC-KHSGRAD","LC-LSAMP","LC-MATHCOR","LC-MCNAIR",
                                            "LC-MOTOWN","LC-NDN","LC-NEUROSC","LC-NUR","LC-OISS","LC-ORGCHEM","LC-PASSMAT","LC-PEACE",
                                            "LC-PEACEBK","LC-PHA2","LC-PR","LC-PREMED","LC-PSPRELW","LC-PSY","LC-PSY2","LC-PSYLIFE","LC-RSP0993",
                                            "LC-RURALMD","LC-SBA1","LC-SBA2","LC-SLAVIC","LC-SW","LC-SWCRIT","LC-SWL","LC-TED2250","LC-TED2251",
                                            "LC-TIP","LC-UPREPHS","LC-USL","LC-VET","LC-VISARTS","LC-WSUCOMP","LC-WSUCUS","LC?COMERIC","LISADOBBS1",
                                            "LISADOBBS4","LS_DEANAWD","LWJD","LX-BIO105X","MAC","MAC_","MCCSC","NMS","NNFT_UG","NNFTDR","NWLB-G",
                                            "NWLB-U","OISS","P-af9124","PREEXCLUDE","PREMED","PRSN","PSC","PSC_TRNS","RA-RH","RH-AH","RH-FA",
                                            "RH-GH","RH-RA","RH-TT","SCH","SDS","TER_STEM","TRANS_GOLD","TRANSFERS","TRIO","TRNS_GOLD","TRNS_GREEN",
                                            "UAC_REIN","UAC-REIN","UNVN","UR-UROP","VET","VMD","WAS","WDIR-HF","WDIR-MC","WDIR-WC","WDS","WDUS",
                                            "WSCH","WSCH_2000","WSCH_AWAYN","WSCH_GOLD","WSCH_GREEN","WSGO","WSGR"};

                JList<String> starsList = new JList<String> (starsCohort);
                starsList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
                starsList.setVisibleRowCount(-1);
                starsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                JScrollPane starsScroll = new JScrollPane (starsList,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                starsScroll.setPreferredSize(new Dimension(300,250));
            starsPanel.add(starsScroll);

            starsBackButton = new JButton ("Back");
            //starsBackButton.addActionListener( e -> starsBackClick());
            starsFinishButton = new JButton ("Finish");
            starsFinishButton.addActionListener(e -> starsFinishClick());   
        Box starsBox = Box.createHorizontalBox();
            starsBox.add(starsBackButton);
            starsBox.add(Box.createHorizontalGlue());
            starsBox.add(starsFinishButton);


        mainStarsPanel.add(starsPanel);
        mainStarsPanel.add(starsBox, BorderLayout.SOUTH);

        this.add(mainStarsPanel);

    this.pack();
    this.setVisible(true);
}       

private void starsFinishClick ()
    {   
        List<String> testSelect = ArrayList<String> ();
            testSelect.add(starsList.getSelectedValuesList());


        System.out.println(testSelect);
    }
}

My understanding is I should be able to return the strings selected with .getSelectedValuesList . I also need to be able to alter the results to feed into another part of the program (an ArrayList). Thank you for any guidance or alternate methods to achieve the goal.

Pshemo

If you want to create list and add to it elements from other list you can either

  • pass list you want to copy from to constructor of new list like:

    List<String> newList = new ArrayList<>(oldListOfString);
    
  • instead of add use newList.addAll(oldList).

So your method can probably look like:

private void starsFinishClick ()
{   
    List<String> testSelect = new ArrayList<String>();
    testSelect.addAll(starsList.getSelectedValuesList());

    System.out.println(testSelect);
}

But you have also other problems. In your listSelection constructor you are hiding starsList field by creating local list

JList<String> starsList = new JList<String>(starsCohort);

which means that starsList field will still be null causing later NullPointerException. To solve this problem simply assign new value to this field (oh, and don't forget to add generic type to that field). :

starsList = new JList<String>(starsCohort);

Also in your

List<String> testSelect = ArrayList<String> ();
testSelect.add(starsList.getSelectedValuesList());

you forgot about new keyword between = and ArrayList<String> ().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

return multiple selections Jlist as a string

From Dev

Handsontable dropdowns with multiple selections

From Dev

Display on multiple selections

From Dev

SQL Query, Multiple Selections

From Dev

Multiple Interval Selection in JList

From Dev

Getting value of String in Jlist

From Dev

URL Query string using multiple drop down list selections (ideally PHP?)

From Dev

URL Query string using multiple drop down list selections (ideally PHP?)

From Dev

MediaWiki / OOUI: SelectWidget for multiple selections?

From Dev

Format multiple text selections in PowerPoint

From Dev

icCube cascading Filters with multiple selections

From Dev

jQuery checkbox selection with multiple selections

From Dev

How to target multiple selections with xclip

From Dev

How to avoid Multiple Jbutton selections

From Dev

skipping reCaptcha multiple image selections

From Dev

How to save multiple button selections?

From Dev

Get selections for multiple BootstrapTable tables

From Dev

Multiple selections on collection view cells

From Dev

MVC Linq for multiple selects that allow multiple selections

From Dev

SQL - INSERT INTO multiple columns from multiple selections

From Dev

adding values of multiple selection JList

From Dev

Grouped UITableView - allowing multiple selections and single selection

From Dev

TableView AllowsMultipleSelection - Still allowing multiple selections

From Dev

QListWidget MultiSelection always does multiple selections

From Dev

JQM Select List multiple selections and get values

From Dev

HTML radio buttons allowing multiple selections

From Dev

Update multiple rows in sql table for checkbox selections

From Dev

Multiple selections from a list using Python

From Dev

How to create a dialog with checkboxes that allows multiple selections

Related Related

  1. 1

    return multiple selections Jlist as a string

  2. 2

    Handsontable dropdowns with multiple selections

  3. 3

    Display on multiple selections

  4. 4

    SQL Query, Multiple Selections

  5. 5

    Multiple Interval Selection in JList

  6. 6

    Getting value of String in Jlist

  7. 7

    URL Query string using multiple drop down list selections (ideally PHP?)

  8. 8

    URL Query string using multiple drop down list selections (ideally PHP?)

  9. 9

    MediaWiki / OOUI: SelectWidget for multiple selections?

  10. 10

    Format multiple text selections in PowerPoint

  11. 11

    icCube cascading Filters with multiple selections

  12. 12

    jQuery checkbox selection with multiple selections

  13. 13

    How to target multiple selections with xclip

  14. 14

    How to avoid Multiple Jbutton selections

  15. 15

    skipping reCaptcha multiple image selections

  16. 16

    How to save multiple button selections?

  17. 17

    Get selections for multiple BootstrapTable tables

  18. 18

    Multiple selections on collection view cells

  19. 19

    MVC Linq for multiple selects that allow multiple selections

  20. 20

    SQL - INSERT INTO multiple columns from multiple selections

  21. 21

    adding values of multiple selection JList

  22. 22

    Grouped UITableView - allowing multiple selections and single selection

  23. 23

    TableView AllowsMultipleSelection - Still allowing multiple selections

  24. 24

    QListWidget MultiSelection always does multiple selections

  25. 25

    JQM Select List multiple selections and get values

  26. 26

    HTML radio buttons allowing multiple selections

  27. 27

    Update multiple rows in sql table for checkbox selections

  28. 28

    Multiple selections from a list using Python

  29. 29

    How to create a dialog with checkboxes that allows multiple selections

HotTag

Archive