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.
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.
Comments