2

I have a class named Parser which gets some input and do some calculations and output the results. I also have a jFrame, which has some text fields. I am misunderstanding how to run the parser and use the inputs from the jFrame. I don't know if I should implement the action Listener in my Parser class? or should I import all my Parser class methods in the jFrame? should I have run method in my main of the Parser or should I use the void run in the jframe class??

Here is my class Parser:

public class Parser{
    public static List getXKeywords(String Url, int X, String html) throws Exception {
//somemethod with someoutput
    }
    public static void main(String[] args) throws Exception {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SpyBiteDemo Sp = new SpyBiteDemo();
                Sp.setVisible(true);
             int X=Sp.getKeywordcount();
              //this top line is not correct because it can only be done when the jframe jButton1 was clicked

            }
        });              
} 
}

and here is the jFrame;

public class SpyBiteDemo extends javax.swing.JFrame {
    /**
     * Creates new form SpyBiteDemo
     */
    public SpyBiteDemo() {
        initComponents();

    }
    public String getKeywordcount()
    {
    return jTextField4.getText();
    }
//some methods
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //get the input from the jframe
        //feed it to the parser?? how???
       String SeedUrl=jTextField1.getText();
            Parser P=new Parser();
            //I don't have access to methods 
           because they are static

    }
    }

here I am trying to get keywordcount variable from the jFrame which is the int X in the getXKeywords method.

Nickool
  • 3,662
  • 10
  • 42
  • 72
  • You might want to read up on the [model / view / controller pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). Dividing your GUI into a model, view, and controllers allows you to focus on one part of your GUI at a time, and makes developing complicated GUIs a lot easier. – Gilbert Le Blanc Apr 24 '16 at 22:08
  • @GilbertLeBlanc yeah it had been a long time that I did not work with java, I should do it. – Nickool Apr 24 '16 at 22:59

1 Answers1

0

I solved my problem with the help of this link

I created a constructor in my parser class and also included a jframe in the parser class as follow:

public class Parser {
        SpyBiteDemo Sp=new SpyBiteDemo();
    public Parser(SpyBiteDemo Sp)
            {
               this.Sp=Sp;
             int X = Sp.getXKeywords();
         //do whatever
      }

and in the action performed of the jframe class I call my parser constructor class:

public class SpyBiteDemo extends javax.swing.JFrame {
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

       Parser P=new Parser(this);

    }
}
Community
  • 1
  • 1
Nickool
  • 3,662
  • 10
  • 42
  • 72