Solved

no output from drawstring method

Posted on 2003-12-01
16
321 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

746 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

10 Experts available now in Live!

Get 1:1 Help Now