How to setText from class1 to class2?

2019-08-05 20:10发布

I have a problem setting the text of a field in class1 to another field in class2. Basically, I have two classes. In class1 i have a method that allows the user to search for a word that is in a file(reading from file) and then when the word is found, i want to set it to the class2 "field1".

For example, if i search for "San", the searched word in class2 should show "San" and the second word should show "Aya".

I dont know where iam going wrong and the program doesn't show any errors. Any help will be appreciated. Thanks in advance.

file.txt

San Aya

public class MyFileReader {

    JTextField searchfield = new JTextField(10);
    JPanel panel = new JPanel();

    public MyFileReader() {
        panel.add(new JLabel("Search:"));
        panel.add(searchfield);
        panel.setLayout(new GridLayout(5, 2));
        int result = JOptionPane.showConfirmDialog(null, panel,
                "Search", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            MyContentManager contentManager = new MyContentManager();
            try {
                String stringSearch = searchfield.getText();
                BufferedReader bf = new BufferedReader(new FileReader("file.txt"));
                int linecount = 0;
                String line;
                ArrayList<String> list = new ArrayList<String>();
                while ((line = bf.readLine()) != null) {
                    list.add(line);
                    linecount++;
                    int indexfound = line.indexOf(stringSearch);
                    if (indexfound > -1) {
                        String[] word = line.split("\t");
                        String firstword = word[0];
                        String secondword = word[1];
                        contentManager.field1.setText(stringSearch);//This is the problem
                        contentManager.field2.setText(secondword);//This is the problem
                    }
                }
                bf.close();
            } catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }
        }
    }

    public static void main(String[] args) {
        new MyFileReader();
    }
}

class2

public class MyContentManager {

    JTextField field1 = new JTextField(10);
    JTextField field2 = new JTextField(10);
    JPanel panel = new JPanel();

    public MyContentManager() {
        panel.add(new JLabel("Searched For:"));
        panel.add(field1);
        panel.add(new JLabel("Second word:"));
        panel.add(field2);
        panel.setLayout(new GridLayout(5, 2));
        int result = JOptionPane.showConfirmDialog(null, panel,
                "Search found", JOptionPane.YES_NO_OPTION);
    }
}

标签: java swing oop
3条回答
爷、活的狠高调
2楼-- · 2019-08-05 20:20

class2 was built before the search, you have to delay it's instantiation and add two arguments in its constructor to set the fields with the proper values.

JOptionPane was shown in the constructor, if you want to use settter as commented, you have to move the dialog to this setter.

查看更多
Luminary・发光体
3楼-- · 2019-08-05 20:24

I would give your 2nd class setter methods, have it produce a JPanel that can be obtained via a getter method, and simply display it in a JOptionPane (if desired). For instance:

DamClass1.java

class DamClass1 {
   JTextField searchfield = new JTextField(10);
   JPanel panel = new JPanel();

   public DamClass1() {
      panel.add(new JLabel("Search:"));
      panel.add(searchfield);
      panel.setLayout(new GridLayout(5, 2));
      int result = JOptionPane.showConfirmDialog(null, panel, "Search",
            JOptionPane.YES_NO_OPTION);
      if (result == JOptionPane.YES_OPTION) {
         DamClass2 c2 = new DamClass2();
         String stringSearch = searchfield.getText();

         if (stringSearch.equals("Foo")) {
            c2.setField1(stringSearch);
            c2.setField2("Bar");

            int result2 = JOptionPane.showConfirmDialog(panel, c2.getPanel(),
                  "Search found", JOptionPane.YES_NO_OPTION);
         }

         // commented to make the code runnable for me.
         // try {
         // BufferedReader bf = new BufferedReader(new FileReader("file.txt"));
         // int linecount = 0;
         // String line;
         // ArrayList<String> list = new ArrayList<String>();
         // while ((line = bf.readLine()) != null) {
         // list.add(line);
         // linecount++;
         // int indexfound = line.indexOf(stringSearch);
         // if (indexfound > -1) {
         // String[] word = line.split("\t");
         // String firstword = word[0];
         // String secondword = word[1];
         // c2.field1.setText(stringSearch);//This is the problem
         // c2.field2.setText(secondword);//This is the problem
         // }
         // }
         // bf.close();
         // } catch (IOException e) {
         // System.out.println("IO Error Occurred: " + e.toString());
         // }
      }
   }

   public static void main(String[] args) {
      DamClass1 s1 = new DamClass1();
   }
}

DamClass2.java

class DamClass2 {
   private JTextField field1 = new JTextField(10);
   private JTextField field2 = new JTextField(10);
   private JPanel panel = new JPanel();

   public DamClass2() {
      panel.add(new JLabel("Searched For:"));
      panel.add(field1);
      panel.add(new JLabel("Second word:"));
      panel.add(field2);
      panel.setLayout(new GridLayout(5, 2));
   }

   public JPanel getPanel() {
      return panel;
   }

   public void setField1(String text) {
      field1.setText(text);
   }

   public void setField2(String text) {
      field2.setText(text);
   }
}

Please put in some effort to post better formatted code when asking a question here.

查看更多
神经病院院长
4楼-- · 2019-08-05 20:29

You need to change the second class to some like:

public class MyContentManager {

    public int showFieldsFound(String first, String second) {
        JTextField field1 = new JTextField(10);
        field1.setText(first);
        JTextField field2 = new JTextField(10);
        field2.setText(second)
        JPanel panel = new JPanel();
        panel.add(new JLabel("Searched For:"));
        panel.add(field1);
        panel.add(new JLabel("Second word:"));
        panel.add(field2);
        panel.setLayout(new GridLayout(5, 2));
        return JOptionPane.showConfirmDialog(null, panel,
                "Search found", JOptionPane.YES_NO_OPTION);
    }
}

This will deal with showing the fields when you find them. Now your first class needs to do something like:

public class MyFileReader {

    JTextField searchfield = new JTextField(10);
    JPanel panel = new JPanel();

    public MyFileReader() {
        panel.add(new JLabel("Search:"));
        panel.add(searchfield);
        panel.setLayout(new GridLayout(5, 2));
        int result = JOptionPane.showConfirmDialog(null, panel,
                "Search", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            MyContentManager contentManager = new MyContentManager();
            try {
                String stringSearch = searchfield.getText();
                BufferedReader bf = new BufferedReader(new FileReader("file.txt"));
                int linecount = 0;
                String line;
                ArrayList<String> list = new ArrayList<String>();
                while ((line = bf.readLine()) != null) {
                    list.add(line);
                    linecount++;
                    int indexfound = line.indexOf(stringSearch);
                    if (indexfound > -1) {
                        String[] word = line.split("\t");
                        String firstword = word[0];
                        String secondword = word[1];
                        int resultFromShowing = contentManager.showFieldsFound(stringSearch, secondWord);
                    }
                }
                bf.close();
            } catch (IOException e) {
                System.out.println("IO Error Occurred: " + e.toString());
            }
        }
    }

    public static void main(String[] args) {
        new MyFileReader();
    }
}

Not sure what you want to do with the result from showing the words but I have changed the second class to return the value.

查看更多
登录 后发表回答