Solved

no output from drawstring method

Posted on 2003-12-01
16
346 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
How to convert String matching to regex in java 4 52
SequenceInputStream example 3 19
junit as external jar or library 7 24
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)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 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