Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

why wont the textfield show up

  Asked By: Seth    Date: May 27    Category: Java    Views: 1109
  

But why wont the textfield show up when I run this?

Instead, when I place mouse over where the text field is , it will
change from pointer to edit pointer as if to say its there but I cant
see it.. the area is entirely white.

The only way it shows up is when I left click on it, then finaly I
can see the textfield outline

And the drawstrings show up right away...

is there a problem with my code or , my OS screwed up?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Server extends JApplet implements ActionListener {
JTextField hostIp;

public void init() {
Container cPane = getContentPane();
cPane.setLayout(new FlowLayout());

hostIp = new JTextField("0.0.0.0",15);
hostIp.setActionCommand("hostIp");
hostIp.addActionListener(this);
cPane.add(hostIp);
}

public void actionPerformed(ActionEvent e) {
}

public void paint(Graphics g) {
g.drawString("Welcome to Java!!", 50, 60 );
g.drawString("WTF , why isnt my text field visible!!!fark!!", 50, 0 );
}
}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Eline Bakker     Answered On: May 27

You might try putting a call to super.paint(g) at the top of your paint method.
That's where the painting of the components is handled. If you override it,
without calling it, the components don't get painted.

 
Answer #2    Answered By: Harriet Hughes     Answered On: May 27

As you can probably tell, Im just starting out jearning java.
The funnty thing is Im following examples from Osborne's java  2
Complete reference 3rd Ed, and many of the examples omit to do this,
or use repaint.

Even when looking at the examples at SUN it didnt seem obvious, i
guess cause they didnt over-ride paint.
java.sun.com/.../example-
swing/index.html#TextDemo
java.sun.com/.../example-
swing/TextDemo.java

 
Answer #3    Answered By: Blandina Garcia     Answered On: May 27

You're quite welcome. Sometimes, calling super.paint(g) is not necessary. It
depends on what you want your paint method to do. If you want to control how
everything is displayed, you can code  for it and leave the super class  out of
the process. If you want to enhance what the super class does by default, then
tell it to do it's thing then you can add to it with your own code. You
mentioned a call to repaint(). I wouldn't recommend doing that in the paint
method unless you really get a kick out of infinite loops.

 
Didn't find what you were looking for? Find more on why wont the textfield show up Or get search suggestion and latest updates.




Tagged: