components not intially displayed

This is probably a beginnerish problem but please bear with me.

I've recently tried to convert my awt program to swing and the only problem I'm getting is that the components only appear on the applet when i mouse over them (such as a button or a text area). the awt components did not used to do this and I'm not quite sure why it does do this. I've created a container in the the main public void init, setLayout to null, and added all the components.

Also on a different note, what exactly does thread-safe mean, I've seen it in a variety of places but am not quite sure how it works or if I should worry about it.

Any help is appreciated.

[674 byte] By [tw11a] at [2007-11-27 5:50:42]
# 1

Well who knows exactly what you are doing but here are some things to check:

a) don't override the paint() method of JFrame. It is responsible for painting all the child components

b) add all your components to the gui "before" invoking the frame.setVisible(...) method

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 15:38:37 > top of Java-index,Desktop,Core GUI APIs...
# 2
Have you implemented the start method of your applet? Could you post your init and start methods?
sztyopeka at 2007-7-12 15:38:37 > top of Java-index,Desktop,Core GUI APIs...
# 3
Maybe you should get rid of the bear, that can't help your concentration.
RedUnderTheBeda at 2007-7-12 15:38:37 > top of Java-index,Desktop,Core GUI APIs...
# 4

I've had the same problem and stared for years (not really) before I finally figured it out for myself.

If you had overrided the paint() method, then here is the code you should have put for it.

public void paint(Graphics g)

{

//Your painting code here...

super.paint(g);

}

Lord_Jirachia at 2007-7-12 15:38:37 > top of Java-index,Desktop,Core GUI APIs...
# 5

the program is on a JApplet because i need to draw several lines and grids, it also used Graphics2D which = (Graphics2D)g; Here is the init method and the paint method.

public void init()

{

setLayout(null);

Font myFont = new Font("Monospaced", Font.PLAIN, 11);

crashtab="\t";

a[0]=390;a[1]=510;

a[8]=390;a[9]=530;

x=0;wait=0;kx=0; ky=0;

makeBorder();

start = new Line2D.Double(390,490,390,550);

pass = new Line2D.Double(395,50,395,110);

move = new Button("Move");

rp = new Button("Repaint");

ymove = new TextField("",100);

xmove = new TextField("",100);

errormsgl = new Label("");

whoturn=new Label("Next move: Blue ");

rp.setBounds(0,0,50,25);

move.setBounds(140,250,200,50);

xmove.setBounds(140,300,100,20);

xmove.addActionListener(this);

ymove.setBounds(240,300,100,20);

ymove.addActionListener(this);

errormsgl.setBounds(140,225,300,25);

whoturn.setBounds(140,200,300,25);

add(move);

add(whoturn);

add(rp);

add(xmove);

add(ymove);

add(errormsgl);

xmove.requestFocus();

lom = new TextArea("List of Vectors\n\nBlue\t\tMagenta\n-\t\t-\n(0,0)\t\t(0,0)\n",0,0,1);

lom.setBounds(455,150,220,320);

lom.setEditable(false);

lom.setFont(myFont);

add(lom);

ins = new JTextArea("Please enter the x coordinate in the first box and the y coordinate in the second box.\nWhen entering in coordinates, note that positive means to the right and up while negative means down and to the left. After entering the coordinates, press \"Move\" or Enter.\n\nWhen you crash, the black dot is where you will begin on your turn.");

ins.setBounds(140,330,280,147);

ins.setLineWrap(true);

ins.setWrapStyleWord(true);

ins.setEditable(false);

add(ins);

move.addActionListener(this);

rp.addActionListener(this);

starter = true;

playgame=false;

crash=false;

apass=false;

bpass=false;

gameover=false;

}

public void paint(Graphics g)

{

g2 = (Graphics2D) g;

if(starter)

{

printMain();

starter = false;

}

else if(playgame)

{

g2.setStroke(new BasicStroke(3));

Line2D.Double move = new Line2D.Double(a[0+x], a[1+x], a[2+x], a[3+x]);

Point2D.Double cpt = didCrash(move);

if (cpt!=null)

{

if(crash) wait++;

crash=true;

appendLom();

double crashx = cpt.getX(), crashy = cpt.getY();

move.setLine(a[x], a[x+1], crashx, crashy);

Ellipse2D.Double boom = new Ellipse2D.Double(crashx-5,crashy-5,10,10);

a[2+x] = ((int)crashx/10)*10+kx;

a[3+x] = ((int)crashy/10)*10+ky;

g2.setColor(Color.red);

a[4+x]=0;a[5+x]=0;

g2.fill(boom);

errormsgl.setText("You crashed!");

g2.setColor(Color.black);

g2.fill(new Ellipse2D.Double(a[x+2]-3,a[x+3]-3,6,6));

kx=0;ky=0;

}

else appendLom();

if(x==0) g2.setColor(Color.blue);

else g2.setColor(Color.magenta);

g2.draw(move);

a[x] = a[2+x];

a[1+x] = a[3+x];

if(crash&&wait==1)

{

x=Math.abs(x-8);

crash=false;

appendLom();

wait=0;

}

if(crash) wait++;

x=Math.abs(x-8);

if(x==0) whoturn.setText("Next move: Blue");

else whoturn.setText("Next move: Magenta");

playgame=false;

}

}

basically the ins jtextarea does not display unless i hightlight the text, i also wanted to make lom a Jtextarea but had the same problems

Message was edited by:

tw11

tw11a at 2007-7-12 15:38:37 > top of Java-index,Desktop,Core GUI APIs...
# 6

First of all its hard to give answers because you don't even know what type of application you are writing. You posted in the Swing forum, but you are using AWT components and you are using AWT painting techniques.

You should not be using AWT components in a Swing application. Also, as you where told in the first reply, you don't override the paint() method of a Swing component. So you should be using JApplet not Applet and if you need to do custom painting then you extend JComponent or JPanel and override the paintComponent() method. Then you add this component to the content pane of your JApplet.

So start by creating a simple Swing JApplet with a couple of components. Once you understand those basics then you can add custom painting.

camickra at 2007-7-12 15:38:37 > top of Java-index,Desktop,Core GUI APIs...
# 7

Here are two advices at first approach:

1. Never override the paint() method if possible. Use a JPanel with paintComponent() overridden instead, then add this JPanel to your JApplet.

2. You also may want to call super.paintComponent(g) from the overridden paintComponent() method to make sure the border and all necessary things are displayed correctly.

If this doesn't help, we shall see what else we can do.

(Related topic may be: http://forum.java.sun.com/thread.jspa?threadID=5171351)

sztyopeka at 2007-7-12 15:38:37 > top of Java-index,Desktop,Core GUI APIs...