Results 1 to 6 of 6
  1. #1
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Problem with my inputBox

    So I was doing something where I could ask multiple questions and then break the answers down too, if necessary.

    The code is:

    Java Code:
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.SwingUtilities;
    
    public class inputBox
    {
    	
    	JTextArea[]	input;																//A bunch of JtextArea boxes
    	String[][]	output;																//The string that will be returned
    	Boolean		wait	= true;															//A Boolean Variable
    	String[]		defaultSplicer = new String[] {","};										//Default splicer
    
    	public String[][] getInput(String[] questions)											//Simple call method
    	{
    		return getInput("Seperate with \";\" (with no spaces before or after) if necessary", questions, defaultSplicer);		//Call the more complicated getInput, and return that
    	}
    
    	public String[][] getInput(String instructions, String[] questions, String[] splicer)			//More complicated call method
    	{
    		if (splicer.length < questions.length)											//If there are more questions than splicers...
    		{
    			String temp = splicer[splicer.length - 1];										//create a temporary string that holds the last splicer
    			String[] tempsplicer = new String[questions.length];								//create a temporary string[] that has as many spaces as questions
    			for (int i = 0; i < splicer.length; i++)										//Populate the temporary string[] with the old string array strings
    			{
    				tempsplicer[i] = splicer[i];
    			}
    			for (int i = splicer.length; i < questions.length; i++)								//Populate the created one with the last splicer in the array
    			{
    				tempsplicer[i] = temp;
    			}
    			splicer = tempsplicer;													//Set the old String[] of splices to the new String[]
    		}
    		graphicalInterface(questions, instructions, splicer);									//Call the graphical portion
    		wait = true;															//Set wait to true
    		while (wait)																//Wait for the graphical interface to tell it to proceed
    		{
    			System.out.println("1");													//Used to ensure the problem is not in this thread
    		}
    		return output;															//Return output
    	}
    
    	private void graphicalInterface(final String[] questions, final String instructions, final String[] splicer)
    	{
    		JFrame Frame = new JFrame("Input");												//Create a new JFrame
    		input = new JTextArea[questions.length];											//Set the size of the JTextArea[] to the number of questions
    		Box holder = Box.createVerticalBox();												//formatting...
    		holder.add(new JLabel(instructions));												//Adding JLabel with the text Instructions to the top
    		for (int i = 0; i < questions.length; i++)											//For every question...
    		{
    			holder.add(Box.createVerticalStrut(15));										//formatting...
    			holder.add(Box.createHorizontalBox().add(new JLabel(questions[i])));					//Add a Jlabel that states the question
    			input[i] = new JTextArea("");													//Add a JtextBox to hold the users response
    			holder.add(Box.createHorizontalBox().add(new JScrollPane(input[i], ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS)));	//formatting...
    		}
    		JButton done = new JButton("Done");												//Add a "done" button to the bottom
    		done.addActionListener(new ActionListener()											//When the button is pressed...
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				output = new String[questions.length][];									//Set the number of responses to the number of questions
    				for (int i = 0; i < questions.length; i++)									//For every answer to a question...
    				{
    					output[i] = input[i].getText().split(splicer[i]);							//The response to the question is broken apart by the matching splice...
    				}
    				wait = false;												//Tell the other thread it can continue now that output has been set
    				SwingUtilities.windowForComponent((Component) e.getSource()).dispose();			//Destroy frame
    			}
    		});
    		holder.add(Box.createVerticalStrut(15));											//Formatting...
    		holder.add(done);																//Attach the "done" button
    		Frame.add(holder);																//Formatting...
    		Frame.setLocationRelativeTo(null);													//Center on screen
    		Frame.validate();																//Validate pieces now that they are all in place...
    		Frame.pack();																	//Fit the screen to the questions...
    		Frame.setSize(new Dimension((int) Frame.getHeight() + (int) input[0].getHeight() * input.length * 4, (int) Frame.getHeight()));	//fit the screen to my custom resizing specifications...
    		Frame.setResizable(true);																					//Let the user resize the frame
    		Frame.setVisible(true);																						//Let the user see the frame
    	}
    }
    It works great until it is used in an ActionListener...
    I am not sure why... is it because the ActionListener pauses graphical functions?

    a use example that works...
    Java Code:
    inputBox input = new inputBox();
    String Instructions = "This is the stuff at the top";
    String[] Questions = new String[] {"Question 1","Question 2","Question 3"};
    String[] Splicers = new String[] {" ",","}; //Will separate the answers in the third question by the "," (the last one...)
    String[][] answers = input.getInput(Instructions, Questions, Splicers);
    
    //To show answers output...
    for (int i = 0; i < answers.length; i++)
    {
         for (int ii = 0; ii < answers[i].length; ii++)
              {
              System.out.println("*" + answers[i][ii] + "*");
              }
         System.out.println();
    }
    A use example that freezes program...
    Java Code:
    JFrame frame = new JFrame("Tester");
    JButton button = new JButton("Click me!")
    button.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e)
         {
    inputBox input = new inputBox();
    String Instructions = "This is the stuff at the top";
    String[] Questions = new String[] {"Question 1","Question 2","Question 3"};
    String[] Splicers = new String[] {" ",","}; //Will separate the answers in the third question by the "," (the last one...)
    String[][] answers = input.getInput(Instructions, Questions, Splicers);
    
    //To show answers output...
    for (int i = 0; i < answers.length; i++)
    {
         for (int ii = 0; ii < answers[i].length; ii++)
              {
              System.out.println("*" + answers[i][ii] + "*");
              }
         System.out.println();
    }
         {
    });
    Last edited by watachiaieto; 05-23-2012 at 05:37 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with my inputBox

    Where's your main method? How is this code reached?

    You can't do this sort of code in a Swing GUI:

    Java Code:
            wait = true;
            while (wait)
            {
                System.out.println("1");
            }
    Read up on event-driven program, because pausing an event thread will only make your entire program freeze. Better to change the state of an object and base your GUI's behavior on that state.

  3. #3
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with my inputBox

    There is no main method - the class does not do anyting on instantiation.
    Instead the classes only function (that is public) is the getInput() function.

    Can you explain what you meant about:
    Better to change the state of an object and base your GUI's behavior on that state.
    That is what I thought I was doing by making it wait until the GUI makes it continue. the continuous loop ends once the parallel private graphicalInterface() function says it can.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with my inputBox

    Quote Originally Posted by watachiaieto View Post
    Can you explain what you meant about:
    "Better to change the state of an object and base your GUI's behavior on that state."
    Simple example, your program has a JButton (that for some reason you don't want to disable) and a boolean variable called go. And inside of the button's ActionListener you have an if block,

    Java Code:
    if (go) { 
       // ... allow the button to do something
    } else {
       // else warn the user that nothing can be done until go is true.
    }
    So there is no continuous loop that squashes code progress in a thread, but the code tells the button not to react until appropriate conditions are met.

    That is what I thought I was doing by making it wait until the GUI makes it continue. the continuous loop ends once the parallel private graphicalInterface() function says it can.
    I see no "parallel" code in your post above -- there are no background threads that I can see, but if I'm missing them, please point them out to me.

  5. #5
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: Problem with my inputBox

    I am not blocking the function of the button, I am blocking the continuation of the program until the button is pushed.

    The more complicated getInput method will continue on once the frame appears, and then it will return output before the user enters their answers or hits the "done" button, and will therefore return an String[][] that is null.

    So there is the infinite loop that waits intill wait is false. Wait is only false after the user clicks "done".

    A window can only return void, so I made a variable that the window could affect and then called that window in a function that returns a String[][] and that piece that returns a String[][] waits for the graphical interface instead of continuing on.

    in the getInput command:
    Java Code:
    while (wait)
    {
    }
    in the "done" button ActionListener
    Java Code:
    wait = false

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with my inputBox

    I know what you're doing, but as I've said above, what you're doing is wrong. That's not how event-driven programming works. I've also offered a possible solution above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •