Solved

no output from drawstring method

Posted on 2003-12-01
16
329 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error with Java/Cache JDBC Classpath 2 31
micro services spring boot application error 3 143
Java - Why doesn't this JFrame work 3 41
MySQL  on Tomcat 8 27
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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now