aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes sysntax eror Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "sysntax eror" Watch "sysntax eror" New topic
Author

sysntax eror

Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Hello all. I am new and I am having a tough time spotting my error here. The idea is to produce an applet that will have a JButton which I can push to display the string myName. Later I will make it so that it will chage the font size and style, but for now I can't even get it to display the String. It compiles fine but when I launch it in the browser or with the appletviewer, it shows the button and nothing happens when I click it. Can you see anything wrong with the way i set this up? Thanks in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JBlueGrey extends JApplet implements ActionListener
{
String myName = new String("Randy");
JButton btnPush = new JButton("Push");
Font firstFont = new Font("Helvetica", Font.ITALIC, 8);
public void init()
{
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(btnPush);
btnPush.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == btnPush)
{
Graphics draw = getGraphics();
draw.setFont(firstFont);
draw.drawString(myName, 10, 100);
}
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Phil,
Note that a "syntax error" is an error in the structure of your code, such that it won't even compile; your subject line is a little misleading.
Anyway, basically the problem is that you can't just paint in an event handler like that; there are specific methods you've got to override in which you can do your painting. Furthermore, I think it's very likely that, given the coordinates you're using, you're going to be trying to paint the text right on top of the button.
You could have a look at what the Java Tutorial has to say about painting to learn more.


[Jess in Action][AskingGoodQuestions]
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Thank you for the advice. I will check out the tutorial.
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
One more thing if you don't mind. I know that I can't paint there as you said. It is because ActionsPerformed doesnt automaticaly supply us with a graphics method so we have to create our own. (I thought I had this straight) Anyway I was thinking that Graphics draw = getGraphics(); would instantiate a graphics object and the getGraphics would provide the graphics capabilities. Am I way out in lef field here? If I am please slap me around and get me in the right direction. I thought that I was basicaly creating a graphics object with that line and would be able to do the things Graphics objects do such as paint. Oh well I will go back to the tutorial now.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Technically you can do what you're doing, but the thing is that once you paint something, it may soon need to be re-painted (window resized, overlapped, new components added, menu pops up, or many other reasons) and unless you provide a standard paintComponent() method that Java can call to do the repainting, then your original painting will be lost.
Mike Brown
Greenhorn

Joined: Oct 19, 2003
Posts: 25
Thanks for a good clear expaination. I will keep it in mind.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sysntax eror