Solved

no output from drawstring method

Posted on 2003-12-01
16
339 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
  • 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 32
Maven Project: Hibernate Dependencies Conflict 10 24
throw exception 21 44
Java: anonymous class 4 22
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

810 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