Quick Question

Hello All,

I’m in the process of making a GUI for a program that I wrote. What I want to do is take the character string that the user types into the text field and use it in main() like a regular string. Excuse me if this problem has been addressed in the past.

Here is my GUI thus far: Thanks to all those who respond.



import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;


public class GUITest extends JFrame {
	
	private String text;
	private static String filename;

	public GUITest() {
		
		super("LaTeX Template.exe");
		setSize(300,300);
		FlowLayout flo = new FlowLayout();
		setLayout(flo);
		JLabel Title = new JLabel("Doc Title:", JLabel.RIGHT);
		add(Title);
		JTextField text = new JTextField(20);
		add(text);
		JButton Create = new JButton("Create!");
		add(Create);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
	}
	
	public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals("Create!")) {
        	filename = "Hello";
        }
	}
	
	public static void main(String args[]) {
		
		GUITest Temp = new GUITest();
		if (filename != null) {
			System.out.print(filename);
		}
	}
	
}


Ok, I did a little research online and I now believe the code does almost everything I need it to. The only problem is that when I run the program it doesn’t wait for the user to enter values and the resulting fields end null when they shouldn’t be. How can I make the program wait on the user?

Thanks All,

Updated Code:



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

import javax.swing.*;


public class GUITest extends JFrame {
	
	private JTextField item1;
	
	public GUITest() {
		super("LaTeX Template");
		setLayout(new FlowLayout());
		
		item1 = new JTextField(10);
		add(item1);
		
		thehandler handler = new thehandler();
		item1.addActionListener(handler);
		
	}
	
	private class thehandler implements ActionListener {
		public void actionPerformed(ActionEvent event) {
		
			if(event.getSource() == item1) {
				TemplateCreator.filename = String.format("%s", event.getActionCommand());
				System.out.println(TemplateCreator.filename);
			}
		}
	}
	
}


Haha, I’ve found a real Getto way to solve this problem.



try {
	do {
		Thread.sleep(100);
	} while(filename == null);
}
catch(Exception e) {}


Does anyone know of a method that will do this for me?

Put the stuff you want to happen into functions then have the GUI events run your functions.

Your problem is one of “flow” control…


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUITest2 extends JFrame implements ActionListener {
  private static JTextField textField = new JTextField(20);

  public GUITest2() {
    super("LaTeX Template.exe");
    setSize(300, 300);
    FlowLayout flo = new FlowLayout();
    setLayout(flo);
    JLabel label = new JLabel("Doc Title:", JLabel.RIGHT);
    add(label);
    add(textField);
    JButton button = new JButton("Create!");
    button.addActionListener(this);
    add(button);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent event) {
    display(readTextField());
  }

  private String readTextField() {
    return textField.getText();
  }

  private void display(String s) {
    System.out.println("user typed in "+s);
  }

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