-3

The first code here comes from the prerec of the main code i cannot figure out.

boolean CompanyLoaded, StartingNum = true;

FirstCheck newwin = new FirstCheck();

public CollectNumbers() { 

        if (StartingNum = true) firstCheck = newwin.FirstCheck(); <----Runs FirstCheck okay

        if (CompanyLoaded = true) LoadCompany();

From here on out i have my FirstCheck class i'll post below. I have tried abstract overiding the actionPerformed, i have tried thread sleep, wait, and cannot figure out how to get my FirstCheck() method to wait until actionPerformed() to return my string(Int). Any help would be appreciated!

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

abstract class FirstCheck extends JFrame implements ActionListener {

    static int num;
    static boolean bnum = true;
    JTextField NumberEntry;

    Toolkit   tools   = Toolkit.getDefaultToolkit();
    Dimension windowLocVar = tools.getScreenSize(); 


    public int FirstCheck() {

        JFrame frame = new JFrame();

        NumberEntry = new JTextField();

        JButton Done = new JButton("Done");
        Done.addActionListener(this);

        JLabel label = new JLabel("Starting Check Number?");
        label.setVerticalTextPosition(JLabel.BOTTOM);  
        label.setHorizontalTextPosition(JLabel.CENTER);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3,1));
        panel.add(NumberEntry);
        panel.add(label);
        panel.add(Done); 

        frame.add(panel);                       
        frame.pack();                                      
        frame.setVisible(true);                            
        frame.setSize(250,150);                            
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setLocation(windowLocVar.width/2-300,windowLocVar.height/2-100);


        //try {
        //  Thread.sleep(5000);
        //  } 
        //catch(InterruptedException e) {
        //  // Restore the interrupted status
        //  Thread.currentThread().interrupt();
        //  }



    }



    public int actionPerformed(ActionEvent e) {
               bnum = false;
               num = Integer.parseInt(NumberEntry.getText());
               return num;

    }


    }
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

2

Instead of JFrame frame = new JFrame() use JDialog frame = new JDialog((Frame)null, true).

This will create a modal dialog which will block the code execution at the the point of of frame.setVisible(true) until the dialog is closed.

Take a look at How to use dialogs for more information.

In your actionPerformed method, you will need to store a "return" value that can be interrogated by your application once the dialog is closed...

public void actionPerformed(ActionEvent e) {
    bnum = false;
    num = Integer.parseInt(NumberEntry.getText());
    Object source = e.getSource();
    if (source instanceof Component) {
        // Close the dialog....
        SwingUtilities.getWindowAncestor((Component)source).dispose();
    }
}

Updated

Equally, you could just use a JOptionPane instead...

Take a look at

As a couple of examples

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366