Thank you for the replies to my noob question.
To be more specific, I have a Main class with a main method which contains methods get_name and show_name.
Method get_name creates a new instance of class getInput and executes method myGUI which prompts the user to input a part name and part description into text fields. When the OK button is hit, an action listener sets string_name to the part name.
Method setString then sets return_string equal to string_name which is returned to method get_name in the Main class, which sets got_name equal to return_string.
Method show_name should then be executed, displaying a frame which shows the
string got_name. The trouble is, even though show_name appears after got_name in the main method, they both seem to execute simultaneously.
I'm attempting to develop a GUI class which can be executed from several different classes and pass strings to those classes. See below for the code:
Here is the Main class:
package javaapplication11;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static String got_name = null;
public static void main(String[] args)
{
get_name();
show_name();
}
public static void get_name(){
getInput MainInput = new getInput();
got_name = MainInput.myGUI();
}
public static void show_name(){
JFrame parentFrame = new JFrame();
JOptionPane.showMessageDialog(parentFrame, "got name is: "+got_name);
}
}
Here is the getInput class which is called from class Main:
package javaapplication11;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class getInput implements ActionListener
{
public String string_name = null;
public String string_desc = null;
public String return_name = "test";
public static String return_desc = null;
public static JTextField text_name;
public static JTextField text_desc;
public static ActionListener new_listener;
public static JFrame parentFrame;
public String myGUI()
{
parentFrame = new JFrame("New Part");
text_name = new JTextField(20);
text_desc = new JTextField(20);
JLabel label_name = new JLabel("Part number: ");
JLabel label_desc = new JLabel("Part description: ");
JButton ok_button = new JButton("OK");
JPanel panel_1 = new JPanel();
panel_1.add(label_name);
panel_1.add(text_name);
panel_1.add(label_desc);
panel_1.add(text_desc);
panel_1.add(ok_button);
parentFrame.getContentPane().add(panel_1);
parentFrame.setBounds(400, 200, 250, 160);
parentFrame.setVisible(true);
ok_button.addActionListener(new getInput());
return return_name;
}
public void setString(){
return_name = string_name;
}
public void actionPerformed(ActionEvent e)
{
string_name = text_name.getText();
string_desc = text_desc.getText();
parentFrame.dispose();
JFrame parentFrame_2 = new JFrame();
JOptionPane.showMessageDialog(parentFrame_2, "Name: "+string_name+
"\nDescription: "+string_desc);
setString();
}
}