Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Make JTextArea accept input from JButton

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Make JTextArea accept input from JButton

    Hey everybody,

    I don't really have a whats wrong with my code as much as a how would i do such a thing.

    So i have a JTextArea that asks whats your name, and then a bunch of other questions. Then i have a textfield and button for input to the JTextArea since i was very unsure of how to do something like System.in/out in eclipse.

    But i can't just put like an integer to keep track of what question its on.

    Im trying to figure out a way to make my JTextArea wait for me to hit the enter button and then accept the input into a string.

    Heres the code, their was more but i found this pretty tight snag so i decided to start over. Basically this will just let you see my GUI

     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
     
    public class General extends JFrame{
    	JPanel Outside;
    	JPanel info1, info2;
    	JButton[][] Buttons = new JButton[30][30];
    	JTextField Input;
    	JButton Enter;
    	static JTextArea Output;
    	General() throws FileNotFoundException{
    		setLayout(null);
    		setBounds(0, 0, 1498, 800);
    		Outside = new JPanel(new GridLayout(30, 30));
    		Outside.setBounds(10,10,700,700);
    		for(int a = 0; a < 30; a++){
    			for(int b = 0; b < 30; b++){
    				Buttons[a][b] = new JButton();
    				Buttons[a][b].setBorder(BorderFactory.createLineBorder(Color.BLACK));
    				Buttons[a][b].setBackground(Color.WHITE);
    				Outside.add(Buttons[a][b]);
    			}
    		}
    		Input = new JTextField();
    		Input.setBounds(10, 720, 602, 25);
    		Enter = new JButton("ENTER");
    		Enter.setBounds(612, 720, 90, 25);
    		Enter.addActionListener(new InputListener());
    		Output = new JTextArea();
    		Output.setBounds(730,10,300,735);
    		Output.setBorder(BorderFactory.createLineBorder(Color.BLACK,3));
    		info1 = new JPanel();
    		info1.setLayout(null);
    		info1.setBounds(1050, 10, 200, 735);
    		info1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		info2 = new JPanel();
    		info2.setLayout(null);
    		info2.setBounds(1270, 10, 200, 735);
    		info2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		add(info1);
    		add(info2);
    		add(Output);
    		add(Enter);
    		add(Input);
    		add(Outside);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	public static void main(String[] args) throws FileNotFoundException {
    		new General();
     
    	}
    	public class InputListener implements ActionListener{
    		public void actionPerformed(ActionEvent e) {
     
    		}
    	}
     
    }
    Last edited by crazed8s; December 5th, 2010 at 02:17 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Make JTextArea accept input from JButton

    Why would a JTextArea ask the user something?

    It's impossible to ask it and let the user enter it at same time, at least that I'm aware of.

    You could ask them a question using JOptionPane and ask them to put it in text area.

    Then have

    String str = area.getText();

    if (str.equals("Something")
    do something

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Make JTextArea accept input from JButton

    But you'd have to have it clear it afterward after it did whatever you wanted it to do, or else thins will likely go wrong.

    (:
    Last edited by javapenguin; December 5th, 2010 at 02:37 AM.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Make JTextArea accept input from JButton

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Make JTextArea accept input from JButton

    Using a JOptionPane doesn't really make sense for my program. I was trying to find another way to allow the usser to input the data i needed but oh well guess ill keep trying

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Make JTextArea accept input from JButton

    Sounds like you want to listen for action events for when the JButton gets clicked. See: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Make JTextArea accept input from JButton

    no ive got that down alright the problem is i need my method to stop and wait for button to be pressed again and then take the text from the jtextfield and use it as input to decide what the next question should be.

    i just need a way to make my methods wait for enter to be pressed again because i don't want my action listener to be epicly long and call all the methods. i just want its input to be put into string and then whatever method im in be able to use that string. The more i say it the more it sounds like its gonna be really hard to do or impossible?!?

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Make JTextArea accept input from JButton

    Not fully sure I understand the problem, but from what I can gather I'll second Helloworld's advice, sounds like you should register one or more ActionListener's, so your method's don't wait for the button to get clicked but get called upon button click.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Make JTextArea accept input from JButton

    It sounds like you're trying to turn a naturally object oriented and event driven model and forcing it to be sequential. This is very difficult to do, and a rather poor GUI design (it can lead to GUI's which become unresponsive). However, you don't need to do this.
    The reason you don't want your program to loop and wait for the button to be pressed is because the rest of your GUI (painting, updating text inside the textbox, updating the little flashing carat, etc.) will not be able to happen, and this will freeze your GUI. You could run this on a separate thread, but you would just be wasting system resources and could enter race conditions if you don't handle multi-threading properly.

    Simply put whatever code you want to handle a user clicking the Enter button into a method, then call that method from actionPerformed with the appropriate parameters.

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == Enter) // the Enter button sent this event
        {
            handleUserAnswer(Input.getText()); // method to handle whatever the user inputed
        }
    }

  10. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Make JTextArea accept input from JButton

    Thats more or less what i was trying to avoid but if its the only way then i guess thats ok. But the problem is me thinking the whole program through. So my handleUserInput method would have to store the entire gamestate, the last output, and this string input, and then just interpret what the input was referencing. Thats what im struggling with. Just makes it harder to work through. I was looking for something like the system.out/in box at the bottom of eclipse but thats beyond my scope of understanding i assume.

Similar Threads

  1. Expanding JTextArea
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 27th, 2010, 09:15 PM
  2. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  3. how do I keep a persistent prompt in JTextArea and allow user input
    By cazmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2010, 12:14 PM
  4. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 09:08 PM
  5. Replies: 9
    Last Post: June 27th, 2009, 04:05 PM