Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default Inputting some text in a GUI.

    Hello. I am new to Java and am finding it difficult to grasp the GUI aspect.

    What I want to do is to have some kind of input box where the user enters some text and then presses a button which processes the text (using a class that I have developed).

    I don't have a clue as to how I should go about creating this input box. i would be very grateful if someone could give some suggestions as to the best way to create this input box?

    Many thanks

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

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    i wrote some code. I think that is what you are looking for.

    Java Code:
    import javax.swing.JTextArea;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class CreateInputBoxExample {
    	MyPanel panel = new MyPanel();
    	JFrame frame = new JFrame("my frame");
    	
    	public void createAndShowGUI() {
    		frame.add(panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,300);
    		frame.setLocation(100,100);
    		frame.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				CreateInputBoxExample createInputBox = new CreateInputBoxExample();
    				createInputBox.createAndShowGUI();
    			}
    		});
    	}
    
    }
    
    class MyPanel extends JPanel { 
    	MyPanel() {
    		JTextArea textArea = new JTextArea(numberOfColumns, numberOfRows);
    		JButton processButton = new JButton("process the text");
    
    		add(textArea);
    		add(processButton);
    	}
    	public static final int numberOfColumns = 10;
    	public static final int numberOfRows = 20;
    }

  4. #4
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Smile

    Thank you very much for both of your replies. I really appreciated the time and quality of both posts.

    I have spent sometime using the JOptionPane which works very nicely and the coding is simple.

    I have also copied and pasted the coding from the last post which perhaps produces a more impressive text box as well as demonstrates using more complicated coding. However, I would be grateful if someone could tell me what variable (field) the text that has been input is stored (so that I can process it)?

    Many thanks!

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    textArea.getText()

  6. #6
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Smile

    Thank you!

  7. #7
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool textArea.getText()

    Concerning the comment use "textArea.getText() ".

    Do I need to write a procedure to getText() ?

    Also if you are using a textfield would the code become textField.getText()??

Similar Threads

  1. GUI's and inputting doubles or ints
    By lopder1 in forum New To Java
    Replies: 19
    Last Post: 11-05-2009, 08:50 PM
  2. Inputting custom numbers
    By Jakora33 in forum New To Java
    Replies: 14
    Last Post: 09-08-2009, 09:14 PM
  3. Reversing and inputting files
    By jigglywiggly in forum New To Java
    Replies: 14
    Last Post: 03-23-2009, 08:28 AM
  4. inputting and writing a file backwards
    By jigglywiggly in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 07:24 PM
  5. Replies: 2
    Last Post: 12-06-2008, 06:54 PM

Posting Permissions

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