Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

java - Wait and notify to wait for button press causing jframe not to load

I'm writing a method (getInfo) that is called by the main class of my program. There is a case where the user needs to select from a list (in a JComboBox) to determine what is returned by the method, so I want the method to wait until the user makes their choice. I am trying to use wait and notify to do this, however when the jframe pops up that holds the JComboBox where the user makes their selection, it is empty. I suspect that this is because calling wait() pauses my entire program, so how can I wait for the button to be pressed to finish the method and have it return something. Here is the class with the problem:

package sm;

import org.jsoup.Jsoup;
//import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ScrapingManager {
    
    String courseName;
    String courseType;
    String site;
    Element row;
    Elements allWithName;
    JComboBox<String> options;
    JFrame frame;
    String slots;
    String location;
    public static final Object obj = new Object();

    public ScrapingManager(String name) {
        courseName = name;
        try {
        courseType = name.substring(0, 4);
        } catch(Exception e) {System.out.println("Error: Course code must be at least 4 characters");};
        //getInfo(searchFirstPage());
    }
    
    public String searchFirstPage() {
        Document page;
        String result = "error";
        try {
            page = Jsoup.connect("https://app.stfx.ca/web/Forms/shared/Registrar_website_Timetable.htm").get();
            Element link = page.getElementsContainingOwnText(courseType).first();
            //System.out.println(link.attr("href"));
            result = link.attr("href");
        } catch(Exception e) {/*e.printStackTrace();*/System.out.println("Error: Course code not recognized");}
        return result;
    }
    
    public String[] getInfo() { 
        Document doc;
        slots = "Error";
        location = "Error";
        try {
            doc = Jsoup.connect(searchFirstPage()).get();
            allWithName = doc.getElementsContainingOwnText(courseName).select(":not(:contains('L'))");//it works without select part but shows labs too
            if(allWithName.size() > 1) {
                //give warning that it might be the wrong times
                frame = new JFrame("Warning");
                JPanel panel = new JPanel();
                JLabel warning1 = new JLabel("There are multiple classes with that name,");
                JLabel warning2 = new JLabel("select yours from the list below:");
                String[] array = new String[allWithName.size()];
                for(int i = 0; i < allWithName.size(); i++) {
                    array[i] = allWithName.get(i).parent().child(8).text();
                }
                options = new JComboBox<String>(array);
                JButton ok = new JButton("Select");
                ok.addActionListener(new SubmitListener());
                panel.add(warning1);
                panel.add(warning2);
                panel.add(options);
                panel.add(ok);
                frame.getContentPane().add(panel);
                frame.setSize(400, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                synchronized(obj) {
                    try {
                    obj.wait();
                    } catch(Exception e) {}
                }
            } else {
                row = doc.getElementsContainingOwnText(courseName).first().parent();
            }
            
            slots = row.child(8).text();
            location = row.child(9).text();
            //System.out.println(slots + " " + location);
        } catch(Exception e) {e.printStackTrace();}
        return new String[] {slots, location};
    }
    
    public class SubmitListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {    
              row = allWithName.get(options.getSelectedIndex());
              frame.dispose();
              synchronized(obj) {
                  obj.notify();
              }
        }    
    }
}

Thanks in advance!

question from:https://stackoverflow.com/questions/65893348/wait-and-notify-to-wait-for-button-press-causing-jframe-not-to-load

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of wait(), use JOptionPane.showInputDialog(...), like here:

import javax.swing.JOptionPane;

public class LanguagesSelector{
    public static void main(String[] args) {
        String[] languages = {"Spanish", "English", "Italian", "Other"};
        Object language = JOptionPane.showInputDialog(null,
                "Choose Language", "language", JOptionPane.PLAIN_MESSAGE,
                null, languages, languages[0]);
        System.out.println("Selected " + language);
    }
}

In JOptionPane class, the AWT (whom swing base on) thread wait for the user choose an option, so it is should work in your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...