从JButton ActionListener填充JList

时间:2014-07-22 13:34:23

标签: java swing jframe jbutton jlist

在按下按钮后尝试填充JList时遇到很多问题。下面的代码使用了我以前成功使用过的技术,但我一直无法使用它。目标是在按下按钮后运行测试并显示通过的URL和在单独的JLists中失败的URL。

动作侦听器:

//Start button--starts tests when pressed.
        JButton start = new JButton("Start"); 
        start.setPreferredSize(new Dimension(400, 40));
        start.setAlignmentX(Component.CENTER_ALIGNMENT); 
        start.addActionListener(new Web(urlA, codeA, cb, passJ, failJ));
        panel2.add(start); 

动作侦听器方法:

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;


public class Web implements ActionListener {
    private ArrayList<String> urls; 
    private ArrayList<Integer> statusCodes;
    private JComboBox cb;

    private JList passJ = new JList();  
    private JList failJ = new JList(); 

    //constructor--allows other values to be used
    public Web(ArrayList<String> urls, ArrayList<Integer> statusCodes, JComboBox cb, JList passList, JList failList ){
        this.urls = urls; 
        this.statusCodes = statusCodes; 
        this.cb = cb;
        this.passJ = passJ;
        this.failJ = failJ; 

    }


    @Override
public void actionPerformed(ActionEvent event){

        ArrayList<String> resultsP = new ArrayList<String>(); 
        ArrayList<String> resultsF = new ArrayList<String>();

        //get source
        JButton start = (JButton) event.getSource();


        //get value from combobox
        String selected = cb.getSelectedItem().toString(); 

        if(selected.equals("ALL")){

        }

        if(selected.equals("STATUS CODE")){

            for(int i = 0; i < urls.size(); i++){


            try {
                URL u = new URL(urls.get(i));
                HttpURLConnection connection = (HttpURLConnection)u.openConnection(); //open connection and cast to HttpURLConnection
                connection.setRequestMethod("GET"); 
                connection.connect(); 
                int code = connection.getResponseCode();

                if (code == statusCodes.get(i)){
                    System.out.println(i + "."+ urls.get(i)+" \t\t\t PASS");
                    resultsP.add(urls.get(i)); 

                    }
                else{
                    System.out.println(i + "." +urls.get(i)+ "\t\t\t FAIL");
                    resultsF.add(urls.get(i)); 


                }

            } catch (MalformedURLException | ProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 



} 

            for (String str: resultsP){
                System.out.println(str);
            }
            System.out.println("/////////////////////////////////////////////////////////////////////////////////"); 
            for (String str: resultsF){
                System.out.println(str); 
            }

            passJ.removeAll(); 
            failJ.removeAll();

            passJ.setListData(resultsP.toArray());
            failJ.setListData(resultsF.toArray())   ; 

            passJ.repaint(); 
            failJ.repaint(); 


        }//StatusCodeTest


    }
}

如何将列表添加到GUI:

        JList passJ = new JList(urlA.toArray()); 

        JScrollPane scroll1 = new JScrollPane(passJ); 
        scroll1.setPreferredSize(new Dimension (700, 150)); 
        scroll1.setMaximumSize( scroll1.getPreferredSize() );
        scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 

        panel2.add(scroll1);        
        panel2.add(Box.createRigidArea(new Dimension(0,50)));

        JList failJ = new JList(urlA.toArray()); 
        JScrollPane scroll2 = new JScrollPane(failJ); 
        scroll2.setPreferredSize(new Dimension(700, 150)); 
        scroll2.setMaximumSize(scroll1.getPreferredSize()); 
        scroll2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 

        panel2.add(scroll2); 
        //spacer
        panel2.add(Box.createRigidArea(new Dimension(0,25)));

任何 GUI 舞会都会非常感激。

1 个答案:

答案 0 :(得分:1)

  1. 您的passJ课程和failJ课程中似乎有Web / GUI的不同实例。
  2. passJ.removeAll(); failJ.removeAll();无法清除JList中的JList项,<{3}}。
  3. 以下是向import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class TestFrame extends JFrame { private JList<Integer> normal; private JList<Integer> fail; private Integer[] vals; public TestFrame() { init(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } private void init() { normal = new JList<Integer>(new DefaultListModel<Integer>()); fail = new JList<Integer>(new DefaultListModel<Integer>()); vals = new Integer[]{1,2,3,4,5,6,7,8,9,33}; JButton add = new JButton("collect data"); add.addActionListener(getCollectListener()); JButton clear = new JButton("clear data"); clear.addActionListener(getClearListener()); JPanel p = new JPanel(); p.add(new JScrollPane(normal)); p.add(new JScrollPane(fail)); JPanel btnPanel = new JPanel(); btnPanel.add(add); btnPanel.add(clear); add(p); add(btnPanel,BorderLayout.SOUTH); } private ActionListener getClearListener() { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ((DefaultListModel<Integer>)normal.getModel()).removeAllElements(); ((DefaultListModel<Integer>)fail.getModel()).removeAllElements(); } }; } private ActionListener getCollectListener() { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for(Integer i : vals){ if(i%3==0){ ((DefaultListModel<Integer>)normal.getModel()).addElement(i); } else { ((DefaultListModel<Integer>)fail.getModel()).addElement(i); } } } }; } public static void main(String args[]) { new TestFrame(); } } 添加/清除项目的简单示例:

    {{1}}