Solved

no output from drawstring method

Posted on 2003-12-01
16
359 Views
Last Modified: 2010-03-31
Hi, below is part of a program that outputs a string when the user enters text into a text box and hits the enter button. The program compiles but when I press enter nothing is outputted. The button gets clicked and nothing happens. Ive commented the area where I think the problem is.
Cheers


//public class
  Label prompt;
       TextField input;
       String name;

//init class
prompt = new Label("Enter your name: ");
                 input = new TextField(20);
                 add(prompt);
                 add(input);
                 enter = new Button(" Enter ");
                 add(enter);


   public boolean action(Event e, Object o){
name = (input.getText());
if(e.target == enter){
           
            name = (input.getText()); //i think its do to with this line?
                  Graphics g = null;
                  g.drawString("Welcome" + name , 100 , 100);
                  }
     
     return true;
}
0
Comment
Question by:eire_ireland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 5
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9849806
You need to add a proper listener to your button

enter.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
       setStringToDraw(input.getTexT());
        
  }
});

and your paint/paintComponent should then be flagged to draw the String set in setStringToDraw and repaint to call. It would be easier to use a text-oriented component than doing painting
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9849812

>>setStringToDraw(input.getTexT());

should have been


       setStringToDraw(input.getText());

0
 

Author Comment

by:eire_ireland
ID: 9850114
Cheers for the help, I tried that but its giving an error, what other way could I go about it?

enter.addActionListener(new ActionListener() {
                       ^
C:\r.c.p\c.java:110: package enter does not exist
enter.addActionListener(new ActionListener() {
     ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -deprecation for details.
2 errors

Process completed.

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 9850132
'enter' is your button variable referred to here:

>>enter = new Button(" Enter ");

just add the code immediately after that
0
 

Author Comment

by:eire_ireland
ID: 9850352
Its not recognising the setStringtoDraw method. This is the code im using and the error is underneath.

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;

     public void init()
     {
prompt = new Label("Enter your name: ");
                 input = new TextField(20);
                 add(prompt);
                 add(input);
                 enter = new Button(" Enter ");
                 enter.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
        setStringToDraw(input.getText());
       
  }
});

C:\r.c.p\c.java:36: cannot resolve symbol
symbol: method setStringToDraw  (java.lang.String)
        setStringToDraw(input.getText());
        ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -deprecation for details.
1 error

Process completed.

                 add(enter);

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 9850442
Yes, that's because you'd have to write the setStringtoDraw method! If you want to make it easier and more direct, just use a String variable instead

stringToDraw = input.getText();
repaint();

your paint method would do

if (stringToDraw != null) {
  // draw the String
}

stringToDraw should be an instance variable
0
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 9852315
add a paint method to do your painting:

public void paint(Graphics g)
{
   super.paint(g);
   g.drawString("Welcome" + name , 100 , 100);
}

And change your existing method to:

   public boolean action(Event e, Object o){
name = (input.getText());
if(e.target == enter){
         
           name = (input.getText()); //i think its do to with this line?
           repaint();
               }
     
     return true;
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9852791
If you want to use an ActionListener on your button it would be:

enter = new Button(" Enter ");
enter.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
       name = input.getText();
       repaint();
  }
});
0
 

Author Comment

by:eire_ireland
ID: 9853331
Cheers for the help fellas, both your answers helped me so im stuck, can they be divided?
0
 
LVL 92

Expert Comment

by:objects
ID: 9853346
there should be a split points button
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9853347
Yep, just go for the 'Split points' link
0
 

Author Comment

by:eire_ireland
ID: 9853358
Objects: how does your action method call the paint method?
0
 
LVL 92

Expert Comment

by:objects
ID: 9853390
paint() gets called automatically whenever the component needs repainting.
Calling repaint() notifies that the component needs repainting and will result in paint() being called.
0
 

Author Comment

by:eire_ireland
ID: 9853692
ok, thats sound
0
 
LVL 92

Expert Comment

by:objects
ID: 9853729
From the code you have posted, you don't actually need a stringToDraw member var.
0
 

Author Comment

by:eire_ireland
ID: 9853775
oh ya, even better
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question