Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-27-2008, 09:59 PM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
[SOLVED] Confused by the textField
I'm just having a look at ways to input and retrieve a string from a text field.
I've got confused somewhere along the way now I get an exception thrown and my components are missing from the JFrame (Plus It won't setBounds)
Any help would be appreciated, Das code:


Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ActionExample extends JFrame { Button goButton; TextField tField; public void ActionExample() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(0,0,200,200); Container c = getContentPane(); c.setLayout(new FlowLayout()); tField = new TextField("*Enter your text here*",35); goButton = new Button("Go"); c.add(tField); c.add(goButton); goButton.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent evt ) { repaint(); } } ); } public static void main(String args[]) { ActionExample ae = new ActionExample(); ae.setVisible(true); } public void paint(Graphics g) { g.drawString(tField.getText(),20,100); } }
Cheers :{p
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-28-2008, 01:51 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
Change public void ActionExample() to public ActionExample()
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-28-2008, 02:37 AM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
Cheers, :{p
Nice one! I can't believe I missed that. I'm assuming it can't be void because it returns a string from the textField to the public void paint(Graphics g)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-28-2008, 06:28 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
it can't be void because it returns a string from the textField to the public void paint(Graphics g)
No. It doesn't return anything. To return something to the paint method the paint method would have to call it first, eg,
Code:
public String getString { ... } public void paint(Graphics g) { ... String s = getString(); ... }
Actually, it's a constructor which is the only kind of method that does not have a return type in its method declaration. Java calls the constructor for class initialization. With the return type included, viz, void the method was never called.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-28-2008, 07:30 PM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
thanks for your help
Cheers, Are you like the only person on here? I't seems like your always giving answers. Much appreciated :{p
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allowing only numeric values in a TextField Java Tip Java Tips 0 03-02-2008 12:08 AM
what does it mean:confused: sivasayanth New To Java 2 01-12-2008 06:52 AM
JSP - getting value from a textfield Java Tip Java Tips 0 12-01-2007 10:58 PM
alignment of textfield in awt nitinborge5 New To Java 2 07-30-2007 01:16 PM
set focus to the textfield paty New To Java 1 07-24-2007 02:32 AM


All times are GMT +3. The time now is 05:15 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org