ArrayList数据需要在JTable

时间:2016-02-27 11:17:04

标签: java arraylist automation jtable htmlunit

我是Java编程的新手,但我想要做的是通过HtmlUnit使用java从网站获取一些数据,我成功地设法做到了,但我需要在JTable中表示这些数据,那就是我搞不清楚了。我有一个Login类,它是我的gui和Nova类,其中实际提取数据。

public class login {
    private String novaUser;
    private String novaPass;
    private Nova myNova;

    private JFrame frame;
    private JTextField novaUsername;
    private JPasswordField novaPassword;



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    login window = new login();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public login() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 758, 440);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(0, 2, 0, 0));

        JLabel lblNovausername = new JLabel("NovaUserName");
        panel.add(lblNovausername);

        novaUsername = new JTextField();
        panel.add(novaUsername);
        novaUsername.setColumns(10);

        JLabel lblNovapassword = new JLabel("NovaPassword");
        panel.add(lblNovapassword);

        novaPassword = new JPasswordField();
        panel.add(novaPassword);

        JButton proceedButn = new JButton("Proceed");
        proceedButn.addActionListener(stuff -> onPress());

    }

    //button method

    private void onPress()  {
        // TODO Auto-generated method stub
        novaUser = novaUsername.getText();
        char[] inputNova = novaPassword.getPassword();
        novaPass = "";
        for (int i1 = 0; i1 < inputNova.length; i1++) {
            novaPass = novaPass + inputNova[i1];
        }

        if(novaPass != "" && novaUser!= ""){
            try{
                myNova = new Nova();
                myNova.NovaConnection(novaUser, novaPass );



            }
            catch (Exception e){
                JOptionPane.showMessageDialog( novaUsername, this, e.getMessage(), 0);
                return;
           }
                //result.add(resultField);


    }

        }`enter code here`

    }





public class Nova extends NovaOrders {

    private HtmlPage loginPage = null;
    private HtmlPage buttonPage = null;
    private WebClient client;
    private WebResponse response;
    private HtmlInput buttonLogin = null;
    private HtmlAnchor button1 = null; // ??
    private HtmlPasswordInput passField = null;
    private HtmlElement messageLogInText = null;
    private HtmlElement textField = null;
    private HtmlTextInput userField = null;
    private HtmlTextInput tInput = null;
    private HtmlElement orderCounter = null;
    int OrdersNumber = 0;
    String Orders="";
    String MON="";

    ArrayList<NovaOrders> listAllOrders = new ArrayList<NovaOrders>();

    public void setlistAllOrders(ArrayList<NovaOrders> listAllOrders) {
        this.listAllOrders = listAllOrders;
    }

    public  ArrayList<NovaOrders> getAllOrders() {
        return listAllOrders;
    }

    public void NovaConnection(String username, String password) throws Exception{

        messageLogInText = null;
        String content;

        client  = new WebClient();
        init();


         try {

                loginPage = client.getPage("some_url");
                passField = loginPage.getHtmlElementById("loginForm:passWord");
                buttonLogin = loginPage.getHtmlElementById("loginForm:login");
                userField = loginPage.getHtmlElementById("loginForm:userId");
                // Change the value of the text field
                userField.setValueAttribute(username);
                passField.setValueAttribute(password);
                response = loginPage.getWebResponse();
                content = response.getContentAsString();
            }
         catch (Exception e) {
                System.out.println("It appears to have an error");
            }
         try {
             loginPage = buttonLogin.click();
         }
         catch (Exception e){
            System.out.println("Login Error");
         }
         try {
             messageLogInText = buttonPage.getHtmlElementById("loginForm:messageBean");

         }
         catch (Exception e){
             System.out.println("Login is successful");
         }

         if(messageLogInText != null){
             throw new MyException("You failed to login");
         }
         NovaSearch(MON);

    }



        public void NovaSearch(String MON) throws Exception {
            // TODO Auto-generated method stub
            int i = 0;

            for(i=0; i < 100; i ++ ){
            try{
                loginPage = client.getPage("some_url");
                /*orderCounter = loginPage.getHtmlElementById("mf:headerRecordPageOutputFormat");
                OrdersNumber = Integer.parseInt(orderCounter.getTextContent());
                */
                textField = loginPage.getHtmlElementById("mf:orderlistByOwnerTable:"+ i +":masterOrderNumber");
                MON = textField.getTextContent();
                loginPage = client.getPage("some_url");
                tInput = loginPage.getHtmlElementById("mf:NOVA_SEARCH_ORDER_PARAMETER_MASTERORDERNUMBER");
                tInput.setValueAttribute(MON);
                buttonLogin = loginPage.getHtmlElementById("mf:searchInLiveButton");
                loginPage = buttonLogin.click();
                System.out.println(MON);
            }

            catch (Exception e) {
                System.out.println("These are all orders currently");
            }
            try{
                textField = loginPage.getHtmlElementById("mf:orderlistArchiveTable:0:country");
                Cty = Integer.parseInt(textField.getTextContent());
                System.out.println(Cty);
            }
            catch (Exception e){
                System.out.println("Cant find Cty");
            }
            try{
                textField = loginPage.getHtmlElementById("mf:orderlistArchiveTable:0:bpName");
                BP = textField.getTextContent();
                System.out.println(BP);

            }
            catch (Exception e){
                throw new MyException("Cant find BP");
            }
            try{
                textField = loginPage.getHtmlElementById("mf:orderlistArchiveTable:0:bpOrderRef");
                BPref = textField.getTextContent();
                System.out.println(BPref);

            }
            catch (Exception e){
                System.out.println("Cant find BPref");
            }
            try{
                textField = loginPage.getHtmlElementById("mf:orderlistArchiveTable:0:installAtCustomerName");
                User = textField.getTextContent();
                System.out.println(User);

            }
            catch (Exception e){
                throw new MyException("Cant find user ");
            }
            try{
                textField = loginPage.getHtmlElementById("mf:orderlistArchiveTable:0:received");
                String time = textField.getTextContent();
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
                Rec = format.parse(time);
                System.out.println(Rec);
            }
            catch (Exception e){
                System.out.println("Cant find the date");
            }
            try{
                textField = loginPage.getHtmlElementById("mf:orderlistArchiveTable:0:status");
                Status = textField.getTextContent();
                System.out.println(Status);
            }
            catch (Exception e){
                throw new MyException("problem with status");
            }
            try{
                textField = loginPage.getHtmlElementById("mf:orderlistArchiveTable:0:mon");
                MONnum = textField.getTextContent();
                System.out.println(MONnum);
            }
            catch (Exception e){
                throw new MyException("cant find mon");
            }

            //new order
            /*
            NovaOrders order = new NovaOrders();
            order.Cty = Cty;
            order.BP = BP;
            order.BPref = BPref;
            order.User = User;
            order.Rec = Rec;
            order.Status = Status;
            order.MONnum = MONnum;

            listAllOrders.add(order);
            */



            }

            }

        private void init() {
            Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
            Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
           //org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);
            client = new WebClient(BrowserVersion.FIREFOX_38);
            client.getOptions().setUseInsecureSSL(true);
            client.getOptions().setRedirectEnabled(true);
            client.setAjaxController(new NicelyResynchronizingAjaxController());
            client.getOptions().setJavaScriptEnabled(true);
            client.getOptions().setActiveXNative(true);
            client.getOptions().setThrowExceptionOnScriptError(true);
            client.getOptions().setThrowExceptionOnFailingStatusCode(true);
            client.getCookieManager().setCookiesEnabled(true);
            client.getOptions().setCssEnabled(true);
            client.setIncorrectnessListener((s, o) -> {});
            client.setCssErrorHandler(new ErrorHandler() {
                public void warning(CSSParseException e) throws CSSException {

                }

                public void error(CSSParseException e) throws CSSException {

                }

                public void fatalError(CSSParseException e) throws CSSException {

                }
            });

            client.setJavaScriptErrorListener(new JavaScriptErrorListener() {
                public void scriptException(InteractivePage interactivePage, ScriptException e) {

                }

                public void timeoutError(InteractivePage interactivePage, long l, long l1) {

                }

                public void malformedScriptURL(InteractivePage interactivePage, String s, MalformedURLException e) {

                }

                public void loadScriptError(InteractivePage interactivePage, URL url, Exception e) {

                }
            });

            client.setHTMLParserListener(new HTMLParserListener() {
                public void error(String s, URL url, String s1, int i, int i1, String s2) {

                }

                public void warning(String s, URL url, String s1, int i, int i1, String s2) {

                }
            });
        }

    }

1 个答案:

答案 0 :(得分:0)

以下是JTables的文件。

https://docs.oracle.com/javase/tutorial/uiswing/components/table.html

如果您在此帖子下面的此文档评论中遇到特定问题,那么有人可以回答