将多个选择Jlist作为字符串返回

时间:2015-11-28 17:50:44

标签: java

我是Java的新手,所以如果这是一个糟糕的问题我会道歉。我正在使用JList来允许用户从提供的列表中选择多个Cohort指定。这只是一个较大程序的一部分,但它是我唯一遇到麻烦的部分。我可以创建JList,但是我不能返回所选项的字符串。如何将所选字符串拉出到列表或数组以供进一步使用?我的代码如下。

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);
    }
}

我的理解是我应该能够返回用.getSelectedValuesList选择的字符串。我还需要能够改变结果以提供给程序的另一部分(ArrayList)。感谢您提供任何指导或替代方法来实现目标。

1 个答案:

答案 0 :(得分:0)

如果您想创建列表并从其他列表中添加元素,您可以

  • 将要复制的列表传递给新列表的构造函数,如:

    List<String> newList = new ArrayList<>(oldListOfString);
    
  • 而不是add使用newList.addAll(oldList)

所以你的方法可能看起来像:

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

    System.out.println(testSelect);
}

但你还有其他问题。在listSelection构造函数中,您通过创建本地列表

来隐藏starsList字段
JList<String> starsList = new JList<String>(starsCohort);

这意味着starsList字段仍然是null,导致后来的NullPointerException。要解决此问题,只需为此字段分配新值(哦,不要忘记将泛型类型添加到该字段)。 :

starsList = new JList<String>(starsCohort);

同样在你的

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

您忘记了new=之间的ArrayList<String> ()关键字。