Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quick Question  (Read 688 times)
0 Members and 1 Guest are viewing this topic.
Offline Kiddo

Senior Newbie





« Posted 2011-01-18 02:23:20 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
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);
      }
   }
   
}

Offline Kiddo

Senior Newbie





« Reply #1 - Posted 2011-01-18 04:06:09 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
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);
         }
      }
   }
   
}

Offline Kiddo

Senior Newbie





« Reply #2 - Posted 2011-01-18 04:16:57 »

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

1  
2  
3  
4  
5  
6  
try {
   do {
      Thread.sleep(100);
   } while(filename == null);
}
catch(Exception e) {}


Does anyone know of a method that will do this for me?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zoto

Senior Duke


Medals: 4



« Reply #3 - Posted 2011-01-18 04:30:03 »

Put the stuff you want to happen into functions then have the GUI events run your functions.
Offline SwampChicken
« Reply #4 - Posted 2011-01-18 04:37:03 »

Your problem is one of "flow" control...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
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();
  }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-16 18:59:02

Norakomi (39 views)
2014-10-16 06:22:06

Norakomi (31 views)
2014-10-16 06:20:20

lcass (36 views)
2014-10-15 07:18:58

TehJavaDev (66 views)
2014-10-13 15:39:48

TehJavaDev (65 views)
2014-10-13 15:35:47

TehJavaDev (56 views)
2014-10-13 15:32:37

BurntPizza (72 views)
2014-10-11 14:24:42

BurntPizza (44 views)
2014-10-11 14:10:45

BurntPizza (84 views)
2014-10-11 13:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 13:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 13:36:02

List of Learning Resources
by Longor1996
2014-08-16 01:40:00

List of Learning Resources
by SilverTiger
2014-08-05 10:33:27

Resources for WIP games
by CogWheelz
2014-08-01 07:20:17

Resources for WIP games
by CogWheelz
2014-08-01 07:19:50

List of Learning Resources
by SilverTiger
2014-07-31 07:29:50

List of Learning Resources
by SilverTiger
2014-07-31 07:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!