hey there, I have the following code:

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

public class TabelaEShahut extends JPanel{
public static void main(String[] args){
new TabelaEShahut();
}
public TabelaEShahut(){
JFrame f = new JFrame();
f.setSize(500, 500);
f.setVisible(true);
f.setTitle("shah");
f.getContentPane().add(this);
}

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 700, 700);
g.setColor(Color.red);
g.fillRect(50, 50, 400, 400);
g.setColor(Color.black);
g.fillRect(150, 50, 100, 100);
g.fillRect(350, 50, 100, 100);
g.fillRect(50, 150, 100, 100);
g.fillRect(250, 150, 100, 100);
g.fillRect(150, 250, 100, 100);
g.fillRect(350, 250, 100, 100);
g.fillRect(50, 350, 100, 100);
g.fillRect(250, 350, 100, 100);
}

}

I expected this code to work properly and display a four by four chess board on my screen... and it did, indeed, but the problem is that it does not do so everytime i run the code. i can never know if it is going to output the result or not when i run it. initially, it refused to show the board like twenty times but i kept comparing the code with other files i have that i know to have worked and comipling and running the file in question and it suddenly started working - but not everytime i run it. i was wondering what would the problem be and if there is any way i can prevent such unpredictabilities from occuring. you might need to know that i used jGRASP to compile and run the file.

thanks a lot to anybody that replies and accept my apology for my long explanations.

nothing wrong with the code.

oh thanks, but i remain unaware of the reason why it won't work everytime i run it.

no, it works every time I run.

the problem might be your ide or compiler.

you are most likely right. but, would it be the version of jGRASP i am using. or could it be me computer? and if it is my ide, how do i know that?

what is jgrasp? I have heard it first time ever.

there are better and more common ides.
why not use eclipse, netbeans or jdeveloper?

i used eclipse initially, but it crashed... i need to reinstall it. jgrasp is an ide. anyways, don't let me take more of your time. i think ima just switch to a more reliable ide and see if it works better. thanks anyways. take care!

Just use a commandline compiler and a text editor.

You should never do anything in Swing outside the event dispatching thread, except those few methods which are guaranteed to be thread safe (SwingUtilities.invokeLater(), etc.). Your code should look like:

public static void main(String[] args){
   SwingUtilities.invokeLater(
      new Runnable() {
         public void run() {
            new TabelaEShahut();
        }
     } );
}

That is unlikely to be causing the problem, but for code with lots of stuff happening at startup it can cause random failures.

jGRASP only calls java to run your programs (it doesn't have its own version of Java as some IDEs do), so it is unlikely to be causing the problem unless it is not running the program at all (you could add a println() call to be sure).

Are you sure your window isn't popping up underneath jGRASP and you just aren't noticing it?

i see the frame every time i run the code. but, on some occasions i only see the frame without it's content (in this case the four by four, red and black, chess board painted on a white background). when i say only the frame, i mean a window with gray field in it instead of the content inside paintComponent.

i see the frame every time i run the code. but, on some occasions i only see the frame without it's content (in this case the four by four, red and black, chess board painted on a white background). when i say only the frame, i mean a window with gray field in it instead of the content inside paintComponent.

That's probably because you're adding the content pane after making the frame visible. Since this is happening on the main thread and the first paint (and lots of other stuff) is happening on the AWT event dispatching thread, you have a race condition. That is why it sometimes works and sometimes fails. Try:

f.getContentPane().add(this);
f.setSize(500, 500);
f.setVisible(true);
f.setTitle("shah");

Even though this will probably "work", you should use the invokeLater() anyway, otherwise your code can fail randomly for other reasons. Calling non-thead-safe Swing methods from outside the event dispatching thread is not guaranteed to work at all. It might "seem to always work" for your code on your system with your current version of Java, but might fail on others, or fail for no reason on yours one in a thousand times, or fail every time for some future version of Java.

amazing. you seem to be right Ibarowski. I switched the order of my methods inside my constructor method and they now behave properly everytime i run the code. thanks a lot.

amazing. you seem to be right Ibarowski. I switched the order of my methods inside my constructor method and they now behave properly everytime i run the code. thanks a lot.

Yes, but again, use the invokeLater() to launch your gui, otherwise similar problems could crop up.

i shall do that. thanks again. at school, we did not learn about the invokeLater() method, but I shall just copy the code you wrote eariler and study it. take care!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.