Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default getting text from a text field

    Hi guys, really confused on how to do this and hope you can help. I have a text field and a button that needs to retrieve the text when pressed and throw it to a variable. I know this is very newbish but I can't find the answer anywhere on the internet. thanks for the help.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you are looking for stuff on a pre built java class check the java API with google. The answer is the method getText(), it returns a string.

    If your text field is named blah it would be
    Java Code:
    blah.getText();
    You can set a string variable to be the return of the method call like this

    Java Code:
    String s = blah.getText();

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

    Default

    Quote Originally Posted by tomandhisjones View Post
    Hi guys, really confused on how to do this and hope you can help. I have a text field and a button that needs to retrieve the text when pressed and throw it to a variable. I know this is very newbish but I can't find the answer anywhere on the internet. thanks for the help.
    This kind of thing is relatively straight forward, and the fact that you're having problems with it suggests to me that you may have a problem with your program that is indirectly related to this. Why not show us your code and tell us more about the problems you're having with it?

  4. #4
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    all I have for code right now is the GUI I built in Jigloo and Eclipse, I just honestly don't know where to go from here because I can't figure out how to pass everything around:

    Java Code:
    import javax.swing.GroupLayout;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JEditorPane;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.LayoutStyle;
    
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
    
    
    
    
    
    public class NewJFrame extends javax.swing.JFrame {
    	private JEditorPane jEditorPane1;
    	private JLabel jLabel2;
    	private JButton jButton1;
    	private JTextField jTextField1;
    	private JLabel jLabel1;
    
    String characters;	
    	
    	
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				NewJFrame inst = new NewJFrame();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    			}
    		});
    	}
    	
    	public NewJFrame() {
    		super();
    		initGUI();
    	}
    	
    	private void initGUI() {
    		try {
    			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
    			getContentPane().setLayout(thisLayout);
    			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			{
    				jEditorPane1 = new JEditorPane();
    			}
    			{
    				jTextField1 = new JTextField();
    			}
    			{
    				jButton1 = new JButton();
    				jButton1.setText("submit");
    			}
    			{
    				jLabel1 = new JLabel();
    				jLabel1.setText("Enter text to be searched");
    			}
    			{
    				jLabel2 = new JLabel();
    				jLabel2.setText("Enter a character");
    			}
    			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
    				.addContainerGap(38, 38)
    				.addGroup(thisLayout.createParallelGroup()
    				    .addComponent(jEditorPane1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
    				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
    				        .addGap(38)
    				        .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
    				        .addGap(67)))
    				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    				    .addComponent(jTextField1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
    				    .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
    				.addGap(18)
    				.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
    				.addContainerGap(27, Short.MAX_VALUE));
    			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
    				.addContainerGap()
    				.addGroup(thisLayout.createParallelGroup()
    				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
    				        .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
    				        .addGap(39))
    				    .addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE))
    				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    				.addGroup(thisLayout.createParallelGroup()
    				    .addComponent(jEditorPane1, GroupLayout.Alignment.LEADING, 0, 179, Short.MAX_VALUE)
    				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
    				        .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
    				        .addGap(0, 94, Short.MAX_VALUE))
    				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
    				        .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
    				        .addGap(0, 128, Short.MAX_VALUE)))
    				.addContainerGap());
    			pack();
    			setSize(400, 300);
    		} catch (Exception e) {
    		    //add your error handling code here
    			e.printStackTrace();
    		}
    	}
    
    }
    here is what the program is supposed to do:

    Write a Java GUI application called Index.java that inputs several lines of text and a search character and uses String method indexOf to determine the number of occurrences of the character in the text.

    I had an A in this class up until 2 weeks ago and it has all been Greek to me ever since...

    Thanks again for the help.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by tomandhisjones View Post
    Write a Java GUI application called Index.java
    You are given requirements for a reason.

    The soultion is posted above. Do you have another question?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why are you using an editor pane? I'm sure it's a fine approach but do you need advanced font management? I feel like a simple text area would be sufficient. It seems like your final goal is to type some passage into the editor pane and then allow the user to search for all occurrences of the search term in the passage? Are you gonna highlight the found terms?(then I understand the editor pane)

    Do you know how to attach an action listener to the button? If you do you should be able to capture the search term and use index of on the passage. I hope this makes sense.

  7. #7
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    guess I just used the wrong pane, it says it needs to take multiple lines of text and I wasn't sure if I could do that with a text box. I will research actionlisters more. thanks for the help guys

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can definitely use a text area(which is easier to use) but doesn't offer much flexibility in font formatting.

    Look up adding action listeners to learn more. The basics of the action listener will look like this(pseudo-code)
    Java Code:
    declare string
    initialize string with text field text
    use index of to find occurences of string

  9. #9
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default Solved

    Got it figured out, just forgot to post the reply. Wrote a method to get the text and then added an actionListener and action handler to use the button to get the text.

    Java Code:
    private void getUserText(){
    
    String text;
    
    text = JTextField1.getText();
    
    }
    
    button.addActionListener(new ActionListener() {
    			 
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    getUserText();
                }
            });
    Thanks for everyone's input and help. Hopefully I passed the class.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped. Please mark your thread solved with the thread tools at the top of the page.

Similar Threads

  1. Get value of JComboBox text field
    By nik_meback in forum AWT / Swing
    Replies: 0
    Last Post: 01-07-2011, 02:48 PM
  2. Text-field templates.
    By jdipierro in forum New To Java
    Replies: 4
    Last Post: 05-14-2010, 12:48 AM
  3. Refreshing Text Field Value
    By nehakuls in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-13-2009, 11:05 AM
  4. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  5. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2008, 11:01 PM

Tags for this Thread

Posting Permissions

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